plugin.js 850 B

1234567891011121314151617181920212223242526272829303132
  1. const fs = require('fs');
  2. class KW_WebpackPlugin {
  3. constructor(options) {
  4. this.karmaEmitter = options.karmaEmitter;
  5. this.controller = options.controller;
  6. }
  7. apply(compiler) {
  8. this.compiler = compiler;
  9. // webpack bundles are finished
  10. compiler.hooks.done.tap('KW_WebpackPlugin', async (stats) => {
  11. // read generated file content and store for karma preprocessor
  12. this.controller.bundlesContent = {};
  13. stats.toJson().assets.forEach((webpackFileObj) => {
  14. const filePath = `${compiler.options.output.path}/${
  15. webpackFileObj.name
  16. }`;
  17. this.controller.bundlesContent[webpackFileObj.name] = fs.readFileSync(
  18. filePath,
  19. 'utf-8'
  20. );
  21. });
  22. // karma refresh
  23. this.karmaEmitter.refreshFiles();
  24. });
  25. }
  26. }
  27. module.exports = KW_WebpackPlugin;