Last active
March 15, 2017 03:08
WebPack plugin examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from "path"; | |
function WebpackRequireListPlugin() {} | |
WebpackRequireListPlugin.prototype.apply = function(compiler) { | |
compiler.plugin("emit", function(compilation, callback) { | |
let filelist = {}; | |
compilation.modules.forEach((item, index) => { | |
let query = path | |
.relative(process.cwd(), item.userRequest) | |
.replace(/.[j|t]sx?$/g, "") | |
.replace("src/ts", "."); | |
filelist[query] = index; | |
}); | |
compilation.assets['js/require-ids.js'] = { | |
source: function() { | |
return "window.REQUIRE_LIST = " + JSON.stringify(filelist); | |
}, | |
size: function() { | |
return filelist.length; | |
} | |
}; | |
callback(); | |
}); | |
compiler.plugin("compilation", function(compilation) { | |
compilation.plugin("html-webpack-plugin-before-html-generation", function( | |
htmlPluginData, callback | |
) { | |
htmlPluginData.assets.js = [ | |
"../js/require-ids.js", | |
...htmlPluginData.assets.js | |
]; | |
callback(null, htmlPluginData); | |
}); | |
}); | |
}; | |
export default WebpackRequireListPlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment