RequireIncludePlugin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequireIncludeDependency = require("./RequireIncludeDependency");
  7. const RequireIncludeDependencyParserPlugin = require("./RequireIncludeDependencyParserPlugin");
  8. const ParserHelpers = require("../ParserHelpers");
  9. class RequireIncludePlugin {
  10. apply(compiler) {
  11. compiler.plugin("compilation", (compilation, params) => {
  12. const normalModuleFactory = params.normalModuleFactory;
  13. compilation.dependencyFactories.set(RequireIncludeDependency, normalModuleFactory);
  14. compilation.dependencyTemplates.set(RequireIncludeDependency, new RequireIncludeDependency.Template());
  15. params.normalModuleFactory.plugin("parser", (parser, parserOptions) => {
  16. if(typeof parserOptions.requireInclude !== "undefined" && !parserOptions.requireInclude)
  17. return;
  18. parser.apply(new RequireIncludeDependencyParserPlugin());
  19. parser.plugin("evaluate typeof require.include", ParserHelpers.evaluateToString("function"));
  20. parser.plugin("typeof require.include", ParserHelpers.toConstantDependency(JSON.stringify("function")));
  21. });
  22. });
  23. }
  24. }
  25. module.exports = RequireIncludePlugin;