[6a3a178] | 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 InnerGraph = require("./optimize/InnerGraph");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 11 | /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
---|
| 12 |
|
---|
| 13 | class 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 =
|
---|
| 38 | parser.state.module.buildInfo.topLevelDeclarations = new Set();
|
---|
| 39 | }
|
---|
| 40 | for (const name of parser.scope.definitions.asSet()) {
|
---|
| 41 | const freeInfo = parser.getFreeInfoFromVariable(name);
|
---|
| 42 | if (freeInfo === undefined) {
|
---|
| 43 | topLevelDeclarations.add(name);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | });
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | normalModuleFactory.hooks.parser
|
---|
| 50 | .for("javascript/auto")
|
---|
| 51 | .tap("JavascriptMetaInfoPlugin", handler);
|
---|
| 52 | normalModuleFactory.hooks.parser
|
---|
| 53 | .for("javascript/dynamic")
|
---|
| 54 | .tap("JavascriptMetaInfoPlugin", handler);
|
---|
| 55 | normalModuleFactory.hooks.parser
|
---|
| 56 | .for("javascript/esm")
|
---|
| 57 | .tap("JavascriptMetaInfoPlugin", handler);
|
---|
| 58 | }
|
---|
| 59 | );
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | module.exports = JavascriptMetaInfoPlugin;
|
---|