1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const path = require("path");
|
8 | const ParserHelpers = require("./ParserHelpers");
|
9 |
|
10 | class CommonJsStuffPlugin {
|
11 | apply(compiler) {
|
12 | compiler.hooks.compilation.tap(
|
13 | "CommonJsStuffPlugin",
|
14 | (compilation, { normalModuleFactory }) => {
|
15 | const handler = (parser, parserOptions) => {
|
16 | parser.hooks.expression
|
17 | .for("require.main.require")
|
18 | .tap(
|
19 | "CommonJsStuffPlugin",
|
20 | ParserHelpers.expressionIsUnsupported(
|
21 | parser,
|
22 | "require.main.require is not supported by webpack."
|
23 | )
|
24 | );
|
25 | parser.hooks.expression
|
26 | .for("module.parent.require")
|
27 | .tap(
|
28 | "CommonJsStuffPlugin",
|
29 | ParserHelpers.expressionIsUnsupported(
|
30 | parser,
|
31 | "module.parent.require is not supported by webpack."
|
32 | )
|
33 | );
|
34 | parser.hooks.expression
|
35 | .for("require.main")
|
36 | .tap(
|
37 | "CommonJsStuffPlugin",
|
38 | ParserHelpers.toConstantDependencyWithWebpackRequire(
|
39 | parser,
|
40 | "__webpack_require__.c[__webpack_require__.s]"
|
41 | )
|
42 | );
|
43 | parser.hooks.expression
|
44 | .for("module.loaded")
|
45 | .tap("CommonJsStuffPlugin", expr => {
|
46 | parser.state.module.buildMeta.moduleConcatenationBailout =
|
47 | "module.loaded";
|
48 | return ParserHelpers.toConstantDependency(parser, "module.l")(
|
49 | expr
|
50 | );
|
51 | });
|
52 | parser.hooks.expression
|
53 | .for("module.id")
|
54 | .tap("CommonJsStuffPlugin", expr => {
|
55 | parser.state.module.buildMeta.moduleConcatenationBailout =
|
56 | "module.id";
|
57 | return ParserHelpers.toConstantDependency(parser, "module.i")(
|
58 | expr
|
59 | );
|
60 | });
|
61 | parser.hooks.expression
|
62 | .for("module.exports")
|
63 | .tap("CommonJsStuffPlugin", () => {
|
64 | const module = parser.state.module;
|
65 | const isHarmony =
|
66 | module.buildMeta && module.buildMeta.exportsType;
|
67 | if (!isHarmony) return true;
|
68 | });
|
69 | parser.hooks.evaluateIdentifier
|
70 | .for("module.hot")
|
71 | .tap(
|
72 | "CommonJsStuffPlugin",
|
73 | ParserHelpers.evaluateToIdentifier("module.hot", false)
|
74 | );
|
75 | parser.hooks.expression
|
76 | .for("module")
|
77 | .tap("CommonJsStuffPlugin", () => {
|
78 | const module = parser.state.module;
|
79 | const isHarmony =
|
80 | module.buildMeta && module.buildMeta.exportsType;
|
81 | let moduleJsPath = path.join(
|
82 | __dirname,
|
83 | "..",
|
84 | "buildin",
|
85 | isHarmony ? "harmony-module.js" : "module.js"
|
86 | );
|
87 | if (module.context) {
|
88 | moduleJsPath = path.relative(
|
89 | parser.state.module.context,
|
90 | moduleJsPath
|
91 | );
|
92 | if (!/^[A-Z]:/i.test(moduleJsPath)) {
|
93 | moduleJsPath = `./${moduleJsPath.replace(/\\/g, "/")}`;
|
94 | }
|
95 | }
|
96 | return ParserHelpers.addParsedVariableToModule(
|
97 | parser,
|
98 | "module",
|
99 | `require(${JSON.stringify(moduleJsPath)})(module)`
|
100 | );
|
101 | });
|
102 | };
|
103 |
|
104 | normalModuleFactory.hooks.parser
|
105 | .for("javascript/auto")
|
106 | .tap("CommonJsStuffPlugin", handler);
|
107 | normalModuleFactory.hooks.parser
|
108 | .for("javascript/dynamic")
|
109 | .tap("CommonJsStuffPlugin", handler);
|
110 | }
|
111 | );
|
112 | }
|
113 | }
|
114 | module.exports = CommonJsStuffPlugin;
|