changeScriptSourcePlugin.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. // If your plugin is direct dependent to the html webpack plugin:
  2. var HtmlWebpackPlugin = require('html-webpack-plugin');
  3. function ChangeScriptSourcePlugin () {}
  4. ChangeScriptSourcePlugin.prototype.apply = function (compiler) {
  5. compiler.hooks.compilation.tap('ChangeScriptSourcePlugin', function (compilation) {
  6. console.log('The compiler is starting a new compilation...')
  7. // Staic Plugin interface |compilation |HOOK NAME | register listener
  8. HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync(
  9. 'ChangeScriptSourcePlugin', // <-- Set a meaningful name here for stacktraces
  10. function (data, cb) {
  11. // Manipulate the content
  12. const listSize = data.assetTags.scripts.length;
  13. for (let i = 0; i < listSize; ++i) {
  14. const tag = data.assetTags.scripts[i];
  15. var path = tag.attributes.src;
  16. // remove build/ from src...
  17. data.assetTags.scripts[i].attributes.src = path.substring(path.indexOf("/") + 1);
  18. }
  19. // Tell webpack to move on
  20. cb(null, data);
  21. }
  22. )
  23. })
  24. }
  25. module.exports = ChangeScriptSourcePlugin