LoaderOptionsPlugin.js 993B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
  7. class LoaderOptionsPlugin {
  8. constructor(options) {
  9. if(typeof options !== "object") options = {};
  10. if(!options.test) options.test = {
  11. test: () => true
  12. };
  13. this.options = options;
  14. }
  15. apply(compiler) {
  16. const options = this.options;
  17. compiler.plugin("compilation", (compilation) => {
  18. compilation.plugin("normal-module-loader", (context, module) => {
  19. const resource = module.resource;
  20. if(!resource) return;
  21. const i = resource.indexOf("?");
  22. if(ModuleFilenameHelpers.matchObject(options, i < 0 ? resource : resource.substr(0, i))) {
  23. const filterSet = new Set(["include", "exclude", "test"]);
  24. Object.keys(options)
  25. .filter((key) => !filterSet.has(key))
  26. .forEach((key) => context[key] = options[key]);
  27. }
  28. });
  29. });
  30. }
  31. }
  32. module.exports = LoaderOptionsPlugin;