@jsenv/core 24.6.2 → 25.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/browser_runtime/asset-manifest.json +2 -1
  2. package/dist/browser_runtime/{browser_runtime-c7288751.js → browser_runtime_a8097085.js} +37 -27
  3. package/dist/browser_runtime/{browser_runtime-c7288751.js.map → browser_runtime_a8097085.js.map} +3 -3
  4. package/dist/build_manifest.js +6 -6
  5. package/dist/compile_proxy/asset-manifest.json +2 -1
  6. package/dist/compile_proxy/{compile_proxy-a3969633.html → compile_proxy_e16d7de8.html} +39 -34
  7. package/dist/compile_proxy/{compile_proxy.html__inline__20-9c92c170.js.map → compile_proxy_e3b0c442_9e168143.js.map} +3 -2
  8. package/dist/event_source_client/asset-manifest.json +2 -1
  9. package/dist/event_source_client/{event_source_client-9f14c8b9.js → event_source_client_620fbc2c.js} +1 -1
  10. package/dist/event_source_client/{event_source_client-9f14c8b9.js.map → event_source_client_620fbc2c.js.map} +1 -1
  11. package/dist/redirector/asset-manifest.json +2 -1
  12. package/dist/redirector/{redirector-a6b8d640.html → redirector_2e0c8abe.html} +39 -34
  13. package/dist/redirector/{redirector.html__inline__15-58430672.js.map → redirector_e3b0c442_3a34a156.js.map} +3 -2
  14. package/dist/toolbar/asset-manifest.json +11 -10
  15. package/dist/toolbar/assets/{compilation.css-209d68b4.map → compilation.css_e37c747b.map} +1 -1
  16. package/dist/toolbar/assets/{eventsource.css-38cd0a36.map → eventsource.css_c0c71e7b.map} +1 -1
  17. package/dist/toolbar/assets/{execution.css-0ebe522f.map → execution.css_f3377c10.map} +1 -1
  18. package/dist/toolbar/assets/{focus.css-3f9c156d.map → focus.css_896f3e45.map} +1 -1
  19. package/dist/toolbar/assets/{light-theme.css-78b19a80.map → light-theme.css_72a60fa3.map} +1 -1
  20. package/dist/toolbar/assets/{overflow-menu.css-d9688a1c.map → overflow-menu.css_2859d519.map} +1 -1
  21. package/dist/toolbar/assets/{settings.css-2b81b245.map → settings.css_61548139.map} +1 -1
  22. package/dist/toolbar/assets/{toolbar.main.css-846920e9.map → toolbar.main.css_269d7ce2.map} +9 -9
  23. package/dist/toolbar/assets/{tooltip.css-03395ee6.map → tooltip.css_a94a8bdd.map} +1 -1
  24. package/dist/toolbar/{toolbar.main-7aa01366.js.map → toolbar.main_a5ef2c60.js.map} +3 -2
  25. package/dist/toolbar/{toolbar-84985f43.html → toolbar_412abb83.html} +87 -64
  26. package/dist/toolbar_injector/asset-manifest.json +3 -2
  27. package/dist/toolbar_injector/assets/{jsenv-logo-188b9ca6.svg → jsenv-logo_188b9ca6.svg} +0 -0
  28. package/dist/toolbar_injector/{toolbar_injector-8edcae04.js → toolbar_injector_4f9c19e5.js} +17 -17
  29. package/dist/toolbar_injector/{toolbar_injector-8edcae04.js.map → toolbar_injector_4f9c19e5.js.map} +4 -4
  30. package/package.json +5 -4
  31. package/readme.md +12 -22
  32. package/src/buildProject.js +21 -21
  33. package/src/dev_server.js +6 -2
  34. package/src/internal/building/buildUsingRollup.js +24 -18
  35. package/src/internal/building/build_logs.js +2 -2
  36. package/src/internal/building/build_stats.js +11 -1
  37. package/src/internal/building/html/parseHtmlRessource.js +2 -26
  38. package/src/internal/building/js/parseJsRessource.js +3 -2
  39. package/src/internal/building/json_module.js +11 -0
  40. package/src/internal/building/parseRessource.js +1 -1
  41. package/src/internal/building/ressource_builder.js +216 -215
  42. package/src/internal/building/rollup_plugin_jsenv.js +522 -354
  43. package/src/internal/building/url_loader.js +8 -142
  44. package/src/internal/building/url_versioning.js +226 -0
  45. package/src/internal/compiling/compileHtml.js +8 -1
  46. package/src/internal/compiling/createCompiledFileService.js +7 -7
  47. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +9 -55
  48. package/src/internal/compiling/js-compilation-service/transformJs.js +8 -5
  49. package/src/internal/compiling/jsenvCompilerForHtml.js +4 -4
  50. package/src/internal/compiling/jsenvCompilerForJavaScript.js +2 -2
  51. package/src/internal/compiling/startCompileServer.js +2 -2
  52. package/src/jsenvServiceWorkerFinalizer.js +7 -8
  53. package/src/internal/building/asset_url_versioning.js +0 -50
  54. package/src/internal/building/rollup_build_sourcemap.js +0 -54
  55. package/src/internal/building/url-versioning.js +0 -96
  56. package/src/internal/compiling/js-compilation-service/findAsyncPluginNameInBabelPluginMap.js +0 -9
  57. package/src/internal/renderNamePattern.js +0 -6
@@ -1,17 +1,12 @@
1
- import { resolveUrl, urlToFilename } from "@jsenv/filesystem"
2
-
3
- import { transformJs } from "@jsenv/core/src/internal/compiling/js-compilation-service/transformJs.js"
4
- import { convertCssTextToJavascriptModule } from "@jsenv/core/src/internal/building/css_module.js"
1
+ import { convertJsonTextToJavascriptModule } from "@jsenv/core/src/internal/building/json_module.js"
5
2
  import { getJavaScriptSourceMappingUrl } from "@jsenv/core/src/internal/sourceMappingURLUtils.js"
3
+
6
4
  import { loadSourcemap } from "./sourcemap_loader.js"
7
5
 
8
6
  export const createUrlLoader = ({
9
- projectDirectoryUrl,
10
- buildDirectoryUrl,
11
- babelPluginMap,
7
+ urlCustomLoaders,
12
8
  allowJson,
13
9
  urlImporterMap,
14
- inlineModuleScripts,
15
10
 
16
11
  asServerUrl,
17
12
  asProjectUrl,
@@ -21,112 +16,13 @@ export const createUrlLoader = ({
21
16
  }) => {
22
17
  const urlResponseBodyMap = {}
23
18
 
24
- const loadUrl = async (rollupUrl, { signal, logger, ressourceBuilder }) => {
19
+ const loadUrl = async (rollupUrl, { signal, logger }) => {
25
20
  let url = asServerUrl(rollupUrl)
26
- const { importType, urlWithoutImportType } = extractImportTypeFromUrl(url)
27
- // importing CSS from JS with import assertions
28
- if (importType === "css") {
29
- const importer = urlImporterMap[url]
30
- const cssReference =
31
- await ressourceBuilder.createReferenceFoundInJsModule({
32
- referenceLabel: "css import assertion",
33
- // If all references to a ressource are only import assertions
34
- // the file referenced do not need to be written on filesystem
35
- // as it was converted to a js file
36
- // We pass "isImportAssertion: true" for this purpose
37
- isImportAssertion: true,
38
- jsUrl: importer.url,
39
- jsLine: importer.line,
40
- jsColumn: importer.column,
41
- ressourceSpecifier: urlWithoutImportType,
42
- contentTypeExpected: "text/css",
43
- })
44
- await cssReference.ressource.getReadyPromise()
45
- const cssBuildUrl = resolveUrl(
46
- cssReference.ressource.buildRelativeUrl,
47
- buildDirectoryUrl,
48
- )
49
- const jsBuildUrl = resolveUrl(
50
- urlToFilename(importer.url),
51
- buildDirectoryUrl,
52
- )
53
- let code = String(cssReference.ressource.bufferAfterBuild)
54
- let map
55
- const sourcemapReference = cssReference.ressource.dependencies.find(
56
- (dependency) => {
57
- return dependency.ressource.isSourcemap
58
- },
59
- )
60
- if (sourcemapReference) {
61
- // because css is ready, it's sourcemap is also ready
62
- // we can read directly sourcemapReference.ressource.bufferAfterBuild
63
- map = JSON.parse(sourcemapReference.ressource.bufferAfterBuild)
64
- }
65
-
66
- const jsModuleConversionResult = await convertCssTextToJavascriptModule({
67
- cssUrl: cssBuildUrl,
68
- jsUrl: jsBuildUrl,
69
- code,
70
- map,
71
- })
72
- code = jsModuleConversionResult.code
73
- map = jsModuleConversionResult.map
74
-
75
- return {
76
- url,
77
- code,
78
- map,
79
- }
80
- }
81
- // importing json from JS with import assertion
82
- if (importType === "json") {
83
- const importer = urlImporterMap[url]
84
- const jsonReference =
85
- await ressourceBuilder.createReferenceFoundInJsModule({
86
- referenceLabel: "json import assertion",
87
- // If all references to a ressource are only import assertions
88
- // the file referenced do not need to be written on filesystem
89
- // as it was converted to a js file
90
- // We pass "isImportAssertion: true" for this purpose
91
- isImportAssertion: true,
92
- jsUrl: importer.url,
93
- jsLine: importer.line,
94
- jsColumn: importer.column,
95
- ressourceSpecifier: asServerUrl(urlWithoutImportType),
96
- contentTypeExpected: "application/json",
97
- })
98
- await jsonReference.ressource.getReadyPromise()
99
- let code = String(jsonReference.ressource.bufferAfterBuild)
100
- let map
101
-
102
- const jsModuleConversionResult = await convertJsonTextToJavascriptModule({
103
- code,
104
- map,
105
- })
106
- code = jsModuleConversionResult.code
107
- map = jsModuleConversionResult.map
108
21
 
109
- return {
110
- url,
111
- code,
112
- }
113
- }
114
-
115
- if (url in inlineModuleScripts) {
116
- const transformResult = await transformJs({
117
- code: String(inlineModuleScripts[url].bufferBeforeBuild),
118
- url: asOriginalUrl(url), // transformJs expect a file:// url
119
- projectDirectoryUrl,
120
- babelPluginMap,
121
- // moduleOutFormat: format // we are compiling for rollup output must be "esmodule"
122
- })
123
- let code = transformResult.code
124
- let map = transformResult.map
125
- return {
126
- url,
127
- code,
128
- map,
129
- }
22
+ const customLoader = urlCustomLoaders[url]
23
+ if (customLoader) {
24
+ const result = await customLoader()
25
+ return result
130
26
  }
131
27
 
132
28
  const response = await urlFetcher.fetchUrl(url, {
@@ -233,33 +129,3 @@ export const createUrlLoader = ({
233
129
  getUrlResponseBodyMap: () => urlResponseBodyMap,
234
130
  }
235
131
  }
236
-
237
- const extractImportTypeFromUrl = (url) => {
238
- const urlObject = new URL(url)
239
- const { search } = urlObject
240
- const searchParams = new URLSearchParams(search)
241
-
242
- const importType = searchParams.get("import_type")
243
- if (!importType) {
244
- return {}
245
- }
246
-
247
- searchParams.delete("import_type")
248
- urlObject.search = String(searchParams)
249
- return {
250
- importType,
251
- urlWithoutImportType: urlObject.href,
252
- }
253
- }
254
-
255
- const convertJsonTextToJavascriptModule = ({ code }) => {
256
- // here we could do the following
257
- // return export default jsonText
258
- // This would return valid js, that would be minified later
259
- // however we will prefer using JSON.parse because it's faster
260
- // for js engine to parse JSON than JS
261
-
262
- return {
263
- code: `export default JSON.parse(${JSON.stringify(code.trim())})`,
264
- }
265
- }
@@ -0,0 +1,226 @@
1
+ import { createHash } from "crypto"
2
+ import {
3
+ urlToParentUrl,
4
+ urlToBasename,
5
+ urlToExtension,
6
+ } from "@jsenv/filesystem"
7
+
8
+ export const createUrlVersioner = ({
9
+ entryPointUrls,
10
+ workerUrls,
11
+ classicWorkerUrls,
12
+ serviceWorkerUrls,
13
+ classicServiceWorkerUrls,
14
+ asOriginalUrl,
15
+ lineBreakNormalization,
16
+ }) => {
17
+ const computeBuildRelativeUrl = (ressource) => {
18
+ const pattern = getFilenamePattern({
19
+ ressource,
20
+ entryPointUrls,
21
+ workerUrls,
22
+ classicWorkerUrls,
23
+ serviceWorkerUrls,
24
+ classicServiceWorkerUrls,
25
+ asOriginalUrl,
26
+ precomputeBuildRelativeUrl,
27
+ })
28
+ const buildRelativeUrl = generateBuildRelativeUrl(
29
+ ressource.url,
30
+ ressource.bufferAfterBuild,
31
+ {
32
+ pattern,
33
+ contentType: ressource.contentType,
34
+ lineBreakNormalization,
35
+ },
36
+ )
37
+ return buildRelativeUrl
38
+ }
39
+
40
+ const precomputeBuildRelativeUrl = (ressource, bufferAfterBuild = "") => {
41
+ if (ressource.buildRelativeUrl) {
42
+ return ressource.buildRelativeUrl
43
+ }
44
+ if (ressource.precomputedBuildRelativeUrl) {
45
+ return ressource.precomputedBuildRelativeUrl
46
+ }
47
+
48
+ ressource.bufferAfterBuild = bufferAfterBuild
49
+ const precomputedBuildRelativeUrl = computeBuildRelativeUrl(ressource)
50
+ ressource.bufferAfterBuild = undefined
51
+ ressource.precomputedBuildRelativeUrl = precomputedBuildRelativeUrl
52
+ return precomputedBuildRelativeUrl
53
+ }
54
+
55
+ return {
56
+ computeBuildRelativeUrl,
57
+ precomputeBuildRelativeUrl,
58
+ }
59
+ }
60
+
61
+ const getFilenamePattern = ({
62
+ ressource,
63
+ entryPointUrls,
64
+ workerUrls,
65
+ classicWorkerUrls,
66
+ serviceWorkerUrls,
67
+ classicServiceWorkerUrls,
68
+ asOriginalUrl,
69
+ precomputeBuildRelativeUrl,
70
+ }) => {
71
+ if (ressource.isEntryPoint) {
72
+ const originalUrl = asOriginalUrl(ressource.url)
73
+ const entryPointBuildRelativeUrl = entryPointUrls[originalUrl]
74
+ return entryPointBuildRelativeUrl
75
+ }
76
+
77
+ // service worker:
78
+ // - MUST be at the root (same level than the HTML file)
79
+ // otherwise it might be registered for the scope "/assets/" instead of "/"
80
+ // - MUST not be versioned
81
+ if (ressource.isServiceWorker) {
82
+ const originalUrl = asOriginalUrl(ressource.url)
83
+ const serviceWorkerBuildRelativeUrl = ressource.isJsModule
84
+ ? serviceWorkerUrls[originalUrl]
85
+ : classicServiceWorkerUrls[originalUrl]
86
+ // we could/should log a warning when service worker is not at the root
87
+ // we should also disable versioning for this file
88
+ // of the build directory
89
+ return serviceWorkerBuildRelativeUrl
90
+ }
91
+
92
+ // it's a module worker
93
+ if (ressource.isWorker) {
94
+ const originalUrl = asOriginalUrl(ressource.url)
95
+ const workerBuildRelativeUrl = ressource.isJsModule
96
+ ? workerUrls[originalUrl]
97
+ : classicWorkerUrls[originalUrl]
98
+ return workerBuildRelativeUrl
99
+ }
100
+
101
+ // inline ressource inherits location
102
+ if (ressource.isInline) {
103
+ // inherit parent directory location because it's an inline file
104
+ const importerBuildRelativeUrl = precomputeBuildRelativeUrl(
105
+ ressource.importer,
106
+ )
107
+ const name = urlToBasename(`file://${importerBuildRelativeUrl}`)
108
+ return `${name}_[hash][extname]`
109
+ }
110
+
111
+ // importmap.
112
+ // we want to force the fileName for the importmap
113
+ // so that we don't have to rewrite its content
114
+ // the goal is to put the importmap at the same relative path
115
+ // than in the project
116
+ if (ressource.contentType === "application/importmap+json") {
117
+ const importmapRessourceUrl = ressource.url
118
+ const name = urlToBasename(importmapRessourceUrl)
119
+ return `${name}_[hash][extname]`
120
+ }
121
+
122
+ if (ressource.isJsModule) {
123
+ // it's a js module
124
+ return "[name]_[hash][extname]"
125
+ }
126
+
127
+ return ressource.urlVersioningDisabled
128
+ ? "assets/[name][extname]"
129
+ : "assets/[name]_[hash][extname]"
130
+ }
131
+
132
+ const generateBuildRelativeUrl = (
133
+ fileUrl,
134
+ fileContent,
135
+ {
136
+ name = urlToBasename(fileUrl),
137
+ pattern = "[name]-[hash][extname]",
138
+ contentType = "application/octet-stream",
139
+ lineBreakNormalization = false,
140
+ } = {},
141
+ ) => {
142
+ pattern = typeof pattern === "function" ? pattern() : pattern
143
+ if (pattern.startsWith("./")) pattern = pattern.slice(2)
144
+ const buildRelativeUrl = renderFileNamePattern(pattern, {
145
+ dirname: () => urlToParentUrl(fileUrl),
146
+ name: () => name,
147
+ hash: () =>
148
+ generateContentHash(fileContent, {
149
+ contentType,
150
+ lineBreakNormalization,
151
+ }),
152
+ extname: () => urlToExtension(fileUrl),
153
+ })
154
+ return buildRelativeUrl
155
+ }
156
+
157
+ const renderFileNamePattern = (pattern, replacements) => {
158
+ return pattern.replace(/\[(\w+)\]/g, (_match, type) => {
159
+ const replacement = replacements[type]()
160
+ return replacement
161
+ })
162
+ }
163
+
164
+ // https://github.com/rollup/rollup/blob/19e50af3099c2f627451a45a84e2fa90d20246d5/src/utils/FileEmitter.ts#L47
165
+ export const generateContentHash = (
166
+ stringOrBuffer,
167
+ {
168
+ contentType = "application/octet-stream",
169
+ lineBreakNormalization = false,
170
+ } = {},
171
+ ) => {
172
+ const hash = createHash("sha256")
173
+ hash.update(
174
+ lineBreakNormalization && contentTypeIsTextual(contentType)
175
+ ? normalizeLineBreaks(stringOrBuffer)
176
+ : stringOrBuffer,
177
+ )
178
+ return hash.digest("hex").slice(0, 8)
179
+ }
180
+
181
+ const contentTypeIsTextual = (contentType) => {
182
+ if (contentType.startsWith("text/")) {
183
+ return true
184
+ }
185
+ // catch things like application/manifest+json, application/importmap+json
186
+ if (/^application\/\w+\+json$/.test(contentType)) {
187
+ return true
188
+ }
189
+ if (CONTENT_TYPE_AS_TEXT.includes(contentType)) {
190
+ return true
191
+ }
192
+ return false
193
+ }
194
+
195
+ const CONTENT_TYPE_AS_TEXT = [
196
+ "application/javascript",
197
+ "application/json",
198
+ "image/svg+xml",
199
+ ]
200
+
201
+ const normalizeLineBreaks = (stringOrBuffer) => {
202
+ if (typeof stringOrBuffer === "string") {
203
+ const stringWithLinuxBreaks = stringOrBuffer.replace(/\r\n/g, "\n")
204
+ return stringWithLinuxBreaks
205
+ }
206
+ return normalizeLineBreaksForBuffer(stringOrBuffer)
207
+ }
208
+
209
+ // https://github.com/nodejs/help/issues/1738#issuecomment-458460503
210
+ const normalizeLineBreaksForBuffer = (buffer) => {
211
+ const int32Array = new Int32Array(buffer, 0, buffer.length)
212
+ const int32ArrayWithLineBreaksNormalized = int32Array.filter(
213
+ (element, index, typedArray) => {
214
+ if (element === 0x0d) {
215
+ if (typedArray[index + 1] === 0x0a) {
216
+ // Windows -> Unix
217
+ return false
218
+ }
219
+ // Mac OS -> Unix
220
+ typedArray[index] = 0x0a
221
+ }
222
+ return true
223
+ },
224
+ )
225
+ return Buffer.from(int32ArrayWithLineBreaksNormalized)
226
+ }
@@ -9,8 +9,8 @@ which one is being done first
9
9
  */
10
10
 
11
11
  import { createHash } from "crypto"
12
+
12
13
  import { require } from "../require.js"
13
- import { renderNamePattern } from "../renderNamePattern.js"
14
14
 
15
15
  // https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.js
16
16
  // eslint-disable-next-line import/no-unresolved
@@ -519,6 +519,13 @@ export const getUniqueNameForInlineHtmlNode = (node, nodes, pattern) => {
519
519
  })
520
520
  }
521
521
 
522
+ const renderNamePattern = (pattern, replacements) => {
523
+ return pattern.replace(/\[(\w+)\]/g, (_match, type) => {
524
+ const replacement = replacements[type]()
525
+ return replacement
526
+ })
527
+ }
528
+
522
529
  const parseHtmlAsSingleElement = (html) => {
523
530
  const parse5 = require("parse5")
524
531
  const fragment = parse5.parseFragment(html)
@@ -41,10 +41,10 @@ export const createCompiledFileService = ({
41
41
  outDirectoryRelativeUrl,
42
42
 
43
43
  runtimeSupport,
44
- transformTopLevelAwait,
44
+ babelPluginMap,
45
45
  moduleOutFormat,
46
46
  importMetaFormat,
47
- babelPluginMap,
47
+ topLevelAwait,
48
48
  groupMap,
49
49
  customCompilers,
50
50
 
@@ -185,16 +185,16 @@ export const createCompiledFileService = ({
185
185
  request,
186
186
 
187
187
  runtimeSupport,
188
+ babelPluginMap: babelPluginMapFromCompileId(compileId, {
189
+ babelPluginMap,
190
+ groupMap,
191
+ }),
188
192
  moduleOutFormat:
189
193
  moduleOutFormat === undefined
190
194
  ? compileIdModuleFormats[compileId]
191
195
  : moduleOutFormat,
192
196
  importMetaFormat,
193
- transformTopLevelAwait,
194
- babelPluginMap: babelPluginMapFromCompileId(compileId, {
195
- babelPluginMap,
196
- groupMap,
197
- }),
197
+ topLevelAwait,
198
198
 
199
199
  sourcemapMethod,
200
200
  sourcemapExcludeSources,
@@ -5,11 +5,9 @@ import { babelPluginTransformImportMeta } from "@jsenv/core/src/internal/babel_p
5
5
  import {
6
6
  getMinimalBabelPluginMap,
7
7
  babelPluginsFromBabelPluginMap,
8
- extractSyntaxBabelPluginMap,
9
8
  } from "@jsenv/core/src/internal/compiling/babel_plugins.js"
10
9
  import { babelPluginImportMetadata } from "@jsenv/core/src/internal/compiling/babel_plugin_import_metadata.js"
11
10
 
12
- import { findAsyncPluginNameInBabelPluginMap } from "./findAsyncPluginNameInBabelPluginMap.js"
13
11
  import { ansiToHTML } from "./ansiToHTML.js"
14
12
  import { babelPluginRegeneratorRuntimeAsJsenvImport } from "./babel_plugin_regenerator_runtime_as_jsenv_import.js"
15
13
  import { babelPluginBabelHelpersAsJsenvImports } from "./babel_plugin_babel_helpers_as_jsenv_imports.js"
@@ -25,10 +23,9 @@ export const jsenvTransform = async ({
25
23
  babelPluginMap,
26
24
  moduleOutFormat,
27
25
  importMetaFormat = moduleOutFormat,
26
+ topLevelAwait,
28
27
 
29
28
  babelHelpersInjectionAsImport,
30
- allowTopLevelAwait,
31
- transformTopLevelAwait,
32
29
  transformGenerator,
33
30
  regeneratorRuntimeImportPath,
34
31
  sourcemapEnabled,
@@ -50,7 +47,11 @@ export const jsenvTransform = async ({
50
47
  sourceFileName: inputPath,
51
48
  // https://babeljs.io/docs/en/options#parseropts
52
49
  parserOpts: {
53
- allowAwaitOutsideFunction: allowTopLevelAwait,
50
+ allowAwaitOutsideFunction:
51
+ topLevelAwait === undefined ||
52
+ topLevelAwait === "return" ||
53
+ topLevelAwait === "simple" ||
54
+ topLevelAwait === "ignore",
54
55
  },
55
56
  generatorOpts: {
56
57
  compact: false,
@@ -147,56 +148,9 @@ export const jsenvTransform = async ({
147
148
  "import-metadata": [babelPluginImportMetadata],
148
149
  }
149
150
 
150
- const asyncPluginName = findAsyncPluginNameInBabelPluginMap(babelPluginMap)
151
-
152
- if (
153
- moduleOutFormat === "systemjs" &&
154
- transformTopLevelAwait &&
155
- asyncPluginName
156
- ) {
157
- const babelPluginMapWithoutAsync = {
158
- ...babelPluginMap,
159
- "proposal-dynamic-import": [proposalDynamicImport],
160
- "transform-modules-systemjs": [transformModulesSystemJs],
161
- }
162
- delete babelPluginMapWithoutAsync[asyncPluginName]
163
-
164
- // put body inside something like (async () => {})()
165
- const result = await babelTransform({
166
- ast,
167
- code,
168
- options: {
169
- ...options,
170
- plugins: babelPluginsFromBabelPluginMap(babelPluginMapWithoutAsync),
171
- },
172
- })
173
-
174
- // we need to retranspile the await keywords now wrapped
175
- // inside Systemjs function.
176
- // They are ignored, at least by transform-async-to-promises
177
- // see https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/26
178
- const { babelSyntaxPluginMap } = extractSyntaxBabelPluginMap(babelPluginMap)
179
- const finalResult = await babelTransform({
180
- // ast: result.ast,
181
- code: result.code,
182
- options: {
183
- ...options,
184
- // about inputSourceMap see
185
- // https://github.com/babel/babel/blob/eac4c5bc17133c2857f2c94c1a6a8643e3b547a7/packages/babel-core/src/transformation/file/generate.js#L57
186
- // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-core/src/transformation/file/merge-map.js#L6s
187
- inputSourceMap: result.map,
188
- plugins: babelPluginsFromBabelPluginMap({
189
- ...babelSyntaxPluginMap,
190
- [asyncPluginName]: babelPluginMap[asyncPluginName],
191
- }),
192
- },
193
- })
194
-
195
- return {
196
- ...result,
197
- ...finalResult,
198
- metadata: { ...result.metadata, ...finalResult.metadata },
199
- }
151
+ const asyncToPromise = babelPluginMap["transform-async-to-promises"]
152
+ if (topLevelAwait && asyncToPromise) {
153
+ asyncToPromise.options.topLevelAwait = topLevelAwait
200
154
  }
201
155
 
202
156
  const babelTransformReturnValue = await babelTransform({
@@ -11,9 +11,8 @@ export const transformJs = async ({
11
11
  babelPluginMap,
12
12
  moduleOutFormat = "esmodule",
13
13
  importMetaFormat = moduleOutFormat,
14
- babelHelpersInjectionAsImport = true,
15
- allowTopLevelAwait = true,
16
- transformTopLevelAwait = true,
14
+ babelHelpersInjectionAsImport = moduleOutFormat === "esmodule",
15
+ topLevelAwait,
17
16
  transformGenerator = true,
18
17
  sourcemapEnabled = true,
19
18
  }) => {
@@ -33,6 +32,11 @@ export const transformJs = async ({
33
32
  if (typeof url !== "string") {
34
33
  throw new TypeError(`url must be a string, got ${url}`)
35
34
  }
35
+ if (babelHelpersInjectionAsImport && moduleOutFormat !== "esmodule") {
36
+ throw new Error(
37
+ `babelHelpersInjectionAsImport can be enabled only when "moduleOutFormat" is "esmodule"`,
38
+ )
39
+ }
36
40
 
37
41
  const transformResult = await jsenvTransform({
38
42
  code,
@@ -47,8 +51,7 @@ export const transformJs = async ({
47
51
  importMetaFormat,
48
52
 
49
53
  babelHelpersInjectionAsImport,
50
- allowTopLevelAwait,
51
- transformTopLevelAwait,
54
+ topLevelAwait,
52
55
  transformGenerator,
53
56
  sourcemapEnabled,
54
57
  })
@@ -42,10 +42,10 @@ export const compileHtml = async ({
42
42
  outDirectoryRelativeUrl,
43
43
  compileId,
44
44
 
45
- transformTopLevelAwait,
45
+ babelPluginMap,
46
46
  moduleOutFormat,
47
47
  importMetaFormat,
48
- babelPluginMap,
48
+ topLevelAwait,
49
49
 
50
50
  sourcemapMethod,
51
51
 
@@ -284,10 +284,10 @@ export const compileHtml = async ({
284
284
  compiledUrl: scriptCompiledFileUrl,
285
285
  projectDirectoryUrl,
286
286
 
287
- transformTopLevelAwait,
287
+ babelPluginMap,
288
288
  moduleOutFormat,
289
289
  importMetaFormat,
290
- babelPluginMap,
290
+ topLevelAwait,
291
291
  })
292
292
  } catch (e) {
293
293
  // If there is a syntax error in inline script
@@ -9,9 +9,9 @@ export const compileJavascript = async ({
9
9
  projectDirectoryUrl,
10
10
 
11
11
  babelPluginMap,
12
- transformTopLevelAwait,
13
12
  moduleOutFormat,
14
13
  importMetaFormat,
14
+ topLevelAwait,
15
15
 
16
16
  sourcemapExcludeSources,
17
17
  sourcemapMethod,
@@ -24,9 +24,9 @@ export const compileJavascript = async ({
24
24
  projectDirectoryUrl,
25
25
 
26
26
  babelPluginMap,
27
- transformTopLevelAwait,
28
27
  moduleOutFormat,
29
28
  importMetaFormat,
29
+ topLevelAwait,
30
30
  })
31
31
 
32
32
  return transformResultToCompilationResult(
@@ -82,9 +82,9 @@ export const startCompileServer = async ({
82
82
  projectFileCacheStrategy = "mtime",
83
83
 
84
84
  // js compile options
85
- transformTopLevelAwait = true,
86
85
  moduleOutFormat,
87
86
  importMetaFormat,
87
+ topLevelAwait,
88
88
  env = {},
89
89
  processEnvNodeEnv = process.env.NODE_ENV,
90
90
  replaceProcessEnvNodeEnv = true,
@@ -310,7 +310,7 @@ export const startCompileServer = async ({
310
310
  importDefaultExtension,
311
311
 
312
312
  runtimeSupport,
313
- transformTopLevelAwait,
313
+ topLevelAwait,
314
314
  groupMap: compileServerGroupMap,
315
315
  babelPluginMap,
316
316
  customCompilers,