@jsenv/core 24.6.1 → 24.6.5

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 (32) hide show
  1. package/dist/browser_runtime/asset-manifest.json +1 -1
  2. package/dist/browser_runtime/{browser_runtime-c7288751.js → browser_runtime-bb0e3aa4.js} +37 -27
  3. package/dist/browser_runtime/{browser_runtime-c7288751.js.map → browser_runtime-bb0e3aa4.js.map} +3 -3
  4. package/dist/build_manifest.js +5 -5
  5. package/dist/compile_proxy/asset-manifest.json +1 -1
  6. package/dist/compile_proxy/{compile_proxy-a3969633.html → compile_proxy-6eb67db4.html} +39 -34
  7. package/dist/compile_proxy/{compile_proxy.html__inline__20-9c92c170.js.map → compile_proxy.html__inline__20-9e168143.js.map} +3 -2
  8. package/dist/redirector/asset-manifest.json +1 -1
  9. package/dist/redirector/{redirector-a6b8d640.html → redirector-b6ad84bf.html} +39 -34
  10. package/dist/redirector/{redirector.html__inline__15-58430672.js.map → redirector.html__inline__15-3a34a156.js.map} +3 -2
  11. package/dist/toolbar/asset-manifest.json +1 -1
  12. package/dist/toolbar/{toolbar-84985f43.html → toolbar-1fbf8dcb.html} +86 -61
  13. package/dist/toolbar/{toolbar.main-7aa01366.js.map → toolbar.main-a5ef2c60.js.map} +3 -2
  14. package/dist/toolbar_injector/asset-manifest.json +1 -1
  15. package/dist/toolbar_injector/{toolbar_injector-8edcae04.js → toolbar_injector-997dbaa0.js} +16 -16
  16. package/dist/toolbar_injector/{toolbar_injector-8edcae04.js.map → toolbar_injector-997dbaa0.js.map} +4 -4
  17. package/package.json +31 -31
  18. package/readme.md +23 -35
  19. package/src/buildProject.js +3 -5
  20. package/src/dev_server.js +6 -2
  21. package/src/internal/browser_launcher/executeHtmlFile.js +2 -1
  22. package/src/internal/browser_launcher/from_playwright.js +4 -0
  23. package/src/internal/building/buildUsingRollup.js +15 -11
  24. package/src/internal/building/rollup_plugin_jsenv.js +65 -53
  25. package/src/internal/building/url_loader.js +3 -0
  26. package/src/internal/compiling/createCompiledFileService.js +7 -7
  27. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +9 -55
  28. package/src/internal/compiling/js-compilation-service/transformJs.js +8 -5
  29. package/src/internal/compiling/jsenvCompilerForHtml.js +4 -4
  30. package/src/internal/compiling/jsenvCompilerForJavaScript.js +2 -2
  31. package/src/internal/compiling/startCompileServer.js +2 -2
  32. package/src/internal/compiling/js-compilation-service/findAsyncPluginNameInBabelPluginMap.js +0 -9
@@ -17,6 +17,7 @@ import {
17
17
  } from "@jsenv/filesystem"
18
18
  import { UNICODE } from "@jsenv/log"
19
19
 
20
+ import { transformJs } from "@jsenv/core/src/internal/compiling/js-compilation-service/transformJs.js"
20
21
  import { createUrlConverter } from "@jsenv/core/src/internal/url_conversion.js"
21
22
  import { createUrlFetcher } from "@jsenv/core/src/internal/building/url_fetcher.js"
22
23
  import { createUrlLoader } from "@jsenv/core/src/internal/building/url_loader.js"
@@ -48,7 +49,7 @@ import { getDefaultImportMap } from "../import-resolution/importmap-default.js"
48
49
  import { injectSourcemapInRollupBuild } from "./rollup_build_sourcemap.js"
49
50
  import { createBuildStats } from "./build_stats.js"
50
51
 
51
- export const createJsenvRollupPlugin = async ({
52
+ export const createRollupPlugins = async ({
52
53
  buildOperation,
53
54
  logger,
54
55
 
@@ -72,7 +73,6 @@ export const createJsenvRollupPlugin = async ({
72
73
  format,
73
74
  systemJsUrl,
74
75
  babelPluginMap,
75
- transformTopLevelAwait,
76
76
  node,
77
77
  importAssertionsSupport,
78
78
 
@@ -239,8 +239,40 @@ export const createJsenvRollupPlugin = async ({
239
239
  let minifyJs
240
240
  let minifyHtml
241
241
 
242
- const jsenvRollupPlugin = {}
243
-
242
+ const rollupPlugins = []
243
+ // When format is systemjs, rollup add async/await
244
+ // that might be unsupported by the runtime.
245
+ // in that case we have to transform the rollup output
246
+ if (babelPluginMap["transform-async-to-promises"] && format === "systemjs") {
247
+ rollupPlugins.push({
248
+ name: "jsenv_fix_async_await",
249
+ async renderChunk(code, chunk) {
250
+ let map = chunk.map
251
+ const result = await transformJs({
252
+ code,
253
+ url: chunk.facadeModuleId
254
+ ? asOriginalUrl(chunk.facadeModuleId)
255
+ : resolveUrl(chunk.fileName, buildDirectoryUrl),
256
+ projectDirectoryUrl,
257
+ babelPluginMap: {
258
+ "transform-async-to-promises":
259
+ babelPluginMap["transform-async-to-promises"],
260
+ },
261
+ // pass undefined when format is "systemjs" to avoid
262
+ // re-wrapping the code in systemjs format
263
+ moduleOutFormat: undefined,
264
+ babelHelpersInjectionAsImport: false,
265
+ transformGenerator: false,
266
+ })
267
+ code = result.code
268
+ map = result.map
269
+ return {
270
+ code,
271
+ map,
272
+ }
273
+ },
274
+ })
275
+ }
244
276
  if (minify) {
245
277
  const methodHooks = {
246
278
  minifyJs: async (...args) => {
@@ -271,27 +303,29 @@ export const createJsenvRollupPlugin = async ({
271
303
  return methodHooks.minifyHtml(html, minifyHtmlOptions)
272
304
  }
273
305
 
274
- jsenvRollupPlugin.renderChunk = async (code, chunk) => {
275
- let map = chunk.map
276
- const result = await minifyJs({
277
- url: chunk.facadeModuleId
278
- ? asOriginalUrl(chunk.facadeModuleId)
279
- : resolveUrl(chunk.fileName, buildDirectoryUrl),
280
- code,
281
- map,
282
- ...(format === "global" ? { toplevel: false } : { toplevel: true }),
283
- })
306
+ rollupPlugins.push({
307
+ name: "jsenv_minifier",
308
+ async renderChunk(code, chunk) {
309
+ let map = chunk.map
310
+ const result = await minifyJs({
311
+ url: chunk.facadeModuleId
312
+ ? asOriginalUrl(chunk.facadeModuleId)
313
+ : resolveUrl(chunk.fileName, buildDirectoryUrl),
314
+ code,
315
+ map,
316
+ ...(format === "global" ? { toplevel: false } : { toplevel: true }),
317
+ })
284
318
 
285
- code = result.code
286
- map = result.map
287
- return {
288
- code,
289
- map,
290
- }
291
- }
319
+ code = result.code
320
+ map = result.map
321
+ return {
322
+ code,
323
+ map,
324
+ }
325
+ },
326
+ })
292
327
  }
293
-
294
- Object.assign(jsenvRollupPlugin, {
328
+ rollupPlugins.unshift({
295
329
  name: "jsenv",
296
330
 
297
331
  async buildStart() {
@@ -1105,8 +1139,8 @@ export const createJsenvRollupPlugin = async ({
1105
1139
 
1106
1140
  async generateBundle(outputOptions, rollupResult) {
1107
1141
  const jsChunks = {}
1108
- // rollupResult can be mutated by late asset emission
1109
- // howeverl late chunk (js module) emission is not possible
1142
+ // To keep in mind: rollupResult object can be mutated by late asset emission
1143
+ // however late chunk (js module) emission is not possible
1110
1144
  // as rollup rightfully prevent late js emission
1111
1145
  Object.keys(rollupResult).forEach((fileName) => {
1112
1146
  const file = rollupResult[fileName]
@@ -1137,10 +1171,6 @@ export const createJsenvRollupPlugin = async ({
1137
1171
  jsChunks[fileName] = fileCopy
1138
1172
  }
1139
1173
  })
1140
- await ensureTopLevelAwaitTranspilationIfNeeded({
1141
- format,
1142
- transformTopLevelAwait,
1143
- })
1144
1174
 
1145
1175
  const jsModuleBuild = {}
1146
1176
  Object.keys(jsChunks).forEach((fileName) => {
@@ -1152,10 +1182,12 @@ export const createJsenvRollupPlugin = async ({
1152
1182
  !file.isEntry
1153
1183
 
1154
1184
  if (file.url in inlineModuleScripts && format === "systemjs") {
1155
- const magicString = new MagicString(file.code)
1185
+ const code = file.code
1186
+ const systemRegisterIndex = code.indexOf("System.register([")
1187
+ const magicString = new MagicString(code)
1156
1188
  magicString.overwrite(
1157
- 0,
1158
- "System.register([".length,
1189
+ systemRegisterIndex,
1190
+ systemRegisterIndex + "System.register([".length,
1159
1191
  `System.register("${fileName}", [`,
1160
1192
  )
1161
1193
  file.code = magicString.toString()
@@ -1366,7 +1398,7 @@ export const createJsenvRollupPlugin = async ({
1366
1398
  }
1367
1399
 
1368
1400
  return {
1369
- jsenvRollupPlugin,
1401
+ rollupPlugins,
1370
1402
  getLastErrorMessage: () => lastErrorMessage,
1371
1403
  getResult: () => {
1372
1404
  return {
@@ -1386,26 +1418,6 @@ export const createJsenvRollupPlugin = async ({
1386
1418
  }
1387
1419
  }
1388
1420
 
1389
- const ensureTopLevelAwaitTranspilationIfNeeded = async ({
1390
- format,
1391
- transformTopLevelAwait,
1392
- }) => {
1393
- if (!transformTopLevelAwait) {
1394
- return
1395
- }
1396
-
1397
- if (format === "esmodule") {
1398
- // transform-async-to-promises won't be able to transform top level await
1399
- // for "esmodule", so it would be useless
1400
- return
1401
- }
1402
-
1403
- if (format === "systemjs") {
1404
- // top level await is an async function for systemjs
1405
- return
1406
- }
1407
- }
1408
-
1409
1421
  const prepareEntryPoints = async (
1410
1422
  entryPointMap,
1411
1423
  {
@@ -119,6 +119,9 @@ export const createUrlLoader = ({
119
119
  projectDirectoryUrl,
120
120
  babelPluginMap,
121
121
  // moduleOutFormat: format // we are compiling for rollup output must be "esmodule"
122
+ // we compile for rollup, let top level await untouched
123
+ // it will be converted, if needed, during "renderChunk" hook
124
+ topLevelAwait: "ignore",
122
125
  })
123
126
  let code = transformResult.code
124
127
  let map = transformResult.map
@@ -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,
@@ -1,9 +0,0 @@
1
- export const findAsyncPluginNameInBabelPluginMap = (babelPluginMap) => {
2
- if ("transform-async-to-promises" in babelPluginMap) {
3
- return "transform-async-to-promises"
4
- }
5
- if ("transform-async-to-generator" in babelPluginMap) {
6
- return "transform-async-to-generator"
7
- }
8
- return ""
9
- }