@jsenv/core 25.1.0 → 25.3.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 (58) hide show
  1. package/dist/browser_runtime/browser_runtime_91c5a3b8.js.map +2 -2
  2. package/dist/build_manifest.js +4 -4
  3. package/dist/compile_proxy/asset-manifest.json +2 -2
  4. package/dist/compile_proxy/{compile_proxy_e3b0c442_809f35f7.js.map → compile_proxy.html__inline__20_809f35f7.js.map} +0 -0
  5. package/dist/compile_proxy/{compile_proxy_7ad5faa6.html → compile_proxy_8dfaee51.html} +3 -4
  6. package/dist/redirector/asset-manifest.json +2 -2
  7. package/dist/redirector/{redirector_e3b0c442_e391410e.js.map → redirector.html__inline__15_e391410e.js.map} +0 -0
  8. package/dist/redirector/{redirector_eb92e8a7.html → redirector_3e9a97b9.html} +3 -4
  9. package/dist/toolbar/asset-manifest.json +1 -1
  10. package/dist/toolbar/{toolbar_f7b8a263.html → toolbar_361afb84.html} +2 -3
  11. package/dist/toolbar_injector/asset-manifest.json +2 -2
  12. package/dist/toolbar_injector/{toolbar_injector_49e4756e.js → toolbar_injector_fac1e995.js} +2 -2
  13. package/dist/toolbar_injector/{toolbar_injector_49e4756e.js.map → toolbar_injector_fac1e995.js.map} +2 -2
  14. package/package.json +13 -9
  15. package/readme.md +75 -76
  16. package/src/buildProject.js +21 -13
  17. package/src/commonJsToJavaScriptModule.js +8 -7
  18. package/src/dev_server.js +2 -0
  19. package/src/execute.js +2 -0
  20. package/src/executeTestPlan.js +14 -0
  21. package/src/internal/building/buildUsingRollup.js +4 -2
  22. package/src/internal/building/build_stats.js +3 -0
  23. package/src/internal/building/build_url_generator.js +153 -0
  24. package/src/internal/building/css/parseCssRessource.js +32 -26
  25. package/src/internal/building/html/parseHtmlRessource.js +110 -91
  26. package/src/internal/building/js/parseJsRessource.js +4 -7
  27. package/src/internal/building/parseRessource.js +3 -0
  28. package/src/internal/building/ressource_builder.js +64 -62
  29. package/src/internal/building/ressource_builder_util.js +17 -5
  30. package/src/internal/building/rollup_plugin_jsenv.js +259 -189
  31. package/src/internal/building/url_fetcher.js +16 -7
  32. package/src/internal/building/url_loader.js +1 -5
  33. package/src/internal/building/url_versioning.js +0 -173
  34. package/src/internal/compiling/babel_plugin_import_metadata.js +7 -11
  35. package/src/internal/compiling/babel_plugin_proxy_external_imports.js +31 -0
  36. package/src/internal/compiling/compile-directory/compile-asset.js +8 -4
  37. package/src/internal/compiling/compile-directory/getOrGenerateCompiledFile.js +43 -8
  38. package/src/internal/compiling/compile-directory/updateMeta.js +4 -8
  39. package/src/internal/compiling/compile-directory/validateCache.js +1 -2
  40. package/src/internal/compiling/compileFile.js +22 -10
  41. package/src/internal/compiling/compileHtml.js +15 -28
  42. package/src/internal/compiling/createCompiledFileService.js +22 -24
  43. package/src/internal/compiling/html_source_file_service.js +18 -19
  44. package/src/internal/compiling/js-compilation-service/babelHelper.js +10 -13
  45. package/src/internal/compiling/js-compilation-service/babel_plugin_babel_helpers_as_jsenv_imports.js +4 -2
  46. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +16 -7
  47. package/src/internal/compiling/js-compilation-service/transformJs.js +9 -5
  48. package/src/internal/compiling/jsenvCompilerForHtml.js +231 -195
  49. package/src/internal/compiling/jsenvCompilerForJavaScript.js +15 -11
  50. package/src/internal/compiling/startCompileServer.js +79 -19
  51. package/src/internal/compiling/transformResultToCompilationResult.js +47 -25
  52. package/src/internal/executing/executePlan.js +2 -0
  53. package/src/internal/fetchUrl.js +3 -2
  54. package/src/internal/jsenv_remote_directory.js +156 -0
  55. package/src/internal/origin_directory_converter.js +62 -0
  56. package/src/internal/response_validation.js +11 -24
  57. package/src/internal/sourceMappingURLUtils.js +10 -0
  58. package/src/internal/url_conversion.js +1 -0
@@ -30,6 +30,8 @@ import { jsenvHelpersDirectoryInfo } from "@jsenv/core/src/internal/jsenvInterna
30
30
  import { createImportResolverForNode } from "@jsenv/core/src/internal/import-resolution/import-resolver-node.js"
31
31
  import { createImportResolverForImportmap } from "@jsenv/core/src/internal/import-resolution/import-resolver-importmap.js"
32
32
  import { getDefaultImportmap } from "@jsenv/core/src/internal/import-resolution/importmap_default.js"
33
+ import { createJsenvRemoteDirectory } from "@jsenv/core/src/internal/jsenv_remote_directory.js"
34
+ import { setUrlSearchParamsDescriptor } from "@jsenv/core/src/internal/url_utils.js"
33
35
 
34
36
  import {
35
37
  formatBuildStartLog,
@@ -44,10 +46,8 @@ import {
44
46
  createRessourceBuilder,
45
47
  referenceToCodeForRollup,
46
48
  } from "./ressource_builder.js"
47
-
48
- import { createUrlVersioner } from "./url_versioning.js"
49
+ import { createBuildUrlGenerator } from "./build_url_generator.js"
49
50
  import { visitImportReferences } from "./import_references.js"
50
-
51
51
  import { createBuildStats } from "./build_stats.js"
52
52
 
53
53
  export const createRollupPlugins = async ({
@@ -58,6 +58,7 @@ export const createRollupPlugins = async ({
58
58
  entryPoints,
59
59
  compileServerOrigin,
60
60
  compileDirectoryRelativeUrl,
61
+ jsenvDirectoryRelativeUrl,
61
62
  buildDirectoryUrl,
62
63
 
63
64
  urlMappings,
@@ -65,8 +66,8 @@ export const createRollupPlugins = async ({
65
66
  importMapFileRelativeUrl,
66
67
  importDefaultExtension,
67
68
  externalImportSpecifiers,
68
- externalImportUrlPatterns,
69
69
  importPaths,
70
+ preservedUrls,
70
71
  workers,
71
72
  serviceWorkers,
72
73
  serviceWorkerFinalizer,
@@ -90,6 +91,12 @@ export const createRollupPlugins = async ({
90
91
  minifyCssOptions,
91
92
  minifyHtmlOptions,
92
93
  }) => {
94
+ const jsenvRemoteDirectory = createJsenvRemoteDirectory({
95
+ projectDirectoryUrl,
96
+ jsenvDirectoryRelativeUrl,
97
+ preservedUrls,
98
+ })
99
+
93
100
  const urlImporterMap = {}
94
101
  const inlineModuleScripts = {}
95
102
  const jsModulesFromEntry = {}
@@ -133,6 +140,7 @@ export const createRollupPlugins = async ({
133
140
  const rollupGetModuleInfo = (id) => _rollupGetModuleInfo(id)
134
141
 
135
142
  const {
143
+ compileServerOriginForRollup,
136
144
  asRollupUrl,
137
145
  asProjectUrl,
138
146
  asServerUrl,
@@ -147,13 +155,14 @@ export const createRollupPlugins = async ({
147
155
  urlMappings,
148
156
  })
149
157
 
150
- const urlVersioner = createUrlVersioner({
158
+ const buildUrlGenerator = createBuildUrlGenerator({
151
159
  entryPointUrls,
152
160
  asOriginalUrl,
153
161
  lineBreakNormalization,
154
162
  })
155
163
 
156
164
  const urlFetcher = createUrlFetcher({
165
+ jsenvRemoteDirectory,
157
166
  asOriginalUrl,
158
167
  asProjectUrl,
159
168
  applyUrlMappings,
@@ -168,19 +177,18 @@ export const createRollupPlugins = async ({
168
177
  urlCustomLoaders,
169
178
  allowJson: acceptsJsonContentType({ node, format }),
170
179
  urlImporterMap,
171
- jsConcatenation,
172
180
 
173
- asServerUrl,
174
181
  asProjectUrl,
175
182
  asOriginalUrl,
176
183
 
177
184
  urlFetcher,
178
185
  })
179
186
 
180
- const externalUrlPredicate = externalImportUrlPatternsToExternalUrlPredicate(
181
- externalImportUrlPatterns,
187
+ const urlMetaGetter = createUrlMetaGetter({
182
188
  projectDirectoryUrl,
183
- )
189
+ jsenvRemoteDirectory,
190
+ preservedUrls,
191
+ })
184
192
 
185
193
  // Object mapping project relative urls to build relative urls
186
194
  let buildMappings = {}
@@ -256,11 +264,12 @@ export const createRollupPlugins = async ({
256
264
  async renderChunk(code, chunk) {
257
265
  let map = chunk.map
258
266
  const result = await transformJs({
259
- code,
267
+ projectDirectoryUrl,
268
+ jsenvRemoteDirectory,
260
269
  url: chunk.facadeModuleId
261
270
  ? asOriginalUrl(chunk.facadeModuleId)
262
271
  : resolveUrl(chunk.fileName, buildDirectoryUrl),
263
- projectDirectoryUrl,
272
+ code,
264
273
  babelPluginMap: {
265
274
  "transform-async-to-promises":
266
275
  babelPluginMap["transform-async-to-promises"],
@@ -530,6 +539,7 @@ export const createRollupPlugins = async ({
530
539
  format,
531
540
  systemJsUrl,
532
541
  projectDirectoryUrl,
542
+ jsenvRemoteDirectory,
533
543
  asProjectUrl,
534
544
  asOriginalUrl,
535
545
  asOriginalServerUrl,
@@ -557,7 +567,10 @@ export const createRollupPlugins = async ({
557
567
  urlToCompiledServerUrl: (url) => {
558
568
  return asCompiledServerUrl(url)
559
569
  },
560
- urlToHumanUrl: (url) => {
570
+ urlToHumanUrl: (
571
+ url,
572
+ { showCompiledHint = false, preferRelativeUrls = false } = {},
573
+ ) => {
561
574
  if (
562
575
  !url.startsWith("http:") &&
563
576
  !url.startsWith("https:") &&
@@ -565,7 +578,22 @@ export const createRollupPlugins = async ({
565
578
  ) {
566
579
  return url
567
580
  }
568
- return asOriginalUrl(url) || url
581
+ const originalUrl = asOriginalUrl(url)
582
+ const isCompiled = urlIsInsideOf(url, compileServerOrigin)
583
+ const originalRelativeUrl = urlToRelativeUrl(
584
+ originalUrl,
585
+ projectDirectoryUrl,
586
+ )
587
+ if (showCompiledHint && isCompiled) {
588
+ if (preferRelativeUrls) {
589
+ return `${originalRelativeUrl}[compiled]`
590
+ }
591
+ return `${originalUrl}[compiled]`
592
+ }
593
+ if (preferRelativeUrls) {
594
+ return originalRelativeUrl
595
+ }
596
+ return originalUrl
569
597
  },
570
598
  resolveRessourceUrl: ({
571
599
  ressourceSpecifier,
@@ -573,87 +601,85 @@ export const createRollupPlugins = async ({
573
601
  // isRessourceHint,
574
602
  ressourceImporter,
575
603
  }) => {
576
- // Entry point is not a JS module and references a js module (html referencing js)
577
- if (
578
- ressourceImporter.isEntryPoint &&
579
- !ressourceImporter.isJsModule &&
580
- isJsModule
581
- ) {
582
- const importerCompiledUrl = asCompiledServerUrl(
583
- ressourceImporter.url,
584
- )
585
- const jsModuleUrl = resolveUrl(
586
- ressourceSpecifier,
587
- importerCompiledUrl,
588
- )
589
- return jsModuleUrl
590
- }
591
-
592
- let ressourceUrl
593
- if (
594
- ressourceImporter.isEntryPoint &&
595
- !ressourceImporter.isJsModule
596
- ) {
597
- // Entry point (likely html, unlikely css) is referecing a ressource
598
- // when importmap, parse the original importmap ressource
599
- if (ressourceSpecifier.endsWith(".importmap")) {
600
- ressourceUrl = resolveUrl(
601
- ressourceSpecifier,
604
+ const getRessourceUrl = () => {
605
+ // Entry point is not a JS module and references a js module (html referencing js)
606
+ if (
607
+ ressourceImporter.isEntryPoint &&
608
+ !ressourceImporter.isJsModule &&
609
+ isJsModule
610
+ ) {
611
+ const importerCompiledUrl = asCompiledServerUrl(
602
612
  ressourceImporter.url,
603
613
  )
604
- } else {
614
+ const jsModuleUrl = resolveUrl(
615
+ ressourceSpecifier,
616
+ importerCompiledUrl,
617
+ )
618
+ return jsModuleUrl
619
+ }
620
+ // Entry point (likely html, unlikely css) is referecing a ressource
621
+ // In this situation:
622
+ // - when importmap is referenced: parse the original importmap ressource
623
+ // - other ressource: force compilation (because the HTML url is the original url)
624
+ if (
625
+ ressourceImporter.isEntryPoint &&
626
+ !ressourceImporter.isJsModule
627
+ ) {
628
+ if (ressourceSpecifier.endsWith(".importmap")) {
629
+ const importmapUrl = resolveUrl(
630
+ ressourceSpecifier,
631
+ ressourceImporter.url,
632
+ )
633
+ return importmapUrl
634
+ }
605
635
  const importerCompiled = asCompiledServerUrl(
606
636
  ressourceImporter.url,
607
637
  )
608
- ressourceUrl = resolveUrl(ressourceSpecifier, importerCompiled)
638
+ const ressourceCompiledUrl = resolveUrl(
639
+ ressourceSpecifier,
640
+ importerCompiled,
641
+ )
642
+ return ressourceCompiledUrl
609
643
  }
610
- } else {
611
- ressourceUrl = resolveUrl(
644
+ const ressourceCompiledUrl = resolveUrl(
612
645
  ressourceSpecifier,
613
646
  ressourceImporter.url,
614
647
  )
648
+ return ressourceCompiledUrl
615
649
  }
650
+ const ressourceUrl = getRessourceUrl()
651
+ const resolutionResult = { url: ressourceUrl }
652
+ const ressourceOriginalUrl =
653
+ asOriginalUrl(ressourceUrl) || ressourceUrl
616
654
 
617
- // ignore url outside project directory
618
- // a better version would console.warn about file url outside projectDirectoryUrl
619
- // and ignore them and console.info/debug about remote url (https, http, ...)
620
- const projectUrl = asProjectUrl(ressourceUrl)
621
- if (!projectUrl) {
622
- return {
623
- isExternal: true,
624
- url: ressourceUrl,
625
- }
655
+ if (!urlIsInsideOf(ressourceUrl, compileServerOrigin)) {
656
+ resolutionResult.isCrossOrigin = true
626
657
  }
627
658
 
628
- const originalUrl = asOriginalUrl(projectUrl)
629
- if (workerUrls.includes(originalUrl)) {
630
- return {
631
- isWorker: true,
632
- isJsModule: true,
633
- url: ressourceUrl,
634
- }
635
- }
636
- if (serviceWorkerUrls.includes(originalUrl)) {
637
- return {
638
- isServiceWorker: true,
639
- isJsModule: true,
640
- url: ressourceUrl,
641
- }
642
- }
643
- if (classicWorkerUrls.includes(originalUrl)) {
644
- return {
645
- isWorker: true,
646
- url: ressourceUrl,
647
- }
648
- }
649
- if (classicServiceWorkerUrls.includes(originalUrl)) {
650
- return {
651
- isServiceWorker: true,
652
- url: ressourceUrl,
653
- }
659
+ const urlMeta = urlMetaGetter(ressourceOriginalUrl)
660
+ if (urlMeta.preserve) {
661
+ resolutionResult.isExternal = true
662
+ } else if (jsenvRemoteDirectory.isRemoteUrl(ressourceOriginalUrl)) {
663
+ const fileUrl =
664
+ jsenvRemoteDirectory.fileUrlFromRemoteUrl(ressourceOriginalUrl)
665
+ const compiledFileUrl = asCompiledServerUrl(fileUrl)
666
+ resolutionResult.url = compiledFileUrl
654
667
  }
655
668
 
656
- return ressourceUrl
669
+ if (workerUrls.includes(ressourceOriginalUrl)) {
670
+ resolutionResult.isWorker = true
671
+ resolutionResult.isJsModule = true
672
+ } else if (serviceWorkerUrls.includes(ressourceOriginalUrl)) {
673
+ resolutionResult.isServiceWorker = true
674
+ resolutionResult.isJsModule = true
675
+ } else if (classicWorkerUrls.includes(ressourceOriginalUrl)) {
676
+ resolutionResult.isWorker = true
677
+ } else if (
678
+ classicServiceWorkerUrls.includes(ressourceOriginalUrl)
679
+ ) {
680
+ resolutionResult.isServiceWorker = true
681
+ }
682
+ return resolutionResult
657
683
  },
658
684
  onJsModule: ({ ressource, jsModuleUrl, jsModuleIsInline }) => {
659
685
  // we want to emit chunk only when ressource is referenced by something else than rollup
@@ -663,7 +689,6 @@ export const createRollupPlugins = async ({
663
689
  ) {
664
690
  return null
665
691
  }
666
-
667
692
  if (ressource.isEntryPoint) {
668
693
  } else {
669
694
  const importerUrl = resolveUrl(
@@ -679,12 +704,12 @@ export const createRollupPlugins = async ({
679
704
  if (jsModuleIsInline) {
680
705
  inlineModuleScripts[jsModuleUrl] = ressource
681
706
  urlCustomLoaders[jsModuleUrl] = async () => {
707
+ const url = asOriginalUrl(jsModuleUrl) // transformJs expect a file:// url
708
+ let code = String(ressource.bufferBeforeBuild)
682
709
  const transformResult = await transformJs({
683
- code: String(
684
- inlineModuleScripts[jsModuleUrl].bufferBeforeBuild,
685
- ),
686
- url: asOriginalUrl(jsModuleUrl), // transformJs expect a file:// url
687
710
  projectDirectoryUrl,
711
+ jsenvRemoteDirectory,
712
+ url,
688
713
  babelPluginMap,
689
714
  // moduleOutFormat: format // we are compiling for rollup output must be "esmodule"
690
715
  // we compile for rollup, let top level await untouched
@@ -693,29 +718,34 @@ export const createRollupPlugins = async ({
693
718
  // if we put babel helpers, rollup might try to share them and a file
694
719
  // might try to import from an inline script resulting in 404.
695
720
  babelHelpersInjectionAsImport: false,
721
+ code,
696
722
  })
697
- let code = transformResult.code
698
- let map = transformResult.map
699
723
  return {
700
- code,
701
- map,
724
+ code: transformResult.code,
725
+ map: transformResult.map,
702
726
  }
703
727
  }
704
728
  }
705
729
  }
706
-
707
- const fileName = ressource.relativeUrl
730
+ const fileName = ressource.buildRelativeUrlWithoutHash
731
+ const name = urlToBasename(resolveUrl(fileName, "file://"))
708
732
  const rollupReferenceId = emitChunk({
709
733
  id: jsModuleUrl,
710
- name: urlToBasename(jsModuleUrl),
734
+ name,
735
+ ...(useImportMapToMaximizeCacheReuse && !ressource.isInline
736
+ ? { fileName }
737
+ : {}),
711
738
  })
712
739
  return {
713
740
  rollupReferenceId,
714
- fileName,
741
+ fileName:
742
+ useImportMapToMaximizeCacheReuse && !ressource.isInline
743
+ ? fileName
744
+ : name,
715
745
  }
716
746
  },
717
747
  onAsset: ({ ressource }) => {
718
- const fileName = ressource.relativeUrl
748
+ const fileName = ressource.buildRelativeUrlWithoutHash
719
749
  const rollupReferenceId = emitAsset({
720
750
  fileName,
721
751
  })
@@ -730,7 +760,7 @@ export const createRollupPlugins = async ({
730
760
  ressource.bufferAfterBuild,
731
761
  )
732
762
  },
733
- urlVersioner,
763
+ buildUrlGenerator,
734
764
  },
735
765
  )
736
766
 
@@ -822,13 +852,13 @@ export const createRollupPlugins = async ({
822
852
  return asRollupUrl(specifier)
823
853
  }
824
854
 
825
- const importUrl = await importResolver.resolveImport(
855
+ const specifierUrl = await importResolver.resolveImport(
826
856
  specifier,
827
857
  importerUrl,
828
858
  )
829
- const existingImporter = urlImporterMap[importUrl]
859
+ const existingImporter = urlImporterMap[specifierUrl]
830
860
  if (!existingImporter) {
831
- urlImporterMap[importUrl] = importAssertionInfo
861
+ urlImporterMap[specifierUrl] = importAssertionInfo
832
862
  ? {
833
863
  url: importerUrl,
834
864
  column: importAssertionInfo.column,
@@ -842,37 +872,41 @@ export const createRollupPlugins = async ({
842
872
  line: undefined,
843
873
  }
844
874
  }
845
-
846
875
  // keep external url intact
847
- const importProjectUrl = asProjectUrl(importUrl)
848
- if (!importProjectUrl) {
876
+ const specifierProjectUrl = asProjectUrl(specifierUrl)
877
+ if (!specifierProjectUrl) {
849
878
  onExternal({
850
879
  specifier,
851
880
  reason: `outside project directory`,
852
881
  })
853
882
  return { id: specifier, external: true }
854
883
  }
855
-
856
- if (externalUrlPredicate(asOriginalUrl(importProjectUrl))) {
884
+ const specifierOriginalUrl = asOriginalUrl(specifierProjectUrl)
885
+ const urlMeta = urlMetaGetter(specifierOriginalUrl)
886
+ if (urlMeta.preserve) {
887
+ if (urlMeta.remote) {
888
+ specifier =
889
+ jsenvRemoteDirectory.remoteUrlFromFileUrl(specifierOriginalUrl)
890
+ }
857
891
  onExternal({
858
892
  specifier,
859
- reason: `matches "externalUrlPredicate"`,
893
+ reason: `matches "preservedUrls"`,
860
894
  })
861
895
  return { id: specifier, external: true }
862
896
  }
863
-
864
897
  // const rollupId = urlToRollupId(importUrl, { projectDirectoryUrl, compileServerOrigin })
865
898
  // logger.debug(`${specifier} imported by ${importer} resolved to ${importUrl}`)
866
- return asRollupUrl(importUrl)
899
+ const specifierRollupUrl = asRollupUrl(specifierUrl)
900
+ return specifierRollupUrl
867
901
  },
868
902
 
869
903
  resolveFileUrl: ({ referenceId, fileName }) => {
870
904
  ressourcesReferencedByJs.push(fileName)
871
- const getBuildRelativeUrl = () => {
905
+ const getAssetRelativeUrl = () => {
872
906
  const ressource = ressourceBuilder.findRessource((ressource) => {
873
907
  return ressource.rollupReferenceId === referenceId
874
908
  })
875
- ressource.fileName = fileName
909
+ ressource.buildRelativeUrlWithoutHash = fileName
876
910
  const buildRelativeUrl = ressource.buildRelativeUrl
877
911
  return buildRelativeUrl
878
912
  }
@@ -880,16 +914,16 @@ export const createRollupPlugins = async ({
880
914
  if (!node && useImportMapToMaximizeCacheReuse && urlVersioning) {
881
915
  return `window.__resolveImportUrl__("./${fileName}", import.meta.url)`
882
916
  }
883
- return `new URL("${getBuildRelativeUrl()}", import.meta.url)`
917
+ return `new URL("${getAssetRelativeUrl()}", import.meta.url)`
884
918
  }
885
919
  if (format === "systemjs") {
886
920
  return `new URL(System.resolve("./${fileName}", module.meta.url))`
887
921
  }
888
922
  if (format === "global") {
889
- return `new URL("${getBuildRelativeUrl()}", document.currentScript && document.currentScript.src || document.baseURI)`
923
+ return `new URL("${getAssetRelativeUrl()}", document.currentScript && document.currentScript.src || document.baseURI)`
890
924
  }
891
925
  if (format === "commonjs") {
892
- return `new URL("${getBuildRelativeUrl()}", "file:///" + __filename.replace(/\\/g, "/"))`
926
+ return `new URL("${getAssetRelativeUrl()}", "file:///" + __filename.replace(/\\/g, "/"))`
893
927
  }
894
928
  return null
895
929
  },
@@ -903,37 +937,62 @@ export const createRollupPlugins = async ({
903
937
  if (rollupUrl === EMPTY_CHUNK_URL) {
904
938
  return ""
905
939
  }
906
-
907
940
  let url = asServerUrl(rollupUrl)
908
-
909
- const loadResult = await buildOperation.withSignal((signal) => {
910
- return urlLoader.loadUrl(rollupUrl, {
911
- signal,
912
- logger,
913
- })
914
- })
915
-
916
- if (loadResult.url) url = loadResult.url
917
- const code = loadResult.code
918
- const map = loadResult.map
919
-
920
941
  // Jsenv helpers are injected as import statements to provide code like babel helpers
921
942
  // For now we just compute the information that the target file is a jsenv helper
922
943
  // without doing anything special with "targetIsJsenvHelperFile" information
944
+ const projectUrl = asProjectUrl(rollupUrl)
923
945
  const originalUrl = asOriginalUrl(rollupUrl)
924
946
  const isJsenvHelperFile = urlIsInsideOf(
925
947
  originalUrl,
926
948
  jsenvHelpersDirectoryInfo.url,
927
949
  )
928
950
  // const isEntryPoint = entryPointUrls[originalUrl]
929
-
951
+ const loadResult = await buildOperation.withSignal((signal) => {
952
+ let urlToLoad
953
+ const jsModuleRessource = ressourceBuilder.findRessource(
954
+ (ressource) => ressource.url === url,
955
+ )
956
+ if (
957
+ jsModuleRessource &&
958
+ jsModuleRessource.firstStrongReference &&
959
+ jsModuleRessource.firstStrongReference.integrity
960
+ ) {
961
+ urlToLoad = setUrlSearchParamsDescriptor(url, {
962
+ integrity: jsModuleRessource.firstStrongReference.integrity,
963
+ })
964
+ urlImporterMap[urlToLoad] = urlImporterMap[url]
965
+ } else {
966
+ urlToLoad = url
967
+ }
968
+ return urlLoader.loadUrl(urlToLoad, {
969
+ signal,
970
+ logger,
971
+ })
972
+ })
973
+ // if (loadResult.url) url = loadResult.url
974
+ const code = loadResult.code
975
+ const map = loadResult.map
976
+ if (jsenvRemoteDirectory.isFileUrlForRemoteUrl(originalUrl)) {
977
+ map.sources.forEach((source, index) => {
978
+ if (jsenvRemoteDirectory.isRemoteUrl(source)) {
979
+ const sourceFileUrl =
980
+ jsenvRemoteDirectory.fileUrlFromRemoteUrl(source)
981
+ const sourceRelativeUrl = urlToRelativeUrl(
982
+ sourceFileUrl,
983
+ projectUrl,
984
+ )
985
+ map.sources[index] = sourceRelativeUrl
986
+ }
987
+ })
988
+ }
930
989
  const importer = urlImporterMap[url]
931
990
  // Inform ressource builder that this js module exists
932
991
  // It can only be a js module and happens when:
933
992
  // - entry point (html) references js
934
993
  // - js is referenced by static or dynamic imports
935
994
  // For import assertions, the imported ressource (css,json,...)
936
- // is arelady converted to a js module
995
+ // is already converted to a js module
937
996
  ressourceBuilder.createReferenceFoundByRollup({
938
997
  contentTypeExpected: "application/javascript",
939
998
  referenceLabel: "static or dynamic import",
@@ -943,10 +1002,9 @@ export const createRollupPlugins = async ({
943
1002
  ressourceSpecifier: url,
944
1003
  isJsenvHelperFile,
945
1004
  contentType: "application/javascript",
946
- bufferBeforeBuild: Buffer.from(code),
947
1005
  isJsModule: true,
1006
+ bufferBeforeBuild: Buffer.from(code),
948
1007
  })
949
-
950
1008
  return {
951
1009
  code,
952
1010
  map,
@@ -963,7 +1021,6 @@ export const createRollupPlugins = async ({
963
1021
  })
964
1022
  // const moduleInfo = this.getModuleInfo(id)
965
1023
  const url = asServerUrl(id)
966
-
967
1024
  const mutations = []
968
1025
  await visitImportReferences({
969
1026
  ast,
@@ -1203,7 +1260,6 @@ export const createRollupPlugins = async ({
1203
1260
  code = magicString.toString()
1204
1261
  map = magicString.generateMap({ hires: true })
1205
1262
  }
1206
-
1207
1263
  return { code, map }
1208
1264
  },
1209
1265
 
@@ -1231,6 +1287,9 @@ export const createRollupPlugins = async ({
1231
1287
  return entryPoints
1232
1288
  }
1233
1289
  outputOptions.chunkFileNames = (chunkInfo) => {
1290
+ // maybe we should find ressource from ressource builder and return
1291
+ // the buildRelativeUrlPattern?
1292
+
1234
1293
  // const originalUrl = asOriginalUrl(chunkInfo.facadeModuleId)
1235
1294
  // const basename = urlToBasename(originalUrl)
1236
1295
  if (useImportMapToMaximizeCacheReuse) {
@@ -1248,30 +1307,41 @@ export const createRollupPlugins = async ({
1248
1307
  : `[name]${outputExtension}`
1249
1308
  }
1250
1309
 
1310
+ const getStringAfter = (string, substring) => {
1311
+ const index = string.indexOf(substring)
1312
+ if (index === -1) return ""
1313
+ return string.slice(index + substring.length)
1314
+ }
1315
+
1251
1316
  // rollup does not expects to have http dependency in the mix: fix them
1252
1317
  outputOptions.sourcemapPathTransform = (relativePath, sourcemapPath) => {
1253
1318
  const sourcemapUrl = fileSystemPathToUrl(sourcemapPath)
1254
- const url = relativePathToUrl(relativePath, sourcemapUrl)
1255
- const serverUrl = asServerUrl(url)
1256
- const finalUrl =
1257
- urlFetcher.getUrlBeforeRedirection(serverUrl) || serverUrl
1258
- const projectUrl = asProjectUrl(finalUrl)
1259
-
1260
- if (projectUrl) {
1261
- relativePath = urlToRelativeUrl(projectUrl, sourcemapUrl)
1262
- return relativePath
1263
- }
1264
-
1265
- return finalUrl
1266
- }
1267
-
1268
- const relativePathToUrl = (relativePath, sourcemapUrl) => {
1269
- const rollupUrl = resolveUrl(relativePath, sourcemapUrl)
1270
1319
  // here relativePath contains a protocol
1271
1320
  // because rollup don't work with url but with filesystem paths
1272
1321
  // let fix it below
1273
- const url = fixRollupUrl(rollupUrl)
1274
- return url
1322
+ const sourceRollupUrlRaw = resolveUrl(relativePath, sourcemapUrl)
1323
+ const afterOrigin =
1324
+ getStringAfter(sourceRollupUrlRaw, "http:/jsenv.com") ||
1325
+ getStringAfter(sourceRollupUrlRaw, "https:/jsenv.com")
1326
+ const sourceRollupUrl = afterOrigin
1327
+ ? `${compileServerOriginForRollup}${afterOrigin}`
1328
+ : sourceRollupUrlRaw
1329
+
1330
+ const sourceServerUrl = asServerUrl(sourceRollupUrl)
1331
+ const sourceUrl =
1332
+ urlFetcher.getUrlBeforeRedirection(sourceServerUrl) || sourceServerUrl
1333
+ const sourceProjectUrl = asProjectUrl(sourceUrl)
1334
+
1335
+ if (jsenvRemoteDirectory.isFileUrlForRemoteUrl(sourceProjectUrl)) {
1336
+ const remoteUrl =
1337
+ jsenvRemoteDirectory.remoteUrlFromFileUrl(sourceProjectUrl)
1338
+ return remoteUrl
1339
+ }
1340
+ if (sourceProjectUrl) {
1341
+ relativePath = urlToRelativeUrl(sourceProjectUrl, sourcemapUrl)
1342
+ return relativePath
1343
+ }
1344
+ return sourceUrl
1275
1345
  }
1276
1346
 
1277
1347
  return outputOptions
@@ -1382,26 +1452,34 @@ export const createRollupPlugins = async ({
1382
1452
  // }
1383
1453
  } else {
1384
1454
  const originalProjectUrl = asOriginalUrl(ressourceUrl)
1385
- const originalProjectRelativeUrl = urlToRelativeUrl(
1386
- originalProjectUrl,
1387
- projectDirectoryUrl,
1388
- )
1389
- buildMappings[originalProjectRelativeUrl] =
1390
- jsRessource.buildRelativeUrl
1455
+ if (jsenvRemoteDirectory.isFileUrlForRemoteUrl(originalProjectUrl)) {
1456
+ const remoteUrl =
1457
+ jsenvRemoteDirectory.remoteUrlFromFileUrl(originalProjectUrl)
1458
+ buildMappings[remoteUrl] = jsRessource.buildRelativeUrl
1459
+ } else {
1460
+ const originalProjectRelativeUrl = urlToRelativeUrl(
1461
+ originalProjectUrl,
1462
+ projectDirectoryUrl,
1463
+ )
1464
+ buildMappings[originalProjectRelativeUrl] =
1465
+ jsRessource.buildRelativeUrl
1466
+ }
1391
1467
  }
1392
- ressourceMappings[jsRessource.fileName] = jsRessource.buildRelativeUrl
1468
+ ressourceMappings[jsRessource.buildRelativeUrlWithoutHash] =
1469
+ jsRessource.buildRelativeUrl
1393
1470
  })
1394
1471
  // wait for asset build relative urls
1395
1472
  // to ensure the importmap will contain remappings for them
1396
1473
  // (not sure this is required anymore)
1397
1474
  await Promise.all(
1398
- ressourcesReferencedByJs.map(async (ressourceName) => {
1475
+ ressourcesReferencedByJs.map(async (ressourceFileName) => {
1399
1476
  const ressource = ressourceBuilder.findRessource((ressource) => {
1400
- return ressource.relativeUrl === ressourceName
1477
+ return ressource.buildRelativeUrlWithoutHash === ressourceFileName
1401
1478
  })
1402
1479
  if (ressource && !ressource.isJsModule) {
1403
1480
  await ressource.getReadyPromise()
1404
- ressourceMappings[ressourceName] = ressource.buildRelativeUrl
1481
+ ressourceMappings[ressource.buildRelativeUrlWithoutHash] =
1482
+ ressource.buildRelativeUrl
1405
1483
  }
1406
1484
  }),
1407
1485
  )
@@ -1426,7 +1504,7 @@ export const createRollupPlugins = async ({
1426
1504
  const assetRessource = ressourceBuilder.findRessource(
1427
1505
  (ressource) =>
1428
1506
  // happens for import.meta.url pattern
1429
- ressource.fileName === fileName ||
1507
+ ressource.buildRelativeUrlWithoutHash === fileName ||
1430
1508
  // happens for sourcemap
1431
1509
  ressource.relativeUrl === fileName,
1432
1510
  )
@@ -1455,7 +1533,8 @@ export const createRollupPlugins = async ({
1455
1533
  // in case sourcemap is mutated, we must not trust rollup but the asset builder source instead
1456
1534
  rollupFileInfo.source = assetRessource.bufferAfterBuild
1457
1535
  assetBuild[buildRelativeUrl] = rollupFileInfo
1458
- ressourceMappings[assetRessource.fileName] = buildRelativeUrl
1536
+ ressourceMappings[assetRessource.buildRelativeUrlWithoutHash] =
1537
+ buildRelativeUrl
1459
1538
  if (assetRessource.bufferBeforeBuild) {
1460
1539
  const originalProjectUrl = asOriginalUrl(assetRessource.url)
1461
1540
  const originalProjectRelativeUrl = urlToRelativeUrl(
@@ -1646,26 +1725,6 @@ const prepareEntryPoints = async (
1646
1725
  return entryPointsPrepared
1647
1726
  }
1648
1727
 
1649
- const fixRollupUrl = (rollupUrl) => {
1650
- // fix rollup not supporting source being http
1651
- const httpIndex = rollupUrl.indexOf(`http:/`, 1)
1652
- if (httpIndex > -1) {
1653
- return `http://${rollupUrl.slice(httpIndex + `http:/`.length)}`
1654
- }
1655
-
1656
- const httpsIndex = rollupUrl.indexOf("https:/", 1)
1657
- if (httpsIndex > -1) {
1658
- return `https://${rollupUrl.slice(httpsIndex + `https:/`.length)}`
1659
- }
1660
-
1661
- const fileIndex = rollupUrl.indexOf("file:", 1)
1662
- if (fileIndex > -1) {
1663
- return `file://${rollupUrl.slice(fileIndex + `file:`.length)}`
1664
- }
1665
-
1666
- return rollupUrl
1667
- }
1668
-
1669
1728
  const normalizeRollupResolveReturnValue = (resolveReturnValue) => {
1670
1729
  if (resolveReturnValue === null) {
1671
1730
  return { id: null, external: true }
@@ -1677,25 +1736,36 @@ const normalizeRollupResolveReturnValue = (resolveReturnValue) => {
1677
1736
  return resolveReturnValue
1678
1737
  }
1679
1738
 
1680
- const externalImportUrlPatternsToExternalUrlPredicate = (
1681
- externalImportUrlPatterns,
1739
+ const createUrlMetaGetter = ({
1682
1740
  projectDirectoryUrl,
1683
- ) => {
1684
- const externalImportUrlStructuredMetaMap = normalizeStructuredMetaMap(
1741
+ jsenvRemoteDirectory,
1742
+ preservedUrls,
1743
+ }) => {
1744
+ const preservedFileUrls = {}
1745
+ const remoteFileUrls = {}
1746
+ Object.keys(preservedUrls).forEach((pattern) => {
1747
+ if (jsenvRemoteDirectory.isRemoteUrl(pattern)) {
1748
+ const fileUrlPattern = jsenvRemoteDirectory.fileUrlFromRemoteUrl(pattern)
1749
+ preservedFileUrls[fileUrlPattern] = preservedUrls[pattern]
1750
+ remoteFileUrls[fileUrlPattern] = true
1751
+ }
1752
+ })
1753
+ const structuredMetaMap = normalizeStructuredMetaMap(
1685
1754
  {
1686
- external: {
1687
- ...externalImportUrlPatterns,
1688
- "node_modules/@jsenv/core/helpers/": false,
1755
+ preserve: {
1756
+ ...preservedUrls,
1757
+ ...preservedFileUrls,
1689
1758
  },
1759
+ remote: remoteFileUrls,
1690
1760
  },
1691
1761
  projectDirectoryUrl,
1692
1762
  )
1693
1763
  return (url) => {
1694
1764
  const meta = urlToMeta({
1695
1765
  url,
1696
- structuredMetaMap: externalImportUrlStructuredMetaMap,
1766
+ structuredMetaMap,
1697
1767
  })
1698
- return Boolean(meta.external)
1768
+ return meta
1699
1769
  }
1700
1770
  }
1701
1771