| 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 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 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 | * Handles the hook callback for this code path.
|
|---|
| 33 | * @param {JavascriptParser} parser the parser
|
|---|
| 34 | * @returns {void}
|
|---|
| 35 | */
|
|---|
| 36 | const handler = (parser) => {
|
|---|
| 37 | parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
|
|---|
| 38 | const buildInfo =
|
|---|
| 39 | /** @type {BuildInfo} */
|
|---|
| 40 | (parser.state.module.buildInfo);
|
|---|
| 41 | buildInfo.moduleConcatenationBailout = "eval()";
|
|---|
| 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 | if (parser.isVariableDefined(name)) {
|
|---|
| 59 | topLevelDeclarations.add(name);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | });
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | normalModuleFactory.hooks.parser
|
|---|
| 66 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|---|
| 67 | .tap(PLUGIN_NAME, handler);
|
|---|
| 68 | normalModuleFactory.hooks.parser
|
|---|
| 69 | .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|---|
| 70 | .tap(PLUGIN_NAME, handler);
|
|---|
| 71 | normalModuleFactory.hooks.parser
|
|---|
| 72 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
|---|
| 73 | .tap(PLUGIN_NAME, handler);
|
|---|
| 74 | }
|
|---|
| 75 | );
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | module.exports = JavascriptMetaInfoPlugin;
|
|---|