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 ConstDependency = require("./dependencies/ConstDependency");
|
---|
14 | const {
|
---|
15 | toConstantDependency
|
---|
16 | } = require("./javascript/JavascriptParserHelpers");
|
---|
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 | * Apply the plugin
|
---|
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 | * @param {JavascriptParser} parser the parser
|
---|
40 | * @param {JavascriptParserOptions} parserOptions options
|
---|
41 | * @returns {void}
|
---|
42 | */
|
---|
43 | const handler = (parser, parserOptions) => {
|
---|
44 | if (
|
---|
45 | parserOptions.requireJs === undefined ||
|
---|
46 | !parserOptions.requireJs
|
---|
47 | ) {
|
---|
48 | return;
|
---|
49 | }
|
---|
50 |
|
---|
51 | parser.hooks.call
|
---|
52 | .for("require.config")
|
---|
53 | .tap(PLUGIN_NAME, toConstantDependency(parser, "undefined"));
|
---|
54 | parser.hooks.call
|
---|
55 | .for("requirejs.config")
|
---|
56 | .tap(PLUGIN_NAME, toConstantDependency(parser, "undefined"));
|
---|
57 |
|
---|
58 | parser.hooks.expression
|
---|
59 | .for("require.version")
|
---|
60 | .tap(
|
---|
61 | PLUGIN_NAME,
|
---|
62 | toConstantDependency(parser, JSON.stringify("0.0.0"))
|
---|
63 | );
|
---|
64 | parser.hooks.expression
|
---|
65 | .for("requirejs.onError")
|
---|
66 | .tap(
|
---|
67 | PLUGIN_NAME,
|
---|
68 | toConstantDependency(
|
---|
69 | parser,
|
---|
70 | RuntimeGlobals.uncaughtErrorHandler,
|
---|
71 | [RuntimeGlobals.uncaughtErrorHandler]
|
---|
72 | )
|
---|
73 | );
|
---|
74 | };
|
---|
75 | normalModuleFactory.hooks.parser
|
---|
76 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
77 | .tap(PLUGIN_NAME, handler);
|
---|
78 | normalModuleFactory.hooks.parser
|
---|
79 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
80 | .tap(PLUGIN_NAME, handler);
|
---|
81 | }
|
---|
82 | );
|
---|
83 | }
|
---|
84 | };
|
---|