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";
|
|
|
|
|
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 */
|
2023-05-22 10:12:44 +08:00
|
|
|
/** @typedef {import("./HarmonyModulesPlugin").HarmonyModulesPluginOptions} HarmonyModulesPluginOptions */
|
2023-05-22 05:47:28 +08:00
|
|
|
|
2017-01-11 17:51:58 +08:00
|
|
|
module.exports = class HarmonyDetectionParserPlugin {
|
2023-05-22 10:12:44 +08:00
|
|
|
/**
|
|
|
|
* @param {HarmonyModulesPluginOptions} options options
|
|
|
|
*/
|
2019-06-05 19:25:15 +08:00
|
|
|
constructor(options) {
|
|
|
|
const { topLevelAwait = false } = options || {};
|
|
|
|
this.topLevelAwait = topLevelAwait;
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.hooks.program.tap("HarmonyDetectionParserPlugin", 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(
|
|
|
|
statement =>
|
|
|
|
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
|
|
|
|
2019-05-23 02:11:16 +08:00
|
|
|
parser.hooks.topLevelAwait.tap("HarmonyDetectionParserPlugin", () => {
|
|
|
|
const module = parser.state.module;
|
2019-06-05 19:25:15 +08:00
|
|
|
if (!this.topLevelAwait) {
|
|
|
|
throw new Error(
|
2023-08-24 18:55:23 +08:00
|
|
|
"The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enable it)"
|
2019-06-05 19:25:15 +08:00
|
|
|
);
|
|
|
|
}
|
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;
|
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)
|
2018-02-25 09:00:20 +08:00
|
|
|
.tap("HarmonyDetectionParserPlugin", nullInHarmony);
|
|
|
|
parser.hooks.typeof
|
2019-04-13 00:29:41 +08:00
|
|
|
.for(identifier)
|
2018-02-25 09:00:20 +08:00
|
|
|
.tap("HarmonyDetectionParserPlugin", skipInHarmony);
|
|
|
|
parser.hooks.evaluate
|
2019-04-13 00:29:41 +08:00
|
|
|
.for(identifier)
|
2018-02-25 09:00:20 +08:00
|
|
|
.tap("HarmonyDetectionParserPlugin", nullInHarmony);
|
|
|
|
parser.hooks.expression
|
2019-04-13 00:29:41 +08:00
|
|
|
.for(identifier)
|
2018-02-25 09:00:20 +08:00
|
|
|
.tap("HarmonyDetectionParserPlugin", skipInHarmony);
|
|
|
|
parser.hooks.call
|
2019-04-13 00:29:41 +08:00
|
|
|
.for(identifier)
|
2018-02-25 09:00:20 +08:00
|
|
|
.tap("HarmonyDetectionParserPlugin", skipInHarmony);
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2017-01-11 17:51:58 +08:00
|
|
|
}
|
|
|
|
};
|