| 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 RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 13 | const {
|
|---|
| 14 | toConstantDependency
|
|---|
| 15 | } = require("../javascript/JavascriptParserHelpers");
|
|---|
| 16 | const ConstDependency = require("./ConstDependency");
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
|---|
| 19 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 20 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
|---|
| 21 |
|
|---|
| 22 | const PLUGIN_NAME = "RequireJsStuffPlugin";
|
|---|
| 23 |
|
|---|
| 24 | module.exports = class RequireJsStuffPlugin {
|
|---|
| 25 | /**
|
|---|
| 26 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 27 | * @param {Compiler} compiler the compiler instance
|
|---|
| 28 | * @returns {void}
|
|---|
| 29 | */
|
|---|
| 30 | apply(compiler) {
|
|---|
| 31 | compiler.hooks.compilation.tap(
|
|---|
| 32 | PLUGIN_NAME,
|
|---|
| 33 | (compilation, { normalModuleFactory }) => {
|
|---|
| 34 | compilation.dependencyTemplates.set(
|
|---|
| 35 | ConstDependency,
|
|---|
| 36 | new ConstDependency.Template()
|
|---|
| 37 | );
|
|---|
| 38 | /**
|
|---|
| 39 | * Handles the hook callback for this code path.
|
|---|
| 40 | * @param {JavascriptParser} parser the parser
|
|---|
| 41 | * @param {JavascriptParserOptions} parserOptions options
|
|---|
| 42 | * @returns {void}
|
|---|
| 43 | */
|
|---|
| 44 | const handler = (parser, parserOptions) => {
|
|---|
| 45 | if (
|
|---|
| 46 | parserOptions.requireJs === undefined ||
|
|---|
| 47 | !parserOptions.requireJs
|
|---|
| 48 | ) {
|
|---|
| 49 | return;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | parser.hooks.call
|
|---|
| 53 | .for("require.config")
|
|---|
| 54 | .tap(PLUGIN_NAME, toConstantDependency(parser, "undefined"));
|
|---|
| 55 | parser.hooks.call
|
|---|
| 56 | .for("requirejs.config")
|
|---|
| 57 | .tap(PLUGIN_NAME, toConstantDependency(parser, "undefined"));
|
|---|
| 58 |
|
|---|
| 59 | parser.hooks.expression
|
|---|
| 60 | .for("require.version")
|
|---|
| 61 | .tap(
|
|---|
| 62 | PLUGIN_NAME,
|
|---|
| 63 | toConstantDependency(parser, JSON.stringify("0.0.0"))
|
|---|
| 64 | );
|
|---|
| 65 | parser.hooks.expression
|
|---|
| 66 | .for("requirejs.onError")
|
|---|
| 67 | .tap(
|
|---|
| 68 | PLUGIN_NAME,
|
|---|
| 69 | toConstantDependency(
|
|---|
| 70 | parser,
|
|---|
| 71 | RuntimeGlobals.uncaughtErrorHandler,
|
|---|
| 72 | [RuntimeGlobals.uncaughtErrorHandler]
|
|---|
| 73 | )
|
|---|
| 74 | );
|
|---|
| 75 | };
|
|---|
| 76 | normalModuleFactory.hooks.parser
|
|---|
| 77 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|---|
| 78 | .tap(PLUGIN_NAME, handler);
|
|---|
| 79 | normalModuleFactory.hooks.parser
|
|---|
| 80 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|---|
| 81 | .tap(PLUGIN_NAME, handler);
|
|---|
| 82 | }
|
|---|
| 83 | );
|
|---|
| 84 | }
|
|---|
| 85 | };
|
|---|