1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const EnvironmentNotSupportAsyncWarning = require("../EnvironmentNotSupportAsyncWarning");
|
---|
9 | const { JAVASCRIPT_MODULE_TYPE_ESM } = require("../ModuleTypeConstants");
|
---|
10 | const DynamicExports = require("./DynamicExports");
|
---|
11 | const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
|
---|
12 | const HarmonyExports = require("./HarmonyExports");
|
---|
13 |
|
---|
14 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
15 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
---|
16 | /** @typedef {import("./HarmonyModulesPlugin").HarmonyModulesPluginOptions} HarmonyModulesPluginOptions */
|
---|
17 |
|
---|
18 | module.exports = class HarmonyDetectionParserPlugin {
|
---|
19 | /**
|
---|
20 | * @param {HarmonyModulesPluginOptions} options options
|
---|
21 | */
|
---|
22 | constructor(options) {
|
---|
23 | const { topLevelAwait = false } = options || {};
|
---|
24 | this.topLevelAwait = topLevelAwait;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @param {JavascriptParser} parser the parser
|
---|
29 | * @returns {void}
|
---|
30 | */
|
---|
31 | apply(parser) {
|
---|
32 | parser.hooks.program.tap("HarmonyDetectionParserPlugin", ast => {
|
---|
33 | const isStrictHarmony =
|
---|
34 | parser.state.module.type === JAVASCRIPT_MODULE_TYPE_ESM;
|
---|
35 | const isHarmony =
|
---|
36 | isStrictHarmony ||
|
---|
37 | ast.body.some(
|
---|
38 | statement =>
|
---|
39 | statement.type === "ImportDeclaration" ||
|
---|
40 | statement.type === "ExportDefaultDeclaration" ||
|
---|
41 | statement.type === "ExportNamedDeclaration" ||
|
---|
42 | statement.type === "ExportAllDeclaration"
|
---|
43 | );
|
---|
44 | if (isHarmony) {
|
---|
45 | const module = parser.state.module;
|
---|
46 | const compatDep = new HarmonyCompatibilityDependency();
|
---|
47 | compatDep.loc = {
|
---|
48 | start: {
|
---|
49 | line: -1,
|
---|
50 | column: 0
|
---|
51 | },
|
---|
52 | end: {
|
---|
53 | line: -1,
|
---|
54 | column: 0
|
---|
55 | },
|
---|
56 | index: -3
|
---|
57 | };
|
---|
58 | module.addPresentationalDependency(compatDep);
|
---|
59 | DynamicExports.bailout(parser.state);
|
---|
60 | HarmonyExports.enable(parser.state, isStrictHarmony);
|
---|
61 | parser.scope.isStrict = true;
|
---|
62 | }
|
---|
63 | });
|
---|
64 |
|
---|
65 | parser.hooks.topLevelAwait.tap("HarmonyDetectionParserPlugin", () => {
|
---|
66 | const module = parser.state.module;
|
---|
67 | if (!this.topLevelAwait) {
|
---|
68 | throw new Error(
|
---|
69 | "The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enable it)"
|
---|
70 | );
|
---|
71 | }
|
---|
72 | if (!HarmonyExports.isEnabled(parser.state)) {
|
---|
73 | throw new Error(
|
---|
74 | "Top-level-await is only supported in EcmaScript Modules"
|
---|
75 | );
|
---|
76 | }
|
---|
77 | /** @type {BuildMeta} */
|
---|
78 | (module.buildMeta).async = true;
|
---|
79 | EnvironmentNotSupportAsyncWarning.check(
|
---|
80 | module,
|
---|
81 | parser.state.compilation.runtimeTemplate,
|
---|
82 | "topLevelAwait"
|
---|
83 | );
|
---|
84 | });
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @returns {boolean | undefined} true if in harmony
|
---|
88 | */
|
---|
89 | const skipInHarmony = () => {
|
---|
90 | if (HarmonyExports.isEnabled(parser.state)) {
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 | };
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @returns {null | undefined} null if in harmony
|
---|
97 | */
|
---|
98 | const nullInHarmony = () => {
|
---|
99 | if (HarmonyExports.isEnabled(parser.state)) {
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 | };
|
---|
103 |
|
---|
104 | const nonHarmonyIdentifiers = ["define", "exports"];
|
---|
105 | for (const identifier of nonHarmonyIdentifiers) {
|
---|
106 | parser.hooks.evaluateTypeof
|
---|
107 | .for(identifier)
|
---|
108 | .tap("HarmonyDetectionParserPlugin", nullInHarmony);
|
---|
109 | parser.hooks.typeof
|
---|
110 | .for(identifier)
|
---|
111 | .tap("HarmonyDetectionParserPlugin", skipInHarmony);
|
---|
112 | parser.hooks.evaluate
|
---|
113 | .for(identifier)
|
---|
114 | .tap("HarmonyDetectionParserPlugin", nullInHarmony);
|
---|
115 | parser.hooks.expression
|
---|
116 | .for(identifier)
|
---|
117 | .tap("HarmonyDetectionParserPlugin", skipInHarmony);
|
---|
118 | parser.hooks.call
|
---|
119 | .for(identifier)
|
---|
120 | .tap("HarmonyDetectionParserPlugin", skipInHarmony);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | };
|
---|