@jsenv/core 27.0.0-alpha.12 → 27.0.0-alpha.13

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 (116) hide show
  1. package/main.js +4 -0
  2. package/package.json +13 -7
  3. package/readme.md +4 -12
  4. package/src/build/build.js +438 -387
  5. package/src/build/build_urls_generator.js +23 -20
  6. package/src/build/graph_utils.js +31 -0
  7. package/src/build/{inject_version_mappings.js → inject_global_version_mappings.js} +31 -14
  8. package/src/build/inject_service_worker_urls.js +66 -12
  9. package/src/build/resync_ressource_hints.js +83 -0
  10. package/src/dev/plugins/autoreload/babel_plugin_metadata_import_meta_hot.js +1 -1
  11. package/src/dev/plugins/autoreload/client/import_meta_hot.js +3 -1
  12. package/src/dev/plugins/autoreload/html_hot_dependencies.js +2 -2
  13. package/src/dev/plugins/autoreload/jsenv_plugin_autoreload.js +61 -51
  14. package/src/dev/plugins/autoreload/sse_service.js +23 -3
  15. package/src/dev/plugins/explorer/jsenv_plugin_explorer.js +2 -2
  16. package/src/dev/plugins/toolbar/jsenv_plugin_toolbar.js +3 -1
  17. package/src/dev/start_dev_server.js +10 -5
  18. package/src/execute/execute.js +10 -4
  19. package/src/execute/run.js +17 -54
  20. package/src/execute/runtimes/browsers/from_playwright.js +167 -146
  21. package/src/execute/runtimes/node/node_process.js +281 -37
  22. package/src/omega/{runtime_support/default_runtime_support.js → compat/default_runtime_compat.js} +3 -5
  23. package/src/omega/{runtime_support/features_compatibility.js → compat/features_compats.js} +30 -7
  24. package/src/omega/{runtime_support/runtime_support.js → compat/runtime_compat.js} +14 -16
  25. package/src/omega/errors.js +51 -58
  26. package/src/omega/fetched_content_compliance.js +24 -0
  27. package/src/omega/kitchen.js +396 -280
  28. package/src/omega/server/file_service.js +9 -11
  29. package/src/omega/url_graph/url_graph_load.js +13 -7
  30. package/src/omega/url_graph/url_graph_report.js +7 -5
  31. package/src/omega/url_graph.js +22 -10
  32. package/src/omega/web_workers.js +42 -0
  33. package/src/plugins/bundling/css/bundle_css.js +17 -0
  34. package/src/plugins/bundling/js_classic_workers/bundle_js_classic_workers.js +13 -0
  35. package/src/{build/plugins/bundle_js_module/jsenv_plugin_bundle_js_module.js → plugins/bundling/js_module/bundle_js_module.js} +100 -75
  36. package/src/plugins/bundling/jsenv_plugin_bundling.js +51 -0
  37. package/src/{omega/core_plugins → plugins}/commonjs_globals/jsenv_plugin_commonjs_globals.js +48 -41
  38. package/src/plugins/file_urls/jsenv_plugin_file_urls.js +66 -0
  39. package/src/{omega/core_plugins → plugins}/filesystem_magic/jsenv_plugin_filesystem_magic.js +7 -4
  40. package/src/{omega/core_plugins → plugins}/html_supervisor/client/error_in_document.js +0 -0
  41. package/src/{omega/core_plugins → plugins}/html_supervisor/client/error_in_notification.js +0 -0
  42. package/src/{omega/core_plugins → plugins}/html_supervisor/client/html_supervisor_installer.js +3 -2
  43. package/src/{omega/core_plugins → plugins}/html_supervisor/client/html_supervisor_setup.js +0 -0
  44. package/src/{omega/core_plugins → plugins}/html_supervisor/client/perf_browser.js +0 -0
  45. package/src/{omega/core_plugins → plugins}/html_supervisor/client/uneval_exception.js +0 -0
  46. package/src/{omega/core_plugins → plugins}/html_supervisor/jsenv_plugin_html_supervisor.js +38 -46
  47. package/src/plugins/http_urls/jsenv_plugin_http_urls.js +12 -0
  48. package/src/{omega/core_plugins → plugins}/import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js +26 -8
  49. package/src/plugins/import_meta_url/client/import_meta_url_browser.js +52 -0
  50. package/src/plugins/import_meta_url/client/import_meta_url_commonjs.mjs +9 -0
  51. package/src/{omega/core_plugins → plugins}/importmap/jsenv_plugin_importmap.js +37 -31
  52. package/src/{omega/core_plugins → plugins}/inject_globals/jsenv_plugin_inject_globals.js +4 -6
  53. package/src/{omega/core_plugins → plugins}/inline/client/inline_content.js +0 -0
  54. package/src/{omega/core_plugins → plugins}/inline/jsenv_plugin_data_urls.js +18 -14
  55. package/src/{omega/core_plugins/inline/jsenv_plugin_js_and_css_inside_html.js → plugins/inline/jsenv_plugin_html_inline_content.js} +61 -40
  56. package/src/plugins/inline/jsenv_plugin_inline.js +36 -0
  57. package/src/{omega/core_plugins → plugins}/inline/jsenv_plugin_inline_query_param.js +6 -6
  58. package/src/plugins/inline/jsenv_plugin_js_inline_content.js +263 -0
  59. package/src/plugins/leading_slash/jsenv_plugin_leading_slash.js +13 -0
  60. package/src/plugins/minification/css/minify_css.js +9 -0
  61. package/src/plugins/minification/html/minify_html.js +15 -0
  62. package/src/{build/plugins/minify_js/jsenv_plugin_minify_js.js → plugins/minification/js/minify_js.js} +6 -22
  63. package/src/plugins/minification/jsenv_plugin_minification.js +77 -0
  64. package/src/plugins/minification/json/minify_json.js +8 -0
  65. package/src/{omega/core_plugins → plugins}/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +15 -15
  66. package/src/{omega → plugins}/plugin_controller.js +18 -10
  67. package/src/plugins/plugins.js +50 -0
  68. package/src/plugins/transpilation/as_js_classic/client/s.js +808 -0
  69. package/src/plugins/transpilation/as_js_classic/client/s.js.md +1 -0
  70. package/src/plugins/transpilation/as_js_classic/helpers/babel_plugin_transform_import_meta_url.js +47 -0
  71. package/src/plugins/transpilation/as_js_classic/helpers/systemjs_old.js +43 -0
  72. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +178 -0
  73. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_script_type_module_as_classic.js +156 -0
  74. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_top_level_await.js +37 -0
  75. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_workers_type_module_as_classic.js +133 -0
  76. package/src/{omega/core_plugins → plugins/transpilation}/babel/global_this/babel_plugin_global_this_as_jsenv_import.js +0 -0
  77. package/src/{omega/core_plugins → plugins/transpilation}/babel/global_this/client/global_this.js +0 -0
  78. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugin_babel_helpers_as_jsenv_imports.js +0 -0
  79. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugin_structure.js +3 -21
  80. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugins_compatibility.js +0 -0
  81. package/src/{omega/core_plugins → plugins/transpilation}/babel/jsenv_plugin_babel.js +29 -27
  82. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/babel_plugin_new_stylesheet_as_jsenv_import.js +0 -0
  83. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/client/.eslintrc.cjs +0 -0
  84. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/client/new_stylesheet.js +0 -0
  85. package/src/{omega/core_plugins → plugins/transpilation}/babel/regenerator_runtime/babel_plugin_regenerator_runtime_as_jsenv_import.js +0 -0
  86. package/src/{omega/core_plugins → plugins/transpilation}/babel/regenerator_runtime/client/regenerator_runtime.js +0 -0
  87. package/src/plugins/transpilation/css_parcel/jsenv_plugin_css_parcel.js +18 -0
  88. package/src/{omega/core_plugins → plugins/transpilation}/import_assertions/helpers/babel_plugin_metadata_import_assertions.js +0 -0
  89. package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +243 -0
  90. package/src/plugins/transpilation/jsenv_plugin_transpilation.js +40 -0
  91. package/src/plugins/url_references/css/css_urls.js +49 -0
  92. package/src/plugins/url_references/html/html_urls.js +273 -0
  93. package/src/plugins/url_references/js/js_urls.js +170 -0
  94. package/src/plugins/url_references/jsenv_plugin_url_references.js +18 -0
  95. package/src/plugins/url_references/webmanifest/webmanifest_urls.js +17 -0
  96. package/src/{omega/core_plugins → plugins}/url_resolution/jsenv_plugin_url_resolution.js +12 -5
  97. package/src/{omega/core_plugins → plugins}/url_version/jsenv_plugin_url_version.js +8 -8
  98. package/src/preview/preview.js +3 -0
  99. package/src/test/execute_plan.js +18 -11
  100. package/src/test/execute_test_plan.js +5 -6
  101. package/src/test/logs_file_execution.js +8 -7
  102. package/src/build/plugins/minify_html/jsenv_plugin_minify_html.js +0 -30
  103. package/src/execute/runtimes/node/controlled_process.js +0 -316
  104. package/src/omega/core_plugins/file_urls/jsenv_plugin_file_urls.js +0 -67
  105. package/src/omega/core_plugins/import_assertions/helpers/json_module.js +0 -12
  106. package/src/omega/core_plugins/import_assertions/helpers/text_module.js +0 -6
  107. package/src/omega/core_plugins/import_assertions/jsenv_plugin_import_assertions.js +0 -211
  108. package/src/omega/core_plugins/inline/jsenv_plugin_inline.js +0 -13
  109. package/src/omega/core_plugins/inline/jsenv_plugin_new_inline_content.js +0 -207
  110. package/src/omega/core_plugins/leading_slash/jsenv_plugin_leading_slash.js +0 -12
  111. package/src/omega/core_plugins.js +0 -42
  112. package/src/omega/url_mentions/css_url_mentions.js +0 -63
  113. package/src/omega/url_mentions/html_url_mentions.js +0 -185
  114. package/src/omega/url_mentions/js_module_url_mentions.js +0 -91
  115. package/src/omega/url_mentions/parse_url_mentions.js +0 -37
  116. package/src/omega/url_mentions/worker_classic_url_mentions.js +0 -37
@@ -1,40 +1,44 @@
1
1
  import { applyBabelPlugins } from "@jsenv/utils/js_ast/apply_babel_plugins.js"
2
2
 
3
- import { RUNTIME_SUPPORT } from "@jsenv/core/src/omega/runtime_support/runtime_support.js"
3
+ import { RUNTIME_COMPAT } from "@jsenv/core/src/omega/compat/runtime_compat.js"
4
4
  import { getBaseBabelPluginStructure } from "./helpers/babel_plugin_structure.js"
5
5
  import { babelPluginBabelHelpersAsJsenvImports } from "./helpers/babel_plugin_babel_helpers_as_jsenv_imports.js"
6
6
  import { babelPluginNewStylesheetAsJsenvImport } from "./new_stylesheet/babel_plugin_new_stylesheet_as_jsenv_import.js"
7
7
  import { babelPluginGlobalThisAsJsenvImport } from "./global_this/babel_plugin_global_this_as_jsenv_import.js"
8
8
  import { babelPluginRegeneratorRuntimeAsJsenvImport } from "./regenerator_runtime/babel_plugin_regenerator_runtime_as_jsenv_import.js"
9
9
 
10
- export const jsenvPluginBabel = ({
11
- getCustomBabelPlugins,
12
- topLevelAwait,
13
- } = {}) => {
10
+ export const jsenvPluginBabel = ({ getCustomBabelPlugins } = {}) => {
14
11
  const transformWithBabel = async (urlInfo, context) => {
15
12
  const isJsModule = urlInfo.type === "js_module"
16
13
  const isWorker = urlInfo.subtype === "worker"
17
14
  const isServiceWorker = urlInfo.subtype === "service_worker"
18
- const isWorkerContext = isWorker || isServiceWorker
19
- let { runtimeSupport } = context
20
- if (isServiceWorker) {
15
+ const isSharedWorker = urlInfo.subtype === "shared_worker"
16
+ const isWorkerContext = isWorker || isServiceWorker || isSharedWorker
17
+
18
+ let { clientRuntimeCompat } = context
19
+ if (isWorker) {
20
+ clientRuntimeCompat = RUNTIME_COMPAT.add(clientRuntimeCompat, "worker")
21
+ } else if (isServiceWorker) {
21
22
  // when code is executed by a service worker we can assume
22
23
  // the execution context supports more than the default one
23
24
  // for instance arrow function are supported
24
- runtimeSupport = RUNTIME_SUPPORT.add(runtimeSupport, "service_worker")
25
- }
26
- if (isWorker) {
27
- runtimeSupport = RUNTIME_SUPPORT.add(runtimeSupport, "worker")
25
+ clientRuntimeCompat = RUNTIME_COMPAT.add(
26
+ clientRuntimeCompat,
27
+ "service_worker",
28
+ )
29
+ } else if (isSharedWorker) {
30
+ clientRuntimeCompat = RUNTIME_COMPAT.add(
31
+ clientRuntimeCompat,
32
+ "shared_worker",
33
+ )
28
34
  }
35
+
29
36
  const { referenceUtils } = context
30
- const isSupportedOnRuntime = (feature) =>
31
- RUNTIME_SUPPORT.isSupported(runtimeSupport, feature)
37
+ const isSupported = (feature) =>
38
+ RUNTIME_COMPAT.isSupported(clientRuntimeCompat, feature)
32
39
  const babelPluginStructure = getBaseBabelPluginStructure({
33
40
  url: urlInfo.url,
34
- isSupportedOnRuntime,
35
- topLevelAwait,
36
- usesTopLevelAwait: urlInfo.data.usesTopLevelAwait,
37
- isJsModule,
41
+ isSupported,
38
42
  isWorkerContext,
39
43
  })
40
44
  if (getCustomBabelPlugins) {
@@ -45,11 +49,12 @@ export const jsenvPluginBabel = ({
45
49
  const getImportSpecifier = (clientFileUrl) => {
46
50
  const [reference] = referenceUtils.inject({
47
51
  type: "js_import_export",
52
+ expectedType: "js_module",
48
53
  specifier: clientFileUrl,
49
54
  })
50
55
  return JSON.parse(reference.generatedSpecifier)
51
56
  }
52
- if (!isSupportedOnRuntime("global_this")) {
57
+ if (!isSupported("global_this")) {
53
58
  babelPluginStructure["global-this-as-jsenv-import"] = [
54
59
  babelPluginGlobalThisAsJsenvImport,
55
60
  {
@@ -57,7 +62,7 @@ export const jsenvPluginBabel = ({
57
62
  },
58
63
  ]
59
64
  }
60
- if (!isSupportedOnRuntime("async_generator_function")) {
65
+ if (!isSupported("async_generator_function")) {
61
66
  babelPluginStructure["regenerator-runtime-as-jsenv-import"] = [
62
67
  babelPluginRegeneratorRuntimeAsJsenvImport,
63
68
  {
@@ -65,7 +70,7 @@ export const jsenvPluginBabel = ({
65
70
  },
66
71
  ]
67
72
  }
68
- if (!isSupportedOnRuntime("new_stylesheet")) {
73
+ if (!isSupported("new_stylesheet")) {
69
74
  babelPluginStructure["new-stylesheet-as-jsenv-import"] = [
70
75
  babelPluginNewStylesheetAsJsenvImport,
71
76
  {
@@ -90,22 +95,19 @@ export const jsenvPluginBabel = ({
90
95
  )
91
96
  const { code, map } = await applyBabelPlugins({
92
97
  babelPlugins,
93
- url: urlInfo.url,
94
- generatedUrl: urlInfo.generatedUrl,
95
- content: urlInfo.content,
98
+ urlInfo,
96
99
  })
97
100
  return {
98
101
  content: code,
99
102
  sourcemap: map,
100
103
  }
101
104
  }
102
-
103
105
  return {
104
106
  name: "jsenv:babel",
105
107
  appliesDuring: "*",
106
- transform: {
107
- js_module: transformWithBabel,
108
+ finalizeUrlContent: {
108
109
  js_classic: transformWithBabel,
110
+ js_module: transformWithBabel,
109
111
  },
110
112
  }
111
113
  }
@@ -0,0 +1,18 @@
1
+ import { transpileWithParcel } from "@jsenv/utils/css_ast/parcel_css.js"
2
+
3
+ // https://github.com/parcel-bundler/parcel-css
4
+ export const jsenvPluginCssParcel = () => {
5
+ return {
6
+ name: "jsenv:css_parcel",
7
+ appliesDuring: "*",
8
+ transformUrlContent: {
9
+ css: (urlInfo, context) => {
10
+ const { code, map } = transpileWithParcel(urlInfo, context)
11
+ return {
12
+ content: String(code),
13
+ sourcemap: map,
14
+ }
15
+ },
16
+ },
17
+ }
18
+ }
@@ -0,0 +1,243 @@
1
+ import { urlToFilename } from "@jsenv/filesystem"
2
+
3
+ import { applyBabelPlugins } from "@jsenv/utils/js_ast/apply_babel_plugins.js"
4
+ import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
5
+ import { injectQueryParamsIntoSpecifier } from "@jsenv/utils/urls/url_utils.js"
6
+ import { JS_QUOTES } from "@jsenv/utils/string/js_quotes.js"
7
+
8
+ import { babelPluginMetadataImportAssertions } from "./helpers/babel_plugin_metadata_import_assertions.js"
9
+
10
+ export const jsenvPluginImportAssertions = () => {
11
+ const importAssertions = {
12
+ name: "jsenv:import_assertions",
13
+ appliesDuring: "*",
14
+ transformUrlContent: {
15
+ js_module: async (urlInfo, context) => {
16
+ const importTypesToTranspile = getImportTypesToTranspile(context)
17
+ if (importTypesToTranspile.length === 0) {
18
+ return null
19
+ }
20
+ const { metadata } = await applyBabelPlugins({
21
+ babelPlugins: [babelPluginMetadataImportAssertions],
22
+ urlInfo,
23
+ })
24
+ const { importAssertions } = metadata
25
+ const magicSource = createMagicSource(urlInfo.content)
26
+ importAssertions.forEach((importAssertion) => {
27
+ const assertType = importAssertion.assert.type
28
+ if (!importTypesToTranspile.includes(assertType)) {
29
+ return
30
+ }
31
+ const { searchParam } = importAsInfos[assertType]
32
+ const { path } = importAssertion
33
+ const { node } = path
34
+ if (node.type === "CallExpression") {
35
+ const importSpecifierPath = path.get("arguments")[0]
36
+ const specifier = importSpecifierPath.node.value
37
+ const reference = context.referenceUtils.findByGeneratedSpecifier(
38
+ JSON.stringify(specifier),
39
+ )
40
+ const [newReference] = context.referenceUtils.update(reference, {
41
+ expectedType: "js_module",
42
+ specifier: injectQueryParamsIntoSpecifier(specifier, {
43
+ [searchParam]: "",
44
+ }),
45
+ filename: `${urlToFilename(reference.url)}.js`,
46
+ })
47
+ magicSource.replace({
48
+ start: importSpecifierPath.node.start,
49
+ end: importSpecifierPath.node.end,
50
+ replacement: newReference.generatedSpecifier,
51
+ })
52
+ const secondArgPath = path.get("arguments")[1]
53
+ magicSource.remove({
54
+ start: secondArgPath.node.start,
55
+ end: secondArgPath.node.end,
56
+ })
57
+ return
58
+ }
59
+ const importSpecifierPath = path.get("source")
60
+ const specifier = importSpecifierPath.node.value
61
+ const reference = context.referenceUtils.findByGeneratedSpecifier(
62
+ JSON.stringify(specifier),
63
+ )
64
+ const [newReference] = context.referenceUtils.update(reference, {
65
+ expectedType: "js_module",
66
+ specifier: injectQueryParamsIntoSpecifier(specifier, {
67
+ [searchParam]: "",
68
+ }),
69
+ filename: `${urlToFilename(reference.url)}.js`,
70
+ })
71
+ magicSource.replace({
72
+ start: importSpecifierPath.node.start,
73
+ end: importSpecifierPath.node.end,
74
+ replacement: newReference.generatedSpecifier,
75
+ })
76
+ const assertionsPath = path.get("assertions")[0]
77
+ magicSource.remove({
78
+ start: assertionsPath.node.start,
79
+ end: assertionsPath.node.end,
80
+ })
81
+ })
82
+ return magicSource.toContentAndSourcemap()
83
+ },
84
+ },
85
+ }
86
+ return [importAssertions, ...jsenvPluginAsModules()]
87
+ }
88
+
89
+ const jsenvPluginAsModules = () => {
90
+ const inlineContentClientFileUrl = new URL(
91
+ "../../inline/client/inline_content.js",
92
+ import.meta.url,
93
+ ).href
94
+
95
+ const asJsonModule = {
96
+ name: `jsenv:as_json_module`,
97
+ appliesDuring: "*",
98
+ fetchUrlContent: (urlInfo, context) => {
99
+ return fetchOriginalUrl({
100
+ urlInfo,
101
+ context,
102
+ searchParam: "as_json_module",
103
+ convertToJsModule: (urlInfo) => {
104
+ // here we could `export default ${jsonText}`:
105
+ // but js engine are optimized to recognize JSON.parse
106
+ // and use a faster parsing strategy
107
+ return `export default JSON.parse(${JSON.stringify(
108
+ urlInfo.content.trim(),
109
+ )})`
110
+ },
111
+ })
112
+ },
113
+ }
114
+
115
+ const asCssModule = {
116
+ name: `jsenv:as_css_module`,
117
+ appliesDuring: "*",
118
+ fetchUrlContent: (urlInfo, context) => {
119
+ return fetchOriginalUrl({
120
+ urlInfo,
121
+ context,
122
+ searchParam: "as_css_module",
123
+ convertToJsModule: (urlInfo) => {
124
+ const cssText = JS_QUOTES.escapeSpecialChars(urlInfo.content, {
125
+ // If template string is choosen and runtime do not support template literals
126
+ // it's ok because "jsenv:new_inline_content" plugin executes after this one
127
+ // and convert template strings into raw strings
128
+ canUseTemplateString: true,
129
+ })
130
+ return `import { InlineContent } from ${JSON.stringify(
131
+ inlineContentClientFileUrl,
132
+ )}
133
+
134
+ const inlineContent = new InlineContent(${cssText}, { type: "text/css" })
135
+ const stylesheet = new CSSStyleSheet()
136
+ stylesheet.replaceSync(inlineContent.text)
137
+ export default stylesheet`
138
+ },
139
+ })
140
+ },
141
+ }
142
+
143
+ const asTextModule = {
144
+ name: `jsenv:as_text_module`,
145
+ appliesDuring: "*",
146
+ fetchUrlContent: (urlInfo, context) => {
147
+ return fetchOriginalUrl({
148
+ urlInfo,
149
+ context,
150
+ searchParam: "as_text_module",
151
+ convertToJsModule: (urlInfo) => {
152
+ const textPlain = JS_QUOTES.escapeSpecialChars(urlInfo.content, {
153
+ // If template string is choosen and runtime do not support template literals
154
+ // it's ok because "jsenv:new_inline_content" plugin executes after this one
155
+ // and convert template strings into raw strings
156
+ canUseTemplateString: true,
157
+ })
158
+ return `import { InlineContent } from ${JSON.stringify(
159
+ inlineContentClientFileUrl,
160
+ )}
161
+
162
+ const inlineContent = new InlineContent(${textPlain}, { type: "text/plain" })
163
+ export default inlineContent.text`
164
+ },
165
+ })
166
+ },
167
+ }
168
+
169
+ return [asJsonModule, asCssModule, asTextModule]
170
+ }
171
+
172
+ const fetchOriginalUrl = async ({
173
+ urlInfo,
174
+ context,
175
+ searchParam,
176
+ expectedType,
177
+ convertToJsModule,
178
+ }) => {
179
+ const urlObject = new URL(urlInfo.url)
180
+ const { searchParams } = urlObject
181
+ if (!searchParams.has(searchParam)) {
182
+ return null
183
+ }
184
+ searchParams.delete(searchParam)
185
+ const originalUrl = urlObject.href
186
+ const originalReference = {
187
+ ...(context.reference.original || context.reference),
188
+ expectedType,
189
+ }
190
+ originalReference.url = originalUrl
191
+ const originalUrlInfo = context.urlGraph.reuseOrCreateUrlInfo(
192
+ originalReference.url,
193
+ )
194
+ await context.fetchUrlContent({
195
+ reference: originalReference,
196
+ urlInfo: originalUrlInfo,
197
+ })
198
+ return {
199
+ type: "js_module",
200
+ contentType: "text/javascript",
201
+ content: convertToJsModule(originalUrlInfo, context),
202
+ }
203
+ }
204
+
205
+ const importAsInfos = {
206
+ json: {
207
+ searchParam: "as_json_module",
208
+ expectedType: "json",
209
+ },
210
+ css: {
211
+ searchParam: "as_css_module",
212
+ expectedType: "css",
213
+ },
214
+ text: {
215
+ searchParam: "as_text_module",
216
+ expectedType: "text",
217
+ },
218
+ }
219
+
220
+ const getImportTypesToTranspile = ({
221
+ scenario,
222
+ isSupportedOnCurrentClients,
223
+ }) => {
224
+ // during build always replace import assertions with the js:
225
+ // - means rollup can bundle more js file together
226
+ // - means url versioning can work for css inlined in js
227
+ // - avoid rollup to see import assertions
228
+ // We would have to tell rollup to ignore import with assertion
229
+ if (scenario === "build") {
230
+ return ["json", "css", "text"]
231
+ }
232
+ const importTypes = []
233
+ if (!isSupportedOnCurrentClients("import_type_json")) {
234
+ importTypes.push("json")
235
+ }
236
+ if (!isSupportedOnCurrentClients("import_type_css")) {
237
+ importTypes.push("css")
238
+ }
239
+ if (!isSupportedOnCurrentClients("import_type_text")) {
240
+ importTypes.push("text")
241
+ }
242
+ return importTypes
243
+ }
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Transforms code to make it compatible with browser that would not be able to
3
+ * run it otherwise. For instance:
4
+ * - const -> var
5
+ * - async/await -> promises
6
+ * Anything that is not standard (import.meta.dev for instance) is outside the scope
7
+ * of this plugin
8
+ */
9
+
10
+ import { jsenvPluginCssParcel } from "./css_parcel/jsenv_plugin_css_parcel.js"
11
+ import { jsenvPluginImportAssertions } from "./import_assertions/jsenv_plugin_import_assertions.js"
12
+ import { jsenvPluginAsJsClassic } from "./as_js_classic/jsenv_plugin_as_js_classic.js"
13
+ import { jsenvPluginBabel } from "./babel/jsenv_plugin_babel.js"
14
+
15
+ export const jsenvPluginTranspilation = ({
16
+ importAssertions = true,
17
+ css = true,
18
+ jsModuleAsJsClassic = true,
19
+ systemJsInjection = true,
20
+ topLevelAwait = true,
21
+ getCustomBabelPlugins,
22
+ }) => {
23
+ return [
24
+ // import assertions we want it all the time
25
+ ...(importAssertions ? [jsenvPluginImportAssertions()] : []),
26
+ // babel also so that rollup can bundle babel helpers for instance
27
+ jsenvPluginBabel({
28
+ topLevelAwait,
29
+ getCustomBabelPlugins,
30
+ }),
31
+ // but the conversion from js_module to js_classic
32
+ // we want to do it after bundling
33
+ // so the build function will disable jsModuleAsJsClassic during build
34
+ // and enable it manually during postbuild
35
+ ...(jsModuleAsJsClassic
36
+ ? [jsenvPluginAsJsClassic({ systemJsInjection })]
37
+ : []),
38
+ ...(css ? [jsenvPluginCssParcel()] : []),
39
+ ]
40
+ }
@@ -0,0 +1,49 @@
1
+ /*
2
+ * https://github.com/parcel-bundler/parcel/blob/v2/packages/transformers/css/src/CSSTransformer.js
3
+ */
4
+
5
+ import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
6
+ import { applyPostCss } from "@jsenv/utils/css_ast/apply_post_css.js"
7
+ import { postCssPluginUrlVisitor } from "@jsenv/utils/css_ast/postcss_plugin_url_visitor.js"
8
+
9
+ export const parseAndTransformCssUrls = async (urlInfo, context) => {
10
+ const actions = []
11
+ const magicSource = createMagicSource(urlInfo.content)
12
+ await applyPostCss({
13
+ sourcemaps: false,
14
+ plugins: [
15
+ postCssPluginUrlVisitor({
16
+ urlVisitor: ({
17
+ declarationNode,
18
+ type,
19
+ specifier,
20
+ line,
21
+ column,
22
+ start,
23
+ end,
24
+ }) => {
25
+ const [reference] = context.referenceUtils.found({
26
+ node: declarationNode,
27
+ type: `css_${type}`,
28
+ specifier,
29
+ line,
30
+ column,
31
+ })
32
+ actions.push(async () => {
33
+ magicSource.replace({
34
+ start,
35
+ end,
36
+ replacement: await context.referenceUtils.readGeneratedSpecifier(
37
+ reference,
38
+ ),
39
+ })
40
+ })
41
+ },
42
+ }),
43
+ ],
44
+ url: urlInfo.data.rawUrl || urlInfo.url,
45
+ content: urlInfo.content,
46
+ })
47
+ await Promise.all(actions.map((action) => action()))
48
+ return magicSource.toContentAndSourcemap()
49
+ }