1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Sergey Melyukov @smelukov
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const {
|
---|
9 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
10 | JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
---|
11 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
12 | } = require("./ModuleTypeConstants");
|
---|
13 | const InnerGraph = require("./optimize/InnerGraph");
|
---|
14 |
|
---|
15 | /** @typedef {import("./Compiler")} Compiler */
|
---|
16 | /** @typedef {import("./Module").BuildInfo} BuildInfo */
|
---|
17 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
18 |
|
---|
19 | const PLUGIN_NAME = "JavascriptMetaInfoPlugin";
|
---|
20 |
|
---|
21 | class JavascriptMetaInfoPlugin {
|
---|
22 | /**
|
---|
23 | * Apply the plugin
|
---|
24 | * @param {Compiler} compiler the compiler instance
|
---|
25 | * @returns {void}
|
---|
26 | */
|
---|
27 | apply(compiler) {
|
---|
28 | compiler.hooks.compilation.tap(
|
---|
29 | PLUGIN_NAME,
|
---|
30 | (compilation, { normalModuleFactory }) => {
|
---|
31 | /**
|
---|
32 | * @param {JavascriptParser} parser the parser
|
---|
33 | * @returns {void}
|
---|
34 | */
|
---|
35 | const handler = parser => {
|
---|
36 | parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
|
---|
37 | const buildInfo =
|
---|
38 | /** @type {BuildInfo} */
|
---|
39 | (parser.state.module.buildInfo);
|
---|
40 | buildInfo.moduleConcatenationBailout = "eval()";
|
---|
41 | buildInfo.usingEval = true;
|
---|
42 | const currentSymbol = InnerGraph.getTopLevelSymbol(parser.state);
|
---|
43 | if (currentSymbol) {
|
---|
44 | InnerGraph.addUsage(parser.state, null, currentSymbol);
|
---|
45 | } else {
|
---|
46 | InnerGraph.bailout(parser.state);
|
---|
47 | }
|
---|
48 | });
|
---|
49 | parser.hooks.finish.tap(PLUGIN_NAME, () => {
|
---|
50 | const buildInfo =
|
---|
51 | /** @type {BuildInfo} */
|
---|
52 | (parser.state.module.buildInfo);
|
---|
53 | let topLevelDeclarations = buildInfo.topLevelDeclarations;
|
---|
54 | if (topLevelDeclarations === undefined) {
|
---|
55 | topLevelDeclarations = buildInfo.topLevelDeclarations = new Set();
|
---|
56 | }
|
---|
57 | for (const name of parser.scope.definitions.asSet()) {
|
---|
58 | const freeInfo = parser.getFreeInfoFromVariable(name);
|
---|
59 | if (freeInfo === undefined) {
|
---|
60 | topLevelDeclarations.add(name);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | });
|
---|
64 | };
|
---|
65 |
|
---|
66 | normalModuleFactory.hooks.parser
|
---|
67 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
68 | .tap(PLUGIN_NAME, handler);
|
---|
69 | normalModuleFactory.hooks.parser
|
---|
70 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
---|
71 | .tap(PLUGIN_NAME, handler);
|
---|
72 | normalModuleFactory.hooks.parser
|
---|
73 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
74 | .tap(PLUGIN_NAME, handler);
|
---|
75 | }
|
---|
76 | );
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | module.exports = JavascriptMetaInfoPlugin;
|
---|