@jsenv/core 25.0.0-alpha.0 → 25.0.0-alpha.1

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 -2
  2. package/dist/browser_runtime/{browser_runtime_a8097085.js → browser_runtime_91c5a3b8.js} +137 -26
  3. package/dist/browser_runtime/browser_runtime_91c5a3b8.js.map +1089 -0
  4. package/dist/build_manifest.js +5 -5
  5. package/dist/compile_proxy/asset-manifest.json +2 -2
  6. package/dist/compile_proxy/{compile_proxy_e16d7de8.html → compile_proxy_7ad5faa6.html} +119 -26
  7. package/dist/compile_proxy/{compile_proxy_e3b0c442_9e168143.js.map → compile_proxy_e3b0c442_809f35f7.js.map} +6 -6
  8. package/dist/redirector/asset-manifest.json +2 -2
  9. package/dist/redirector/{redirector_e3b0c442_3a34a156.js.map → redirector_e3b0c442_e391410e.js.map} +6 -6
  10. package/dist/redirector/{redirector_2e0c8abe.html → redirector_eb92e8a7.html} +119 -26
  11. package/dist/toolbar/asset-manifest.json +11 -11
  12. package/dist/toolbar/{toolbar.main_a5ef2c60.js.map → toolbar.main2_6c1b3d82.js.map} +8 -8
  13. package/dist/toolbar/{toolbar_412abb83.html → toolbar_04ba410c.html} +127 -32
  14. package/dist/toolbar_injector/asset-manifest.json +2 -2
  15. package/dist/toolbar_injector/{toolbar_injector_4f9c19e5.js → toolbar_injector_4a48bc53.js} +2 -2
  16. package/dist/toolbar_injector/{toolbar_injector_4f9c19e5.js.map → toolbar_injector_4a48bc53.js.map} +2 -2
  17. package/package.json +5 -4
  18. package/readme.md +22 -89
  19. package/src/buildProject.js +28 -15
  20. package/src/dev_server.js +8 -2
  21. package/src/execute.js +7 -1
  22. package/src/executeTestPlan.js +6 -0
  23. package/src/internal/browser_feature_detection/browser_feature_detection.js +18 -25
  24. package/src/internal/browser_runtime/browser_runtime.js +2 -2
  25. package/src/internal/browser_runtime/createBrowserRuntime.js +1 -1
  26. package/src/internal/browser_runtime/displayErrorInDocument.js +2 -0
  27. package/src/internal/browser_runtime/displayErrorNotification.js +1 -1
  28. package/src/internal/building/buildUsingRollup.js +3 -8
  29. package/src/internal/building/ressource_builder.js +35 -42
  30. package/src/internal/building/rollup_plugin_jsenv.js +195 -186
  31. package/src/internal/building/url_versioning.js +36 -42
  32. package/src/internal/compiling/createCompiledFileService.js +28 -40
  33. package/src/internal/compiling/html_source_file_service.js +66 -51
  34. package/src/internal/compiling/js-compilation-service/babel_plugin_systemjs_prepend.js +23 -0
  35. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +16 -12
  36. package/src/internal/compiling/js-compilation-service/transformJs.js +2 -0
  37. package/src/internal/compiling/jsenvCompilerForHtml.js +43 -44
  38. package/src/internal/compiling/jsenvCompilerForImportmap.js +15 -76
  39. package/src/internal/compiling/jsenvCompilerForJavaScript.js +9 -0
  40. package/src/internal/compiling/startCompileServer.js +29 -5
  41. package/src/internal/dev_server/toolbar/compilation/toolbar.compilation.js +9 -9
  42. package/src/internal/executing/executePlan.js +6 -0
  43. package/src/internal/generateGroupMap/{jsenvBabelPluginCompatMap.js → babel_plugins_compatibility.js} +0 -0
  44. package/src/internal/generateGroupMap/{featuresCompatMap.js → features_compatibility.js} +9 -1
  45. package/src/internal/generateGroupMap/generateGroupMap.js +6 -35
  46. package/src/internal/generateGroupMap/one_runtime_compat.js +9 -12
  47. package/src/internal/generateGroupMap/runtime_compat.js +10 -15
  48. package/src/internal/generateGroupMap/runtime_compat_composition.js +2 -2
  49. package/src/internal/generateGroupMap/shake_babel_plugin_map.js +21 -0
  50. package/src/internal/import-resolution/importmap_default.js +52 -0
  51. package/src/internal/node_feature_detection/node_feature_detection.js +25 -19
  52. package/src/internal/runtime/s.js +101 -6
  53. package/src/internal/unevalException.js +1 -1
  54. package/src/jsenvServiceWorkerFinalizer.js +3 -1
  55. package/dist/browser_runtime/browser_runtime_a8097085.js.map +0 -1067
  56. package/src/internal/generateGroupMap/jsenvPluginCompatMap.js +0 -1
  57. package/src/internal/import-resolution/importmap-default.js +0 -34
@@ -1,6 +1,6 @@
1
1
  import { extname } from "node:path"
2
2
  import MagicString from "magic-string"
3
- import { normalizeImportMap } from "@jsenv/importmap"
3
+ import { composeTwoImportMaps, normalizeImportMap } from "@jsenv/importmap"
4
4
  import { isSpecifierForNodeCoreModule } from "@jsenv/importmap/src/isSpecifierForNodeCoreModule.js"
5
5
  import { createDetailedMessage, loggerToLogLevel } from "@jsenv/logger"
6
6
  import {
@@ -14,6 +14,7 @@ import {
14
14
  urlToMeta,
15
15
  urlToBasename,
16
16
  urlToFilename,
17
+ readFile,
17
18
  } from "@jsenv/filesystem"
18
19
  import { UNICODE } from "@jsenv/log"
19
20
 
@@ -26,6 +27,9 @@ import { createUrlLoader } from "@jsenv/core/src/internal/building/url_loader.js
26
27
  import { stringifyUrlTrace } from "@jsenv/core/src/internal/building/url_trace.js"
27
28
  import { sortObjectByPathnames } from "@jsenv/core/src/internal/building/sortObjectByPathnames.js"
28
29
  import { jsenvHelpersDirectoryInfo } from "@jsenv/core/src/internal/jsenvInternalFiles.js"
30
+ import { createImportResolverForNode } from "@jsenv/core/src/internal/import-resolution/import-resolver-node.js"
31
+ import { createImportResolverForImportmap } from "@jsenv/core/src/internal/import-resolution/import-resolver-importmap.js"
32
+ import { getDefaultImportmap } from "@jsenv/core/src/internal/import-resolution/importmap_default.js"
29
33
 
30
34
  import {
31
35
  formatBuildStartLog,
@@ -44,9 +48,6 @@ import {
44
48
  import { createUrlVersioner } from "./url_versioning.js"
45
49
  import { visitImportReferences } from "./import_references.js"
46
50
 
47
- import { createImportResolverForNode } from "../import-resolution/import-resolver-node.js"
48
- import { createImportResolverForImportmap } from "../import-resolution/import-resolver-importmap.js"
49
- import { getDefaultImportMap } from "../import-resolution/importmap-default.js"
50
51
  import { createBuildStats } from "./build_stats.js"
51
52
 
52
53
  export const createRollupPlugins = async ({
@@ -110,26 +111,19 @@ export const createRollupPlugins = async ({
110
111
  const url = resolveUrl(key, projectDirectoryUrl)
111
112
  entryPointUrls[url] = entryPoints[key]
112
113
  })
113
- const workerUrls = {}
114
- Object.keys(workers).forEach((key) => {
115
- const url = resolveUrl(key, projectDirectoryUrl)
116
- workerUrls[url] = workers[key]
117
- })
118
- const serviceWorkerUrls = {}
119
- Object.keys(serviceWorkers).forEach((key) => {
120
- const url = resolveUrl(key, projectDirectoryUrl)
121
- serviceWorkerUrls[url] = serviceWorkers[key]
122
- })
123
- const classicWorkerUrls = {}
124
- Object.keys(classicWorkers).forEach((key) => {
125
- const url = resolveUrl(key, projectDirectoryUrl)
126
- classicWorkerUrls[url] = classicWorkers[key]
127
- })
128
- const classicServiceWorkerUrls = {}
129
- Object.keys(classicServiceWorkers).forEach((key) => {
130
- const url = resolveUrl(key, projectDirectoryUrl)
131
- classicServiceWorkerUrls[url] = classicServiceWorkers[key]
132
- })
114
+ const workerUrls = workers.map((worker) =>
115
+ resolveUrl(worker, projectDirectoryUrl),
116
+ )
117
+ const serviceWorkerUrls = serviceWorkers.map((serviceWorker) =>
118
+ resolveUrl(serviceWorker, projectDirectoryUrl),
119
+ )
120
+ const classicWorkerUrls = classicWorkers.map((classicWorker) =>
121
+ resolveUrl(classicWorker, projectDirectoryUrl),
122
+ )
123
+ const classicServiceWorkerUrls = classicServiceWorkers.map(
124
+ (classicServiceWorker) =>
125
+ resolveUrl(classicServiceWorker, projectDirectoryUrl),
126
+ )
133
127
 
134
128
  let ressourceBuilder
135
129
  let importResolver
@@ -155,10 +149,6 @@ export const createRollupPlugins = async ({
155
149
 
156
150
  const urlVersioner = createUrlVersioner({
157
151
  entryPointUrls,
158
- workerUrls,
159
- classicWorkerUrls,
160
- serviceWorkerUrls,
161
- classicServiceWorkerUrls,
162
152
  asOriginalUrl,
163
153
  lineBreakNormalization,
164
154
  })
@@ -334,6 +324,7 @@ export const createRollupPlugins = async ({
334
324
  : resolveUrl(chunk.fileName, buildDirectoryUrl),
335
325
  code,
336
326
  map,
327
+ module: format === "esmodule",
337
328
  ...(format === "global" ? { toplevel: false } : { toplevel: true }),
338
329
  })
339
330
 
@@ -463,9 +454,21 @@ export const createRollupPlugins = async ({
463
454
  )
464
455
  importMapUrl = htmlCompiledUrl
465
456
  fetchImportMap = () => {
466
- const importMapRaw = JSON.parse(importMapInfoFromHtml.text)
467
- const importMap = normalizeImportMap(importMapRaw, importMapUrl)
468
- return importMap
457
+ const importmapFileUrl = asProjectUrl(importMapUrl)
458
+ const jsenvImportmap = getDefaultImportmap(importmapFileUrl, {
459
+ projectDirectoryUrl,
460
+ compileDirectoryUrl,
461
+ })
462
+ const htmlImportmap = JSON.parse(importMapInfoFromHtml.text)
463
+ const importmap = composeTwoImportMaps(
464
+ jsenvImportmap,
465
+ htmlImportmap,
466
+ )
467
+ const importmapNormalized = normalizeImportMap(
468
+ importmap,
469
+ importMapUrl,
470
+ )
471
+ return importmapNormalized
469
472
  }
470
473
  }
471
474
  } else if (importMapFileRelativeUrl) {
@@ -488,16 +491,19 @@ export const createRollupPlugins = async ({
488
491
  entryProjectRelativeUrl,
489
492
  compileDirectoryUrl,
490
493
  )
491
- const defaultImportMap = getDefaultImportMap({
492
- importMapFileUrl: entryCompileUrl,
494
+ const jsenvImportmap = getDefaultImportmap(entryCompileUrl, {
493
495
  projectDirectoryUrl,
494
- compileDirectoryRelativeUrl,
496
+ compileDirectoryUrl,
495
497
  })
496
498
  const entryCompileServerUrl = resolveUrl(
497
499
  entryProjectRelativeUrl,
498
500
  compileDirectoryServerUrl,
499
501
  )
500
- return normalizeImportMap(defaultImportMap, entryCompileServerUrl)
502
+ const importmapNormalized = normalizeImportMap(
503
+ jsenvImportmap,
504
+ entryCompileServerUrl,
505
+ )
506
+ return importmapNormalized
501
507
  }
502
508
  }
503
509
 
@@ -624,32 +630,27 @@ export const createRollupPlugins = async ({
624
630
  }
625
631
 
626
632
  const originalUrl = asOriginalUrl(projectUrl)
627
- const workerBuildRelativeUrl = workerUrls[originalUrl]
628
- if (workerBuildRelativeUrl) {
633
+ if (workerUrls.includes(originalUrl)) {
629
634
  return {
630
635
  isWorker: true,
631
636
  isJsModule: true,
632
637
  url: ressourceUrl,
633
638
  }
634
639
  }
635
- const serviceWorkerBuildRelativeUrl = serviceWorkerUrls[originalUrl]
636
- if (serviceWorkerBuildRelativeUrl) {
640
+ if (serviceWorkerUrls.includes(originalUrl)) {
637
641
  return {
638
642
  isServiceWorker: true,
639
643
  isJsModule: true,
640
644
  url: ressourceUrl,
641
645
  }
642
646
  }
643
- const classicWorkerBuildRelativeUrl = classicWorkerUrls[originalUrl]
644
- if (classicWorkerBuildRelativeUrl) {
647
+ if (classicWorkerUrls.includes(originalUrl)) {
645
648
  return {
646
649
  isWorker: true,
647
650
  url: ressourceUrl,
648
651
  }
649
652
  }
650
- const classicServiceWorkerBuildRelativeUrl =
651
- classicServiceWorkerUrls[originalUrl]
652
- if (classicServiceWorkerBuildRelativeUrl) {
653
+ if (classicServiceWorkerUrls.includes(originalUrl)) {
653
654
  return {
654
655
  isServiceWorker: true,
655
656
  url: ressourceUrl,
@@ -781,7 +782,7 @@ export const createRollupPlugins = async ({
781
782
  }
782
783
  },
783
784
 
784
- async resolveId(specifier, importer, { custom }) {
785
+ async resolveId(specifier, importer, { custom = {} } = {}) {
785
786
  if (specifier === EMPTY_CHUNK_URL) {
786
787
  return specifier
787
788
  }
@@ -819,7 +820,7 @@ export const createRollupPlugins = async ({
819
820
  }
820
821
 
821
822
  if (inlineModuleScripts.hasOwnProperty(specifier)) {
822
- return specifier
823
+ return asRollupUrl(specifier)
823
824
  }
824
825
 
825
826
  const importUrl = await importResolver.resolveImport(
@@ -868,31 +869,28 @@ export const createRollupPlugins = async ({
868
869
 
869
870
  resolveFileUrl: ({ referenceId, fileName }) => {
870
871
  ressourcesReferencedByJs.push(fileName)
871
-
872
+ const getBuildRelativeUrl = () => {
873
+ const ressource = ressourceBuilder.findRessource((ressource) => {
874
+ return ressource.rollupReferenceId === referenceId
875
+ })
876
+ ressource.fileName = fileName
877
+ const buildRelativeUrl = ressource.buildRelativeUrl
878
+ return buildRelativeUrl
879
+ }
872
880
  if (format === "esmodule") {
873
881
  if (!node && useImportMapToMaximizeCacheReuse && urlVersioning) {
874
882
  return `window.__resolveImportUrl__("./${fileName}", import.meta.url)`
875
883
  }
876
- return `new URL("${fileName}", import.meta.url)`
884
+ return `new URL("${getBuildRelativeUrl()}", import.meta.url)`
877
885
  }
878
886
  if (format === "systemjs") {
879
887
  return `new URL(System.resolve("./${fileName}", module.meta.url))`
880
888
  }
881
889
  if (format === "global") {
882
- const ressource = ressourceBuilder.findRessource((ressource) => {
883
- return ressource.rollupReferenceId === referenceId
884
- })
885
- ressource.fileName = fileName
886
- const buildRelativeUrl = ressource.buildRelativeUrl
887
- return `new URL("${buildRelativeUrl}", document.currentScript && document.currentScript.src || document.baseURI)`
890
+ return `new URL("${getBuildRelativeUrl()}", document.currentScript && document.currentScript.src || document.baseURI)`
888
891
  }
889
892
  if (format === "commonjs") {
890
- const ressource = ressourceBuilder.findRessource((ressource) => {
891
- return ressource.rollupReferenceId === referenceId
892
- })
893
- ressource.fileName = fileName
894
- const buildRelativeUrl = ressource.buildRelativeUrl
895
- return `new URL("${buildRelativeUrl}", "file:///" + __filename.replace(/\\/g, "/"))`
893
+ return `new URL("${getBuildRelativeUrl()}", "file:///" + __filename.replace(/\\/g, "/"))`
896
894
  }
897
895
  return null
898
896
  },
@@ -973,12 +971,22 @@ export const createRollupPlugins = async ({
973
971
  onReferenceWithImportMetaUrlPattern: async ({ importNode }) => {
974
972
  const specifier = importNode.arguments[0].value
975
973
  const { line, column } = importNode.loc.start
974
+
975
+ const { id } = normalizeRollupResolveReturnValue(
976
+ await this.resolve(specifier, url),
977
+ )
978
+ const ressourceUrl = asServerUrl(id)
979
+ const originalUrl = asOriginalUrl(ressourceUrl)
980
+ const isJsModule = Boolean(
981
+ workerUrls[originalUrl] || serviceWorkerUrls[originalUrl],
982
+ )
976
983
  const reference = ressourceBuilder.createReferenceFoundInJsModule({
977
984
  referenceLabel: "URL + import.meta.url",
978
985
  jsUrl: url,
979
986
  jsLine: line,
980
987
  jsColumn: column,
981
- ressourceSpecifier: specifier,
988
+ ressourceSpecifier: ressourceUrl,
989
+ isJsModule,
982
990
  })
983
991
  if (!reference) {
984
992
  return
@@ -1270,45 +1278,65 @@ export const createRollupPlugins = async ({
1270
1278
  return outputOptions
1271
1279
  },
1272
1280
 
1281
+ async renderChunk(code, chunk) {
1282
+ const { facadeModuleId } = chunk
1283
+ if (!facadeModuleId) {
1284
+ // happens for inline module scripts for instance
1285
+ return null
1286
+ }
1287
+
1288
+ const url = asOriginalUrl(facadeModuleId)
1289
+ if (workerUrls.includes(url) || serviceWorkerUrls.includes(url)) {
1290
+ const magicString = new MagicString(code)
1291
+ const systemjsCode = await readFile(
1292
+ new URL("../runtime/s.js", import.meta.url),
1293
+ )
1294
+ magicString.prepend(systemjsCode)
1295
+ code = magicString.toString()
1296
+ const map = magicString.generateMap({ hires: true })
1297
+ return {
1298
+ code,
1299
+ map,
1300
+ }
1301
+ }
1302
+
1303
+ return null
1304
+ },
1305
+
1273
1306
  async generateBundle(outputOptions, rollupResult) {
1274
- const rollupJsFileInfos = {}
1275
- const rollupAssetFileInfos = {}
1307
+ // it's important to do this to emit late asset
1308
+ rollupEmitFile = (...args) => this.emitFile(...args)
1309
+ rollupSetAssetSource = (...args) => this.setAssetSource(...args)
1276
1310
  // To keep in mind: rollupResult object can be mutated by late asset emission
1277
1311
  // however late chunk (js module) emission is not possible
1278
1312
  // as rollup rightfully prevent late js emission
1279
1313
  Object.keys(rollupResult).forEach((fileName) => {
1280
1314
  const rollupFileInfo = rollupResult[fileName]
1281
-
1282
1315
  // there is 3 types of file: "placeholder", "asset", "chunk"
1283
- if (rollupFileInfo.type === "asset") {
1284
- rollupAssetFileInfos[fileName] = rollupFileInfo
1285
- return
1286
- }
1287
-
1288
1316
  if (rollupFileInfo.type === "chunk") {
1289
1317
  const { facadeModuleId } = rollupFileInfo
1290
1318
  if (facadeModuleId === EMPTY_CHUNK_URL) {
1319
+ delete rollupResult[fileName]
1291
1320
  return
1292
1321
  }
1293
- const fileCopy = { ...rollupFileInfo }
1294
1322
  if (facadeModuleId) {
1295
- fileCopy.url = asServerUrl(facadeModuleId)
1323
+ rollupFileInfo.url = asServerUrl(facadeModuleId)
1296
1324
  } else {
1297
1325
  const sourcePath =
1298
1326
  rollupFileInfo.map.sources[rollupFileInfo.map.sources.length - 1]
1299
- const fileBuildUrl = resolveUrl(
1300
- rollupFileInfo.fileName,
1301
- buildDirectoryUrl,
1302
- )
1327
+ const fileBuildUrl = resolveUrl(fileName, buildDirectoryUrl)
1303
1328
  const originalProjectUrl = resolveUrl(sourcePath, fileBuildUrl)
1304
- fileCopy.url = asCompiledServerUrl(originalProjectUrl, {
1329
+ rollupFileInfo.url = asCompiledServerUrl(originalProjectUrl, {
1305
1330
  projectDirectoryUrl,
1306
1331
  compileServerOrigin,
1307
1332
  compileDirectoryRelativeUrl,
1308
1333
  })
1309
1334
  }
1310
1335
 
1311
- if (fileCopy.url in inlineModuleScripts && format === "systemjs") {
1336
+ if (
1337
+ rollupFileInfo.url in inlineModuleScripts &&
1338
+ format === "systemjs"
1339
+ ) {
1312
1340
  const code = rollupFileInfo.code
1313
1341
  const systemRegisterIndex = code.indexOf("System.register([")
1314
1342
  const magicString = new MagicString(code)
@@ -1317,27 +1345,24 @@ export const createRollupPlugins = async ({
1317
1345
  systemRegisterIndex + "System.register([".length,
1318
1346
  `System.register("${fileName}", [`,
1319
1347
  )
1320
- fileCopy.code = magicString.toString()
1348
+ rollupFileInfo.code = magicString.toString()
1321
1349
  }
1322
1350
 
1351
+ const jsRessource = ressourceBuilder.findRessource((ressource) => {
1352
+ return ressource.url === rollupFileInfo.url
1353
+ })
1323
1354
  if (
1324
1355
  isReferencedByJs({
1325
- rollupFileInfo: fileCopy,
1356
+ rollupFileInfo,
1326
1357
  jsConcatenation,
1327
- ressourceBuilder,
1358
+ jsRessource,
1328
1359
  })
1329
1360
  ) {
1330
- ressourcesReferencedByJs.push(fileCopy.fileName)
1361
+ ressourcesReferencedByJs.push(fileName)
1331
1362
  }
1332
-
1333
- rollupJsFileInfos[fileName] = fileCopy
1334
1363
  }
1335
1364
  })
1336
1365
 
1337
- // it's important to do this to emit late asset
1338
- rollupEmitFile = (...args) => this.emitFile(...args)
1339
- rollupSetAssetSource = (...args) => this.setAssetSource(...args)
1340
-
1341
1366
  // malheureusement rollup ne permet pas de savoir lorsqu'un chunk
1342
1367
  // a fini d'etre résolu (parsing des imports statiques et dynamiques recursivement)
1343
1368
  // donc lorsque le build se termine on va indiquer
@@ -1345,8 +1370,7 @@ export const createRollupPlugins = async ({
1345
1370
  // et donc les assets peuvent connaitre le nom du chunk
1346
1371
  // et mettre a jour leur dépendance vers ce fichier js
1347
1372
  const { jsRessources } = ressourceBuilder.rollupBuildEnd({
1348
- rollupJsFileInfos,
1349
- rollupAssetFileInfos,
1373
+ rollupResult,
1350
1374
  useImportMapToMaximizeCacheReuse,
1351
1375
  })
1352
1376
  Object.keys(jsRessources).forEach((ressourceUrl) => {
@@ -1354,9 +1378,9 @@ export const createRollupPlugins = async ({
1354
1378
  if (jsRessource.isInline) {
1355
1379
  buildInlineFileContents[jsRessource.buildRelativeUrl] =
1356
1380
  jsRessource.bufferAfterBuild
1357
- if (format === "systemjs") {
1358
- ressourcesReferencedByJs.push(jsRessource.fileName)
1359
- }
1381
+ // if (format === "systemjs") {
1382
+ // ressourcesReferencedByJs.push(jsRessource.fileName)
1383
+ // }
1360
1384
  } else {
1361
1385
  const originalProjectUrl = asOriginalUrl(ressourceUrl)
1362
1386
  const originalProjectRelativeUrl = urlToRelativeUrl(
@@ -1366,10 +1390,8 @@ export const createRollupPlugins = async ({
1366
1390
  buildMappings[originalProjectRelativeUrl] =
1367
1391
  jsRessource.buildRelativeUrl
1368
1392
  }
1369
-
1370
1393
  buildManifest[jsRessource.fileName] = jsRessource.buildRelativeUrl
1371
1394
  })
1372
-
1373
1395
  // wait for asset build relative urls
1374
1396
  // to ensure the importmap will contain remappings for them
1375
1397
  // (not sure this is required anymore)
@@ -1384,64 +1406,66 @@ export const createRollupPlugins = async ({
1384
1406
  }
1385
1407
  }),
1386
1408
  )
1387
-
1388
1409
  // wait html files to be emitted
1389
1410
  await ressourceBuilder.getAllEntryPointsEmittedPromise()
1390
1411
  onBundleEnd()
1391
- const jsModuleBuild = {
1392
- ...rollupJsFileInfos,
1393
- }
1412
+
1413
+ const jsModuleBuild = {}
1394
1414
  const assetBuild = {}
1395
1415
  Object.keys(rollupResult).forEach((fileName) => {
1396
- const file = rollupResult[fileName]
1397
- if (file.type !== "asset") {
1398
- return
1399
- }
1400
-
1401
- const assetRessource = ressourceBuilder.findRessource(
1402
- (ressource) =>
1403
- // happens for import.meta.url pattern
1404
- ressource.fileName === fileName ||
1405
- // happens for sourcemap
1406
- ressource.relativeUrl === fileName,
1407
- )
1408
-
1409
- // ignore potential useless assets which happens when:
1410
- // - sourcemap re-emitted
1411
- // - importmap re-emitted to have buildRelativeUrlMap
1412
- if (assetRessource.shouldBeIgnored) {
1413
- return
1414
- }
1416
+ const rollupFileInfo = rollupResult[fileName]
1415
1417
 
1416
- // Ignore file only referenced by import assertions
1417
- // - if file is referenced by import assertion and html or import meta url
1418
- // then source file is duplicated. If concatenation is disabled
1419
- // and import assertions are supported, the file is still converted to js module
1420
- const isReferencedOnlyByImportAssertions =
1421
- assetRessource.references.every((reference) => {
1422
- return reference.isImportAssertion
1418
+ if (rollupFileInfo.type === "chunk") {
1419
+ const jsRessource = ressourceBuilder.findRessource((ressource) => {
1420
+ return ressource.url === rollupFileInfo.url
1423
1421
  })
1424
- if (isReferencedOnlyByImportAssertions) {
1425
- return
1426
- }
1427
-
1428
- const buildRelativeUrl = assetRessource.buildRelativeUrl
1429
- if (assetRessource.isInline) {
1430
- buildInlineFileContents[buildRelativeUrl] = file.source
1422
+ jsModuleBuild[jsRessource.buildRelativeUrl] = rollupFileInfo
1431
1423
  return
1432
1424
  }
1433
1425
 
1434
- // in case sourcemap is mutated, we must not trust rollup but the asset builder source instead
1435
- file.source = assetRessource.bufferAfterBuild
1436
- assetBuild[buildRelativeUrl] = file
1437
- buildManifest[assetRessource.fileName] = buildRelativeUrl
1438
- if (assetRessource.bufferBeforeBuild) {
1439
- const originalProjectUrl = asOriginalUrl(assetRessource.url)
1440
- const originalProjectRelativeUrl = urlToRelativeUrl(
1441
- originalProjectUrl,
1442
- projectDirectoryUrl,
1426
+ if (rollupFileInfo.type === "asset") {
1427
+ const assetRessource = ressourceBuilder.findRessource(
1428
+ (ressource) =>
1429
+ // happens for import.meta.url pattern
1430
+ ressource.fileName === fileName ||
1431
+ // happens for sourcemap
1432
+ ressource.relativeUrl === fileName,
1443
1433
  )
1444
- buildMappings[originalProjectRelativeUrl] = buildRelativeUrl
1434
+ // ignore potential useless assets which happens when:
1435
+ // - sourcemap re-emitted
1436
+ // - importmap re-emitted to have buildRelativeUrlMap
1437
+ if (assetRessource.shouldBeIgnored) {
1438
+ return
1439
+ }
1440
+ // Ignore file only referenced by import assertions
1441
+ // - if file is referenced by import assertion and html or import meta url
1442
+ // then source file is duplicated. If concatenation is disabled
1443
+ // and import assertions are supported, the file is still converted to js module
1444
+ const isReferencedOnlyByImportAssertions =
1445
+ assetRessource.references.every((reference) => {
1446
+ return reference.isImportAssertion
1447
+ })
1448
+ if (isReferencedOnlyByImportAssertions) {
1449
+ return
1450
+ }
1451
+ const buildRelativeUrl = assetRessource.buildRelativeUrl
1452
+ if (assetRessource.isInline) {
1453
+ buildInlineFileContents[buildRelativeUrl] = rollupFileInfo.source
1454
+ return
1455
+ }
1456
+ // in case sourcemap is mutated, we must not trust rollup but the asset builder source instead
1457
+ rollupFileInfo.source = assetRessource.bufferAfterBuild
1458
+ assetBuild[buildRelativeUrl] = rollupFileInfo
1459
+ buildManifest[assetRessource.fileName] = buildRelativeUrl
1460
+ if (assetRessource.bufferBeforeBuild) {
1461
+ const originalProjectUrl = asOriginalUrl(assetRessource.url)
1462
+ const originalProjectRelativeUrl = urlToRelativeUrl(
1463
+ originalProjectUrl,
1464
+ projectDirectoryUrl,
1465
+ )
1466
+ buildMappings[originalProjectRelativeUrl] = buildRelativeUrl
1467
+ }
1468
+ return
1445
1469
  }
1446
1470
  })
1447
1471
  rollupBuild = {
@@ -1452,10 +1476,10 @@ export const createRollupPlugins = async ({
1452
1476
  // fill "buildFileContents", "buildInlineFilesContents"
1453
1477
  // and update "buildManifest" and "buildMappings" in case some ressource where inlined
1454
1478
  // by ressourceBuilder.rollupBuildEnd
1455
- Object.keys(rollupBuild).forEach((fileName) => {
1456
- const rollupFileInfo = rollupBuild[fileName]
1479
+ Object.keys(rollupBuild).forEach((buildRelativeUrl) => {
1480
+ const rollupFileInfo = rollupBuild[buildRelativeUrl]
1457
1481
  const ressource = ressourceBuilder.findRessource((ressource) => {
1458
- if (ressource.buildRelativeUrl === fileName) {
1482
+ if (ressource.buildRelativeUrl === buildRelativeUrl) {
1459
1483
  return true
1460
1484
  }
1461
1485
  if (ressource.url === rollupFileInfo.url) {
@@ -1464,7 +1488,12 @@ export const createRollupPlugins = async ({
1464
1488
  return false
1465
1489
  })
1466
1490
  if (ressource.isInline) {
1467
- delete buildManifest[fileName]
1491
+ delete buildManifest[ressource.fileName]
1492
+ if (ressource.isJsModule) {
1493
+ delete jsModuleBuild[buildRelativeUrl]
1494
+ } else {
1495
+ delete assetBuild[buildRelativeUrl]
1496
+ }
1468
1497
  const originalProjectUrl = asOriginalUrl(ressource.url)
1469
1498
  const originalRelativeUrl = urlToRelativeUrl(
1470
1499
  originalProjectUrl,
@@ -1479,18 +1508,15 @@ export const createRollupPlugins = async ({
1479
1508
  }
1480
1509
  })
1481
1510
 
1482
- await finalizeServiceWorkers({
1483
- serviceWorkers,
1484
- classicServiceWorkers,
1485
- serviceWorkerFinalizer,
1511
+ await visitServiceWorkers({
1486
1512
  projectDirectoryUrl,
1487
- buildDirectoryUrl,
1488
- rollupBuild,
1513
+ serviceWorkerUrls,
1514
+ classicServiceWorkerUrls,
1515
+ serviceWorkerFinalizer,
1489
1516
  buildMappings,
1490
1517
  buildManifest,
1491
1518
  buildFileContents,
1492
1519
  lineBreakNormalization,
1493
- minify,
1494
1520
  })
1495
1521
 
1496
1522
  buildManifest = sortObjectByPathnames(buildManifest)
@@ -1670,17 +1696,10 @@ const acceptsJsonContentType = ({ node, format }) => {
1670
1696
  return false
1671
1697
  }
1672
1698
 
1673
- const isReferencedByJs = ({
1674
- rollupFileInfo,
1675
- jsConcatenation,
1676
- ressourceBuilder,
1677
- }) => {
1699
+ const isReferencedByJs = ({ rollupFileInfo, jsConcatenation, jsRessource }) => {
1678
1700
  if (rollupFileInfo.isDynamicEntry) {
1679
1701
  return true
1680
1702
  }
1681
- const jsRessource = ressourceBuilder.findRessource((ressource) => {
1682
- return ressource.url === rollupFileInfo.url
1683
- })
1684
1703
  if (!jsConcatenation && rollupFileInfo.isEntry) {
1685
1704
  return true
1686
1705
  }
@@ -1693,46 +1712,36 @@ const isReferencedByJs = ({
1693
1712
  return false
1694
1713
  }
1695
1714
 
1696
- const finalizeServiceWorkers = async ({
1697
- serviceWorkers,
1698
- classicServiceWorkers,
1715
+ const visitServiceWorkers = async ({
1716
+ projectDirectoryUrl,
1717
+ serviceWorkerUrls,
1718
+ classicServiceWorkerUrls,
1699
1719
  serviceWorkerFinalizer,
1700
1720
  buildMappings,
1701
1721
  buildManifest,
1702
1722
  buildFileContents,
1703
1723
  lineBreakNormalization,
1704
1724
  }) => {
1705
- const serviceWorkerKeys = Object.keys(serviceWorkers)
1706
- const classicServiceWorkerKeys = Object.keys(classicServiceWorkers)
1707
- const projectRelativeUrls = [
1708
- ...serviceWorkerKeys,
1709
- ...classicServiceWorkerKeys,
1725
+ const allServiceWorkerUrls = [
1726
+ ...serviceWorkerUrls,
1727
+ ...classicServiceWorkerUrls,
1710
1728
  ]
1711
1729
 
1712
1730
  await Promise.all(
1713
- projectRelativeUrls.map(async (projectRelativeUrl) => {
1714
- const projectUrl = resolveUrl(projectRelativeUrl, "file://")
1715
- projectRelativeUrl = urlToRelativeUrl(projectUrl, "file://")
1716
- const serviceWorkerBuildRelativeUrl = buildMappings[projectRelativeUrl]
1731
+ allServiceWorkerUrls.map(async (serviceWorkerUrl) => {
1732
+ const serviceWorkerRelativeUrl = urlToRelativeUrl(
1733
+ serviceWorkerUrl,
1734
+ projectDirectoryUrl,
1735
+ )
1736
+ const serviceWorkerBuildRelativeUrl =
1737
+ buildMappings[serviceWorkerRelativeUrl]
1717
1738
  if (!serviceWorkerBuildRelativeUrl) {
1718
1739
  throw new Error(
1719
- `"${projectRelativeUrl}" service worker file missing in the build`,
1740
+ `"${serviceWorkerRelativeUrl}" service worker file missing in the build`,
1720
1741
  )
1721
1742
  }
1722
1743
 
1723
- // module service worker
1724
- if (serviceWorkerKeys.includes(projectRelativeUrl)) {
1725
- let code = buildFileContents[serviceWorkerBuildRelativeUrl]
1726
- code = await serviceWorkerFinalizer(code, {
1727
- serviceWorkerBuildRelativeUrl,
1728
- buildManifest,
1729
- buildFileContents,
1730
- lineBreakNormalization,
1731
- })
1732
- buildFileContents[serviceWorkerBuildRelativeUrl] = code
1733
- }
1734
- // "classic" service worker
1735
- else {
1744
+ if (serviceWorkerFinalizer) {
1736
1745
  let code = buildFileContents[serviceWorkerBuildRelativeUrl]
1737
1746
  code = await serviceWorkerFinalizer(code, {
1738
1747
  serviceWorkerBuildRelativeUrl,