1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const InnerGraph = require("./optimize/InnerGraph");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class JavascriptMetaInfoPlugin {
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 | apply(compiler) {
|
20 | compiler.hooks.compilation.tap(
|
21 | "JavascriptMetaInfoPlugin",
|
22 | (compilation, { normalModuleFactory }) => {
|
23 | |
24 |
|
25 |
|
26 |
|
27 | const handler = parser => {
|
28 | parser.hooks.call.for("eval").tap("JavascriptMetaInfoPlugin", () => {
|
29 | parser.state.module.buildInfo.moduleConcatenationBailout = "eval()";
|
30 | parser.state.module.buildInfo.usingEval = true;
|
31 | const currentSymbol = InnerGraph.getTopLevelSymbol(parser.state);
|
32 | if (currentSymbol) {
|
33 | InnerGraph.addUsage(parser.state, null, currentSymbol);
|
34 | } else {
|
35 | InnerGraph.bailout(parser.state);
|
36 | }
|
37 | });
|
38 | parser.hooks.finish.tap("JavascriptMetaInfoPlugin", () => {
|
39 | let topLevelDeclarations =
|
40 | parser.state.module.buildInfo.topLevelDeclarations;
|
41 | if (topLevelDeclarations === undefined) {
|
42 | topLevelDeclarations =
|
43 | parser.state.module.buildInfo.topLevelDeclarations = new Set();
|
44 | }
|
45 | for (const name of parser.scope.definitions.asSet()) {
|
46 | const freeInfo = parser.getFreeInfoFromVariable(name);
|
47 | if (freeInfo === undefined) {
|
48 | topLevelDeclarations.add(name);
|
49 | }
|
50 | }
|
51 | });
|
52 | };
|
53 |
|
54 | normalModuleFactory.hooks.parser
|
55 | .for("javascript/auto")
|
56 | .tap("JavascriptMetaInfoPlugin", handler);
|
57 | normalModuleFactory.hooks.parser
|
58 | .for("javascript/dynamic")
|
59 | .tap("JavascriptMetaInfoPlugin", handler);
|
60 | normalModuleFactory.hooks.parser
|
61 | .for("javascript/esm")
|
62 | .tap("JavascriptMetaInfoPlugin", handler);
|
63 | }
|
64 | );
|
65 | }
|
66 | }
|
67 |
|
68 | module.exports = JavascriptMetaInfoPlugin;
|