2017-01-11 17:51:58 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-11 17:51:58 +08:00
|
|
|
"use strict";
|
|
|
|
|
2024-01-14 18:11:12 +08:00
|
|
|
const EnvironmentNotSupportAsyncWarning = require("../EnvironmentNotSupportAsyncWarning");
|
2023-04-01 01:56:32 +08:00
|
|
|
const { JAVASCRIPT_MODULE_TYPE_ESM } = require("../ModuleTypeConstants");
|
2019-12-06 17:19:41 +08:00
|
|
|
const DynamicExports = require("./DynamicExports");
|
2017-01-20 02:43:42 +08:00
|
|
|
const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
|
2019-12-06 17:19:41 +08:00
|
|
|
const HarmonyExports = require("./HarmonyExports");
|
2017-01-11 17:51:58 +08:00
|
|
|
|
2023-06-17 02:24:34 +08:00
|
|
|
/** @typedef {import("../Module").BuildMeta} BuildMeta */
|
2023-05-22 05:47:28 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
|
|
|
|
2025-04-23 20:03:37 +08:00
|
|
|
const PLUGIN_NAME = "HarmonyDetectionParserPlugin";
|
|
|
|
|
2017-01-11 17:51:58 +08:00
|
|
|
module.exports = class HarmonyDetectionParserPlugin {
|
2023-05-22 05:47:28 +08:00
|
|
|
/**
|
|
|
|
* @param {JavascriptParser} parser the parser
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-11 17:51:58 +08:00
|
|
|
apply(parser) {
|
2025-07-17 00:13:14 +08:00
|
|
|
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
|
2023-04-01 01:56:32 +08:00
|
|
|
const isStrictHarmony =
|
|
|
|
parser.state.module.type === JAVASCRIPT_MODULE_TYPE_ESM;
|
2018-02-25 09:00:20 +08:00
|
|
|
const isHarmony =
|
|
|
|
isStrictHarmony ||
|
2019-01-04 21:53:08 +08:00
|
|
|
ast.body.some(
|
2025-07-17 00:13:14 +08:00
|
|
|
(statement) =>
|
2019-01-04 21:53:08 +08:00
|
|
|
statement.type === "ImportDeclaration" ||
|
|
|
|
statement.type === "ExportDefaultDeclaration" ||
|
|
|
|
statement.type === "ExportNamedDeclaration" ||
|
|
|
|
statement.type === "ExportAllDeclaration"
|
2019-01-04 03:39:52 +08:00
|
|
|
);
|
2018-02-25 09:00:20 +08:00
|
|
|
if (isHarmony) {
|
2017-02-05 07:15:47 +08:00
|
|
|
const module = parser.state.module;
|
2018-08-07 20:20:53 +08:00
|
|
|
const compatDep = new HarmonyCompatibilityDependency();
|
2017-08-08 15:32:43 +08:00
|
|
|
compatDep.loc = {
|
|
|
|
start: {
|
|
|
|
line: -1,
|
|
|
|
column: 0
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line: -1,
|
|
|
|
column: 0
|
|
|
|
},
|
|
|
|
index: -3
|
|
|
|
};
|
2019-10-30 13:40:40 +08:00
|
|
|
module.addPresentationalDependency(compatDep);
|
2019-12-07 08:04:38 +08:00
|
|
|
DynamicExports.bailout(parser.state);
|
|
|
|
HarmonyExports.enable(parser.state, isStrictHarmony);
|
2018-01-09 17:35:08 +08:00
|
|
|
parser.scope.isStrict = true;
|
2017-01-11 17:51:58 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
|
2025-04-23 20:03:37 +08:00
|
|
|
parser.hooks.topLevelAwait.tap(PLUGIN_NAME, () => {
|
2019-05-23 02:11:16 +08:00
|
|
|
const module = parser.state.module;
|
2019-12-07 08:04:38 +08:00
|
|
|
if (!HarmonyExports.isEnabled(parser.state)) {
|
2019-05-23 02:11:16 +08:00
|
|
|
throw new Error(
|
|
|
|
"Top-level-await is only supported in EcmaScript Modules"
|
|
|
|
);
|
|
|
|
}
|
2023-06-17 02:24:34 +08:00
|
|
|
/** @type {BuildMeta} */
|
|
|
|
(module.buildMeta).async = true;
|
2024-01-16 10:25:30 +08:00
|
|
|
EnvironmentNotSupportAsyncWarning.check(
|
|
|
|
module,
|
|
|
|
parser.state.compilation.runtimeTemplate,
|
|
|
|
"topLevelAwait"
|
|
|
|
);
|
2019-05-23 02:11:16 +08:00
|
|
|
});
|
|
|
|
|
2023-05-22 10:12:44 +08:00
|
|
|
/**
|
|
|
|
* @returns {boolean | undefined} true if in harmony
|
|
|
|
*/
|
2017-11-08 18:32:05 +08:00
|
|
|
const skipInHarmony = () => {
|
2019-12-07 08:04:38 +08:00
|
|
|
if (HarmonyExports.isEnabled(parser.state)) {
|
2017-11-08 18:32:05 +08:00
|
|
|
return true;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
|
|
|
|
2023-05-22 10:12:44 +08:00
|
|
|
/**
|
|
|
|
* @returns {null | undefined} null if in harmony
|
|
|
|
*/
|
2017-11-08 18:32:05 +08:00
|
|
|
const nullInHarmony = () => {
|
2019-12-07 08:04:38 +08:00
|
|
|
if (HarmonyExports.isEnabled(parser.state)) {
|
2017-11-08 18:32:05 +08:00
|
|
|
return null;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
|
|
|
|
2017-11-15 16:28:45 +08:00
|
|
|
const nonHarmonyIdentifiers = ["define", "exports"];
|
2019-04-13 00:29:41 +08:00
|
|
|
for (const identifier of nonHarmonyIdentifiers) {
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.evaluateTypeof
|
2019-04-13 00:29:41 +08:00
|
|
|
.for(identifier)
|
2025-04-23 20:03:37 +08:00
|
|
|
.tap(PLUGIN_NAME, nullInHarmony);
|
|
|
|
parser.hooks.typeof.for(identifier).tap(PLUGIN_NAME, skipInHarmony);
|
|
|
|
parser.hooks.evaluate.for(identifier).tap(PLUGIN_NAME, nullInHarmony);
|
|
|
|
parser.hooks.expression.for(identifier).tap(PLUGIN_NAME, skipInHarmony);
|
|
|
|
parser.hooks.call.for(identifier).tap(PLUGIN_NAME, skipInHarmony);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-01-11 17:51:58 +08:00
|
|
|
}
|
|
|
|
};
|