@jsenv/core 24.6.3 → 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 (83) hide show
  1. package/dist/browser_runtime/asset-manifest.json +2 -1
  2. package/dist/browser_runtime/{browser_runtime-c7288751.js → browser_runtime_91c5a3b8.js} +213 -109
  3. package/dist/browser_runtime/browser_runtime_91c5a3b8.js.map +1089 -0
  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-28148b58.html → compile_proxy_7ad5faa6.html} +133 -40
  7. package/dist/compile_proxy/{compile_proxy.html__inline__20-4887fb1d.js.map → compile_proxy_e3b0c442_809f35f7.js.map} +7 -7
  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} +6 -12
  10. package/dist/event_source_client/{event_source_client-9f14c8b9.js.map → event_source_client_620fbc2c.js.map} +2 -2
  11. package/dist/redirector/asset-manifest.json +2 -1
  12. package/dist/redirector/{redirector.html__inline__15-19e529b9.js.map → redirector_e3b0c442_e391410e.js.map} +7 -7
  13. package/dist/redirector/{redirector-3c05dbb2.html → redirector_eb92e8a7.html} +133 -40
  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-eb8acf83.js.map → toolbar.main2_6c1b3d82.js.map} +9 -9
  25. package/dist/toolbar/{toolbar-bab473ea.html → toolbar_04ba410c.html} +155 -62
  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-5e32f96a.js → toolbar_injector_4a48bc53.js} +25 -26
  29. package/dist/toolbar_injector/{toolbar_injector-5e32f96a.js.map → toolbar_injector_4a48bc53.js.map} +4 -5
  30. package/package.json +6 -5
  31. package/readme.md +34 -111
  32. package/src/buildProject.js +45 -32
  33. package/src/dev_server.js +13 -3
  34. package/src/execute.js +7 -1
  35. package/src/executeTestPlan.js +6 -0
  36. package/src/internal/browser_feature_detection/browser_feature_detection.js +18 -25
  37. package/src/internal/browser_runtime/browser_runtime.js +2 -2
  38. package/src/internal/browser_runtime/createBrowserRuntime.js +1 -1
  39. package/src/internal/browser_runtime/displayErrorInDocument.js +2 -0
  40. package/src/internal/browser_runtime/displayErrorNotification.js +1 -1
  41. package/src/internal/building/buildUsingRollup.js +12 -17
  42. package/src/internal/building/build_logs.js +2 -2
  43. package/src/internal/building/build_stats.js +11 -1
  44. package/src/internal/building/html/parseHtmlRessource.js +2 -26
  45. package/src/internal/building/js/parseJsRessource.js +3 -2
  46. package/src/internal/building/json_module.js +11 -0
  47. package/src/internal/building/parseRessource.js +1 -1
  48. package/src/internal/building/ressource_builder.js +210 -216
  49. package/src/internal/building/rollup_plugin_jsenv.js +536 -371
  50. package/src/internal/building/url_loader.js +8 -142
  51. package/src/internal/building/url_versioning.js +220 -0
  52. package/src/internal/compiling/compileHtml.js +8 -1
  53. package/src/internal/compiling/createCompiledFileService.js +32 -44
  54. package/src/internal/compiling/html_source_file_service.js +66 -51
  55. package/src/internal/compiling/js-compilation-service/babel_plugin_systemjs_prepend.js +23 -0
  56. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +21 -63
  57. package/src/internal/compiling/js-compilation-service/transformJs.js +4 -4
  58. package/src/internal/compiling/jsenvCompilerForHtml.js +47 -48
  59. package/src/internal/compiling/jsenvCompilerForImportmap.js +15 -76
  60. package/src/internal/compiling/jsenvCompilerForJavaScript.js +11 -2
  61. package/src/internal/compiling/startCompileServer.js +31 -7
  62. package/src/internal/dev_server/toolbar/compilation/toolbar.compilation.js +9 -9
  63. package/src/internal/executing/executePlan.js +6 -0
  64. package/src/internal/generateGroupMap/{jsenvBabelPluginCompatMap.js → babel_plugins_compatibility.js} +0 -0
  65. package/src/internal/generateGroupMap/{featuresCompatMap.js → features_compatibility.js} +9 -1
  66. package/src/internal/generateGroupMap/generateGroupMap.js +6 -35
  67. package/src/internal/generateGroupMap/one_runtime_compat.js +9 -12
  68. package/src/internal/generateGroupMap/runtime_compat.js +10 -15
  69. package/src/internal/generateGroupMap/runtime_compat_composition.js +2 -2
  70. package/src/internal/generateGroupMap/shake_babel_plugin_map.js +21 -0
  71. package/src/internal/import-resolution/importmap_default.js +52 -0
  72. package/src/internal/node_feature_detection/node_feature_detection.js +25 -19
  73. package/src/internal/runtime/s.js +101 -6
  74. package/src/internal/unevalException.js +1 -1
  75. package/src/jsenvServiceWorkerFinalizer.js +10 -9
  76. package/dist/browser_runtime/browser_runtime-c7288751.js.map +0 -1069
  77. package/src/internal/building/asset_url_versioning.js +0 -50
  78. package/src/internal/building/rollup_build_sourcemap.js +0 -54
  79. package/src/internal/building/url-versioning.js +0 -96
  80. package/src/internal/compiling/js-compilation-service/findAsyncPluginNameInBabelPluginMap.js +0 -9
  81. package/src/internal/generateGroupMap/jsenvPluginCompatMap.js +0 -1
  82. package/src/internal/import-resolution/importmap-default.js +0 -34
  83. package/src/internal/renderNamePattern.js +0 -6
@@ -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,
@@ -95,10 +95,13 @@ export const startCompileServer = async ({
95
95
  babelPluginMap,
96
96
  babelConfigFileUrl,
97
97
  customCompilers = {},
98
-
99
- // remaining options
98
+ workers = [],
99
+ serviceWorkers = [],
100
+ importMapInWebWorkers = false,
101
+ prependSystemJs,
100
102
  runtimeSupport,
101
103
 
104
+ // remaining options
102
105
  livereloadWatchConfig = {
103
106
  "./**": true,
104
107
  "./**/.*/": false, // any folder starting with a dot is ignored (includes .git for instance)
@@ -138,9 +141,14 @@ export const startCompileServer = async ({
138
141
  jsenvDirectoryUrl,
139
142
  projectDirectoryUrl,
140
143
  )
141
-
142
144
  const logger = createLogger({ logLevel })
143
145
 
146
+ const workerUrls = workers.map((worker) =>
147
+ resolveUrl(worker, projectDirectoryUrl),
148
+ )
149
+ const serviceWorkerUrls = serviceWorkers.map((serviceWorker) =>
150
+ resolveUrl(serviceWorker, projectDirectoryUrl),
151
+ )
144
152
  const browser = isBrowserPartOfSupportedRuntimes(runtimeSupport)
145
153
  const babelPluginMapFromFile = await loadBabelPluginMapFromFile({
146
154
  projectDirectoryUrl,
@@ -177,7 +185,19 @@ export const startCompileServer = async ({
177
185
  const { babelSyntaxPluginMap, babelPluginMapWithoutSyntax } =
178
186
  extractSyntaxBabelPluginMap(babelPluginMap)
179
187
  const compileServerGroupMap = generateGroupMap({
180
- babelPluginMap: babelPluginMapWithoutSyntax,
188
+ featureNames: [
189
+ ...(browser
190
+ ? [
191
+ "module",
192
+ "importmap",
193
+ "import_assertion_type_json",
194
+ "import_assertion_type_css",
195
+ ]
196
+ : []),
197
+ ...(browser && workerUrls.length > 0 ? ["worker_type_module"] : []),
198
+ ...(browser && importMapInWebWorkers ? ["worker_importmap"] : []),
199
+ ...Object.keys(babelPluginMapWithoutSyntax),
200
+ ],
181
201
  runtimeSupport,
182
202
  })
183
203
 
@@ -188,7 +208,7 @@ export const startCompileServer = async ({
188
208
  // It happens for module written in ESM but also using process.env.NODE_ENV
189
209
  // for example "react-redux"
190
210
  // This babel plugin won't force compilation because it's added after "generateGroupMap"
191
- // however it will be used even if not part of "pluginRequiredNameArray"
211
+ // however it will be used even if not part of "missingFeatureNames"
192
212
  // as visible in "babelPluginMapFromCompileId"
193
213
  // This is a quick workaround to get things working because:
194
214
  // - If none of your code needs to be compiled but one of your dependency
@@ -310,10 +330,14 @@ export const startCompileServer = async ({
310
330
  importDefaultExtension,
311
331
 
312
332
  runtimeSupport,
313
- transformTopLevelAwait,
333
+ topLevelAwait,
314
334
  groupMap: compileServerGroupMap,
315
335
  babelPluginMap,
316
336
  customCompilers,
337
+ workerUrls,
338
+ serviceWorkerUrls,
339
+ importMapInWebWorkers,
340
+ prependSystemJs,
317
341
  moduleOutFormat,
318
342
  importMetaFormat,
319
343
  jsenvEventSourceClientInjection,
@@ -14,7 +14,7 @@ export const renderCompilationInToolbar = ({ compileGroup }) => {
14
14
  canAvoidCompilation,
15
15
  featuresReport,
16
16
  customCompilerPatterns,
17
- pluginRequiredNameArray,
17
+ missingFeatureNames,
18
18
  inlineImportMapIntoHTML,
19
19
  outDirectoryRelativeUrl,
20
20
  compileId,
@@ -38,7 +38,7 @@ export const renderCompilationInToolbar = ({ compileGroup }) => {
38
38
  missingOnly: true,
39
39
  featuresReport,
40
40
  customCompilerPatterns,
41
- pluginRequiredNameArray,
41
+ missingFeatureNames,
42
42
  inlineImportMapIntoHTML,
43
43
  },
44
44
  )}`,
@@ -54,7 +54,7 @@ export const renderCompilationInToolbar = ({ compileGroup }) => {
54
54
  {
55
55
  featuresReport,
56
56
  customCompilerPatterns,
57
- pluginRequiredNameArray,
57
+ missingFeatureNames,
58
58
  inlineImportMapIntoHTML,
59
59
  },
60
60
  )}`,
@@ -70,7 +70,7 @@ export const renderCompilationInToolbar = ({ compileGroup }) => {
70
70
  {
71
71
  featuresReport,
72
72
  customCompilerPatterns,
73
- pluginRequiredNameArray,
73
+ missingFeatureNames,
74
74
  inlineImportMapIntoHTML,
75
75
  },
76
76
  )}`,
@@ -128,7 +128,7 @@ const getBrowserSupportMessage = ({
128
128
  missingOnly,
129
129
  featuresReport,
130
130
  customCompilerPatterns,
131
- pluginRequiredNameArray,
131
+ missingFeatureNames,
132
132
  inlineImportMapIntoHTML,
133
133
  }) => {
134
134
  const parts = []
@@ -161,14 +161,14 @@ const getBrowserSupportMessage = ({
161
161
  parts.push(`top level await is not supported`)
162
162
  }
163
163
 
164
- const pluginRequiredCount = pluginRequiredNameArray.length
165
- if (pluginRequiredCount === 0) {
164
+ const missingFeatureCount = missingFeatureNames.length
165
+ if (missingFeatureCount === 0) {
166
166
  if (!missingOnly) {
167
- parts.push(`all plugins are natively supported`)
167
+ parts.push(`all features are natively supported`)
168
168
  }
169
169
  } else {
170
170
  parts.push(
171
- `${pluginRequiredCount} plugins are mandatory: ${pluginRequiredNameArray}`,
171
+ `${missingFeatureCount} features are missing: ${missingFeatureNames}`,
172
172
  )
173
173
  }
174
174
 
@@ -51,6 +51,9 @@ export const executePlan = async (
51
51
  compileServerCanWriteOnFilesystem,
52
52
  babelPluginMap,
53
53
  babelConfigFileUrl,
54
+ workers,
55
+ serviceWorkers,
56
+ importMapInWebWorkers,
54
57
  customCompilers,
55
58
  } = {},
56
59
  ) => {
@@ -123,6 +126,9 @@ export const executePlan = async (
123
126
  keepProcessAlive: true, // to be sure it stays alive
124
127
  babelPluginMap,
125
128
  babelConfigFileUrl,
129
+ workers,
130
+ serviceWorkers,
131
+ importMapInWebWorkers,
126
132
  customCompilers,
127
133
  runtimeSupport,
128
134
  })
@@ -1,4 +1,4 @@
1
- // https://github.com/babel/babel/blob/master/packages/babel-compat-data/data/native-modules.json#L1
1
+ import { jsenvBabelPluginCompatMap } from "./babel_plugins_compatibility.js"
2
2
 
3
3
  export const featuresCompatMap = {
4
4
  module: {
@@ -26,4 +26,12 @@ export const featuresCompatMap = {
26
26
  chrome: "93",
27
27
  edge: "93",
28
28
  },
29
+ worker_type_module: {
30
+ chrome: "80",
31
+ edge: "80",
32
+ opera: "67",
33
+ android: "80",
34
+ },
35
+ worker_importmap: {},
36
+ ...jsenvBabelPluginCompatMap,
29
37
  }
@@ -17,7 +17,7 @@
17
17
 
18
18
  {
19
19
  "best": {
20
- "pluginRequiredNameArray" : [
20
+ "missingFeatureNames" : [
21
21
  "transform-block-scoping",
22
22
  ],
23
23
  "minRuntimeVersions": {
@@ -33,40 +33,20 @@ Take chars below to update legends
33
33
  */
34
34
 
35
35
  import { COMPILE_ID_OTHERWISE, COMPILE_ID_BEST } from "../CONSTANTS.js"
36
- import { jsenvBabelPluginCompatMap } from "./jsenvBabelPluginCompatMap.js"
37
- import { jsenvPluginCompatMap } from "./jsenvPluginCompatMap.js"
38
36
  import { createRuntimeCompat } from "./runtime_compat.js"
39
37
 
40
- export const generateGroupMap = ({
41
- babelPluginMap,
42
- runtimeSupport,
43
- babelPluginCompatMap = jsenvBabelPluginCompatMap,
44
- // jsenv plugin are for later, for now, nothing is using them
45
- jsenvPluginMap = {},
46
- }) => {
47
- if (typeof babelPluginMap !== "object") {
48
- throw new TypeError(
49
- `babelPluginMap must be an object, got ${babelPluginMap}`,
50
- )
51
- }
52
- if (typeof jsenvPluginMap !== "object") {
53
- throw new TypeError(
54
- `jsenvPluginMap must be an object, got ${jsenvPluginMap}`,
55
- )
38
+ export const generateGroupMap = ({ featureNames, runtimeSupport }) => {
39
+ if (!Array.isArray(featureNames)) {
40
+ throw new TypeError(`featureNames must be an array, got ${featureNames}`)
56
41
  }
57
42
  if (typeof runtimeSupport !== "object") {
58
43
  throw new TypeError(
59
44
  `runtimeSupport must be an object, got ${runtimeSupport}`,
60
45
  )
61
46
  }
62
-
63
47
  const runtimeNames = Object.keys(runtimeSupport)
64
-
65
48
  const groupWithoutFeature = {
66
- pluginRequiredNameArray: [
67
- ...Object.keys(babelPluginMap),
68
- ...Object.keys(jsenvPluginMap),
69
- ],
49
+ missingFeatureNames: featureNames,
70
50
  minRuntimeVersions: {},
71
51
  }
72
52
  if (runtimeNames.length === 0) {
@@ -74,19 +54,10 @@ export const generateGroupMap = ({
74
54
  [COMPILE_ID_OTHERWISE]: groupWithoutFeature,
75
55
  }
76
56
  }
77
-
78
57
  const runtimeCompat = createRuntimeCompat({
79
58
  runtimeSupport,
80
- pluginMap: {
81
- ...babelPluginMap,
82
- ...jsenvPluginMap,
83
- },
84
- pluginCompatMap: {
85
- ...babelPluginCompatMap,
86
- ...jsenvPluginCompatMap,
87
- },
59
+ featureNames,
88
60
  })
89
-
90
61
  return {
91
62
  [COMPILE_ID_BEST]: runtimeCompat,
92
63
  [COMPILE_ID_OTHERWISE]: groupWithoutFeature,
@@ -3,18 +3,16 @@ import { findHighestVersion } from "../semantic_versioning/index.js"
3
3
  export const createOneRuntimeCompat = ({
4
4
  runtimeName,
5
5
  runtimeVersion,
6
-
7
- pluginMap,
8
- pluginCompatMap,
6
+ featureNames,
7
+ featuresCompatMap,
9
8
  }) => {
10
- const pluginRequiredNameArray = []
9
+ const missingFeatureNames = []
11
10
  // will be the first runtime version compatible with all features not listed in
12
- // pluginRequiredNameArray
11
+ // missingFeatureNames
13
12
  let minRuntimeVersion
14
-
15
- Object.keys(pluginMap).forEach((pluginName) => {
16
- const pluginCompat = pluginCompatMap[pluginName] || {}
17
- const runtimeVersionCompatible = pluginCompat[runtimeName] || "Infinity"
13
+ featureNames.forEach((featureName) => {
14
+ const featureCompat = featuresCompatMap[featureName] || {}
15
+ const runtimeVersionCompatible = featureCompat[runtimeName] || "Infinity"
18
16
 
19
17
  const highestVersion = findHighestVersion(
20
18
  runtimeVersion,
@@ -22,7 +20,7 @@ export const createOneRuntimeCompat = ({
22
20
  )
23
21
  const compatible = highestVersion === runtimeVersion
24
22
  if (!compatible) {
25
- pluginRequiredNameArray.push(pluginName)
23
+ missingFeatureNames.push(featureName)
26
24
  }
27
25
 
28
26
  if (compatible && runtimeVersionCompatible !== "Infinity") {
@@ -33,9 +31,8 @@ export const createOneRuntimeCompat = ({
33
31
  )
34
32
  }
35
33
  })
36
-
37
34
  return {
38
- pluginRequiredNameArray,
35
+ missingFeatureNames,
39
36
  minRuntimeVersion: minRuntimeVersion || runtimeVersion,
40
37
  }
41
38
  }
@@ -1,39 +1,34 @@
1
+ import { featuresCompatMap } from "./features_compatibility.js"
1
2
  import { createOneRuntimeCompat } from "./one_runtime_compat.js"
2
3
 
3
- export const createRuntimeCompat = ({
4
- runtimeSupport,
5
- pluginMap,
6
- pluginCompatMap,
7
- }) => {
4
+ export const createRuntimeCompat = ({ runtimeSupport, featureNames }) => {
8
5
  const minRuntimeVersions = {}
9
- const pluginRequiredNameArray = []
6
+ const missingFeatureNames = []
10
7
  const runtimeNames = Object.keys(runtimeSupport)
11
8
  if (runtimeNames.length === 0) {
12
9
  // when runtimes are unknown, everything is required
13
- Object.keys(pluginMap).forEach((pluginName) => {
14
- pluginRequiredNameArray.push(pluginName)
15
- })
10
+ missingFeatureNames.push(...featureNames)
16
11
  } else {
17
12
  runtimeNames.forEach((runtimeName) => {
18
13
  const runtimeVersion = runtimeSupport[runtimeName]
19
14
  const oneRuntimeCompat = createOneRuntimeCompat({
20
15
  runtimeName,
21
16
  runtimeVersion,
22
- pluginMap,
23
- pluginCompatMap,
17
+ featureNames,
18
+ featuresCompatMap,
24
19
  })
25
20
 
26
21
  minRuntimeVersions[runtimeName] = oneRuntimeCompat.minRuntimeVersion
27
- oneRuntimeCompat.pluginRequiredNameArray.forEach((babelPluginName) => {
28
- if (!pluginRequiredNameArray.includes(babelPluginName)) {
29
- pluginRequiredNameArray.push(babelPluginName)
22
+ oneRuntimeCompat.missingFeatureNames.forEach((missingFeatureName) => {
23
+ if (!missingFeatureNames.includes(missingFeatureName)) {
24
+ missingFeatureNames.push(missingFeatureName)
30
25
  }
31
26
  })
32
27
  })
33
28
  }
34
29
 
35
30
  return {
36
- pluginRequiredNameArray,
31
+ missingFeatureNames,
37
32
  minRuntimeVersions,
38
33
  }
39
34
  }
@@ -31,7 +31,7 @@ const sortObjectKeys = (object) => {
31
31
 
32
32
  // export const composeRuntimeCompat = (...runtimeCompats) => {
33
33
  // return runtimeCompats.reduce(runtimeCompatComposer, {
34
- // pluginRequiredNameArray: [],
34
+ // missingFeatureNames: [],
35
35
  // runtimeCompatMap: {},
36
36
  // })
37
37
  // }
@@ -60,7 +60,7 @@ const sortObjectKeys = (object) => {
60
60
  // }
61
61
 
62
62
  // const runtimeCompatComposer = compositionMappingToStrictReducer({
63
- // pluginRequiredNameArray: (
63
+ // missingFeatureNames: (
64
64
  // babelPluginNamesPrevious,
65
65
  // babelPluginNamesCurrent,
66
66
  // ) => {
@@ -0,0 +1,21 @@
1
+ export const shakeBabelPluginMap = ({
2
+ babelPluginMap,
3
+ missingFeatureNames,
4
+ }) => {
5
+ const babelPluginMapForGroup = {}
6
+ missingFeatureNames.forEach((featureName) => {
7
+ const babelPlugin = babelPluginMap[featureName]
8
+ if (babelPlugin) {
9
+ babelPluginMapForGroup[featureName] = babelPlugin
10
+ }
11
+ })
12
+ Object.keys(babelPluginMap).forEach((key) => {
13
+ if (key.startsWith("syntax-")) {
14
+ babelPluginMapForGroup[key] = babelPluginMap[key]
15
+ }
16
+ if (key === "transform-replace-expressions") {
17
+ babelPluginMapForGroup[key] = babelPluginMap[key]
18
+ }
19
+ })
20
+ return babelPluginMapForGroup
21
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * allows the following:
3
+ *
4
+ * import "@jsenv/core/helpers/regenerator-runtime/regenerator-runtime.js"
5
+ * -> searches a file inside @jsenv/core/*
6
+ *
7
+ */
8
+
9
+ import { urlToRelativeUrl, urlIsInsideOf, resolveUrl } from "@jsenv/filesystem"
10
+
11
+ import { jsenvCoreDirectoryUrl } from "@jsenv/core/src/internal/jsenvCoreDirectoryUrl.js"
12
+
13
+ export const getDefaultImportmap = (
14
+ url,
15
+ { projectDirectoryUrl, compileDirectoryUrl },
16
+ ) => {
17
+ const jsenvCoreDirectoryRelativeUrl = urlToRelativeUrl(
18
+ jsenvCoreDirectoryUrl,
19
+ projectDirectoryUrl,
20
+ )
21
+
22
+ let jsenvCoreUrl
23
+ if (compileDirectoryUrl && urlIsInsideOf(url, compileDirectoryUrl)) {
24
+ jsenvCoreUrl = resolveUrl(
25
+ jsenvCoreDirectoryRelativeUrl,
26
+ compileDirectoryUrl,
27
+ )
28
+ } else {
29
+ jsenvCoreUrl = jsenvCoreDirectoryUrl
30
+ }
31
+
32
+ const importmap = {
33
+ imports: {
34
+ "@jsenv/core/": makeRelativeMapping(jsenvCoreUrl, url),
35
+ },
36
+ }
37
+ return importmap
38
+ }
39
+
40
+ // this function just here to ensure relative urls starts with './'
41
+ // so that importmap do not consider them as bare specifiers
42
+ const makeRelativeMapping = (url, baseUrl) => {
43
+ const relativeUrl = urlToRelativeUrl(url, baseUrl)
44
+
45
+ if (urlIsInsideOf(url, baseUrl)) {
46
+ if (relativeUrl.startsWith("../")) return relativeUrl
47
+ if (relativeUrl.startsWith("./")) return relativeUrl
48
+ return `./${relativeUrl}`
49
+ }
50
+
51
+ return relativeUrl
52
+ }
@@ -34,7 +34,7 @@ export const scanNodeRuntimeFeatures = async ({
34
34
  featuresReport,
35
35
  failFastOnFeatureDetection: true,
36
36
  })
37
- const pluginRequiredNameArray = pluginRequiredNamesFromGroupInfo(groupInfo, {
37
+ const missingFeatureNames = adjustMissingFeatureNames(groupInfo, {
38
38
  featuresReport,
39
39
  coverageHandledFromOutside,
40
40
  })
@@ -43,14 +43,14 @@ export const scanNodeRuntimeFeatures = async ({
43
43
  // node native resolution will not auto add extension
44
44
  !importDefaultExtension &&
45
45
  customCompilerPatterns.length === 0 &&
46
- pluginRequiredNameArray.length === 0 &&
46
+ missingFeatureNames.length === 0 &&
47
47
  featuresReport.dynamicImport &&
48
48
  featuresReport.topLevelAwait
49
49
 
50
50
  return {
51
51
  canAvoidCompilation,
52
52
  featuresReport,
53
- pluginRequiredNameArray,
53
+ missingFeatureNames,
54
54
  compileId,
55
55
  importDefaultExtension,
56
56
  node,
@@ -84,28 +84,34 @@ const importJson = async (url) => {
84
84
  return object
85
85
  }
86
86
 
87
- const pluginRequiredNamesFromGroupInfo = (
87
+ const adjustMissingFeatureNames = (
88
88
  groupInfo,
89
89
  { coverageHandledFromOutside },
90
90
  ) => {
91
- const { pluginRequiredNameArray } = groupInfo
92
- const requiredPluginNames = pluginRequiredNameArray.slice()
93
- const markPluginAsSupported = (name) => {
94
- const index = requiredPluginNames.indexOf(name)
91
+ const { missingFeatureNames } = groupInfo
92
+ const missingFeatureNamesCopy = missingFeatureNames.slice()
93
+ const markAsSupported = (name) => {
94
+ const index = missingFeatureNamesCopy.indexOf(name)
95
95
  if (index > -1) {
96
- requiredPluginNames.splice(index, 1)
96
+ missingFeatureNamesCopy.splice(index, 1)
97
97
  }
98
98
  }
99
-
100
99
  if (coverageHandledFromOutside) {
101
- markPluginAsSupported("transform-instrument")
100
+ markAsSupported("transform-instrument")
102
101
  }
103
- // CSS import assertions and constructable stylesheet are not supported by Node.js
104
- // but we assume they are not used for code executed in Node.js
105
- // Without this check code executed on Node.js would always be compiled
106
- // because import assertions and constructable stylesheet are enabled by default
107
- markPluginAsSupported("transform-import-assertions")
108
- markPluginAsSupported("new-stylesheet-as-jsenv-import")
109
-
110
- return requiredPluginNames
102
+ // Jsenv enable some features because they are standard and we can expect code to use them.
103
+ // At the time of writing this, these features are not available in latest Node.js.
104
+ // Some feature are also browser specific.
105
+ // To avoid compiling code for Node.js these feaure are marked as supported.
106
+ // It means code written to be execute in Node.js should not use these features
107
+ // because jsenv ignore them (it won't try to "polyfill" them)
108
+ markAsSupported("module")
109
+ markAsSupported("importmap")
110
+ markAsSupported("transform-import-assertions")
111
+ markAsSupported("import_assertion_type_json")
112
+ markAsSupported("import_assertion_type_css")
113
+ markAsSupported("new-stylesheet-as-jsenv-import")
114
+ markAsSupported("worker_type_module")
115
+ markAsSupported("worker_importmap")
116
+ return missingFeatureNamesCopy
111
117
  }