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 RuntimeGlobals = require("./RuntimeGlobals");
|
---|
9 | const ConstDependency = require("./dependencies/ConstDependency");
|
---|
10 | const {
|
---|
11 | toConstantDependency
|
---|
12 | } = require("./javascript/JavascriptParserHelpers");
|
---|
13 |
|
---|
14 | /** @typedef {import("./Compiler")} Compiler */
|
---|
15 |
|
---|
16 | module.exports = class RequireJsStuffPlugin {
|
---|
17 | /**
|
---|
18 | * Apply the plugin
|
---|
19 | * @param {Compiler} compiler the compiler instance
|
---|
20 | * @returns {void}
|
---|
21 | */
|
---|
22 | apply(compiler) {
|
---|
23 | compiler.hooks.compilation.tap(
|
---|
24 | "RequireJsStuffPlugin",
|
---|
25 | (compilation, { normalModuleFactory }) => {
|
---|
26 | compilation.dependencyTemplates.set(
|
---|
27 | ConstDependency,
|
---|
28 | new ConstDependency.Template()
|
---|
29 | );
|
---|
30 | const handler = (parser, parserOptions) => {
|
---|
31 | if (
|
---|
32 | parserOptions.requireJs === undefined ||
|
---|
33 | !parserOptions.requireJs
|
---|
34 | ) {
|
---|
35 | return;
|
---|
36 | }
|
---|
37 |
|
---|
38 | parser.hooks.call
|
---|
39 | .for("require.config")
|
---|
40 | .tap(
|
---|
41 | "RequireJsStuffPlugin",
|
---|
42 | toConstantDependency(parser, "undefined")
|
---|
43 | );
|
---|
44 | parser.hooks.call
|
---|
45 | .for("requirejs.config")
|
---|
46 | .tap(
|
---|
47 | "RequireJsStuffPlugin",
|
---|
48 | toConstantDependency(parser, "undefined")
|
---|
49 | );
|
---|
50 |
|
---|
51 | parser.hooks.expression
|
---|
52 | .for("require.version")
|
---|
53 | .tap(
|
---|
54 | "RequireJsStuffPlugin",
|
---|
55 | toConstantDependency(parser, JSON.stringify("0.0.0"))
|
---|
56 | );
|
---|
57 | parser.hooks.expression
|
---|
58 | .for("requirejs.onError")
|
---|
59 | .tap(
|
---|
60 | "RequireJsStuffPlugin",
|
---|
61 | toConstantDependency(
|
---|
62 | parser,
|
---|
63 | RuntimeGlobals.uncaughtErrorHandler,
|
---|
64 | [RuntimeGlobals.uncaughtErrorHandler]
|
---|
65 | )
|
---|
66 | );
|
---|
67 | };
|
---|
68 | normalModuleFactory.hooks.parser
|
---|
69 | .for("javascript/auto")
|
---|
70 | .tap("RequireJsStuffPlugin", handler);
|
---|
71 | normalModuleFactory.hooks.parser
|
---|
72 | .for("javascript/dynamic")
|
---|
73 | .tap("RequireJsStuffPlugin", handler);
|
---|
74 | }
|
---|
75 | );
|
---|
76 | }
|
---|
77 | };
|
---|