UNPKG

2.08 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Sergey Melyukov @smelukov
4*/
5
6"use strict";
7
8const InnerGraph = require("./optimize/InnerGraph");
9
10/** @typedef {import("./Compiler")} Compiler */
11/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
12
13class JavascriptMetaInfoPlugin {
14 /**
15 * Apply the plugin
16 * @param {Compiler} compiler the compiler instance
17 * @returns {void}
18 */
19 apply(compiler) {
20 compiler.hooks.compilation.tap(
21 "JavascriptMetaInfoPlugin",
22 (compilation, { normalModuleFactory }) => {
23 /**
24 * @param {JavascriptParser} parser the parser
25 * @returns {void}
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
68module.exports = JavascriptMetaInfoPlugin;