UNPKG

1.89 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 InnerGraph.bailout(parser.state);
32 });
33 parser.hooks.finish.tap("JavascriptMetaInfoPlugin", () => {
34 let topLevelDeclarations =
35 parser.state.module.buildInfo.topLevelDeclarations;
36 if (topLevelDeclarations === undefined) {
37 topLevelDeclarations = parser.state.module.buildInfo.topLevelDeclarations = new Set();
38 }
39 for (const name of parser.scope.definitions.asSet()) {
40 const freeInfo = parser.getFreeInfoFromVariable(name);
41 if (freeInfo === undefined) {
42 topLevelDeclarations.add(name);
43 }
44 }
45 });
46 };
47
48 normalModuleFactory.hooks.parser
49 .for("javascript/auto")
50 .tap("JavascriptMetaInfoPlugin", handler);
51 normalModuleFactory.hooks.parser
52 .for("javascript/dynamic")
53 .tap("JavascriptMetaInfoPlugin", handler);
54 normalModuleFactory.hooks.parser
55 .for("javascript/esm")
56 .tap("JavascriptMetaInfoPlugin", handler);
57 }
58 );
59 }
60}
61
62module.exports = JavascriptMetaInfoPlugin;