[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const {
|
---|
| 9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
| 10 | JAVASCRIPT_MODULE_TYPE_DYNAMIC
|
---|
| 11 | } = require("../ModuleTypeConstants");
|
---|
| 12 | const RequireIncludeDependency = require("./RequireIncludeDependency");
|
---|
| 13 | const RequireIncludeDependencyParserPlugin = require("./RequireIncludeDependencyParserPlugin");
|
---|
| 14 |
|
---|
| 15 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
| 16 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 17 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
| 18 |
|
---|
| 19 | const PLUGIN_NAME = "RequireIncludePlugin";
|
---|
| 20 |
|
---|
| 21 | class RequireIncludePlugin {
|
---|
| 22 | /**
|
---|
| 23 | * Apply the plugin
|
---|
| 24 | * @param {Compiler} compiler the compiler instance
|
---|
| 25 | * @returns {void}
|
---|
| 26 | */
|
---|
| 27 | apply(compiler) {
|
---|
| 28 | compiler.hooks.compilation.tap(
|
---|
| 29 | PLUGIN_NAME,
|
---|
| 30 | (compilation, { normalModuleFactory }) => {
|
---|
| 31 | compilation.dependencyFactories.set(
|
---|
| 32 | RequireIncludeDependency,
|
---|
| 33 | normalModuleFactory
|
---|
| 34 | );
|
---|
| 35 | compilation.dependencyTemplates.set(
|
---|
| 36 | RequireIncludeDependency,
|
---|
| 37 | new RequireIncludeDependency.Template()
|
---|
| 38 | );
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * @param {Parser} parser parser parser
|
---|
| 42 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
| 43 | * @returns {void}
|
---|
| 44 | */
|
---|
| 45 | const handler = (parser, parserOptions) => {
|
---|
| 46 | if (parserOptions.requireInclude === false) return;
|
---|
| 47 | const warn = parserOptions.requireInclude === undefined;
|
---|
| 48 |
|
---|
| 49 | new RequireIncludeDependencyParserPlugin(warn).apply(parser);
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | normalModuleFactory.hooks.parser
|
---|
| 53 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
| 54 | .tap(PLUGIN_NAME, handler);
|
---|
| 55 | normalModuleFactory.hooks.parser
|
---|
| 56 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
| 57 | .tap(PLUGIN_NAME, handler);
|
---|
| 58 | }
|
---|
| 59 | );
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | module.exports = RequireIncludePlugin;
|
---|