@jsenv/core 29.1.7 → 29.1.9

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.
@@ -16,7 +16,8 @@ import {
16
16
  injectScriptNodeAsEarlyAsPossible,
17
17
  createHtmlNode,
18
18
  } from "@jsenv/ast"
19
- import { injectQueryParams } from "@jsenv/urls"
19
+ import { injectQueryParams, urlToRelativeUrl } from "@jsenv/urls"
20
+ import { SOURCEMAP } from "@jsenv/sourcemap"
20
21
 
21
22
  export const jsenvPluginAsJsClassicHtml = ({
22
23
  systemJsInjection,
@@ -32,10 +33,14 @@ export const jsenvPluginAsJsClassicHtml = ({
32
33
  name: "jsenv:as_js_classic_html",
33
34
  appliesDuring: "*",
34
35
  init: (context) => {
35
- shouldTransformScriptTypeModule =
36
- !context.isSupportedOnCurrentClients("script_type_module") ||
37
- !context.isSupportedOnCurrentClients("import_dynamic") ||
38
- !context.isSupportedOnCurrentClients("import_meta")
36
+ const nodeRuntimeEnabled = Object.keys(context.runtimeCompat).includes(
37
+ "node",
38
+ )
39
+ shouldTransformScriptTypeModule = nodeRuntimeEnabled
40
+ ? false
41
+ : !context.isSupportedOnCurrentClients("script_type_module") ||
42
+ !context.isSupportedOnCurrentClients("import_dynamic") ||
43
+ !context.isSupportedOnCurrentClients("import_meta")
39
44
  },
40
45
  redirectUrl: {
41
46
  link_href: (reference) => {
@@ -63,6 +68,15 @@ export const jsenvPluginAsJsClassicHtml = ({
63
68
  }
64
69
  return null
65
70
  },
71
+ js_url_specifier: (reference) => {
72
+ if (
73
+ shouldTransformScriptTypeModule &&
74
+ reference.expectedType === "js_module"
75
+ ) {
76
+ return turnIntoJsClassicProxy(reference)
77
+ }
78
+ return null
79
+ },
66
80
  },
67
81
  finalizeUrlContent: {
68
82
  html: async (urlInfo, context) => {
@@ -162,10 +176,25 @@ export const jsenvPluginAsJsClassicHtml = ({
162
176
  }
163
177
  if (needsSystemJs) {
164
178
  mutations.push(async () => {
165
- const systemJsFileContent = readFileSync(
179
+ let systemJsFileContent = readFileSync(
166
180
  new URL(systemJsClientFileUrl),
167
181
  { encoding: "utf8" },
168
182
  )
183
+ const sourcemapFound = SOURCEMAP.readComment({
184
+ contentType: "text/javascript",
185
+ content: systemJsFileContent,
186
+ })
187
+ if (sourcemapFound) {
188
+ const sourcemapFileUrl = new URL(
189
+ sourcemapFound.specifier,
190
+ systemJsClientFileUrl,
191
+ )
192
+ systemJsFileContent = SOURCEMAP.writeComment({
193
+ contentType: "text/javascript",
194
+ content: systemJsFileContent,
195
+ specifier: urlToRelativeUrl(sourcemapFileUrl, urlInfo.url),
196
+ })
197
+ }
169
198
  const [systemJsReference, systemJsUrlInfo] =
170
199
  context.referenceUtils.inject({
171
200
  type: "script_src",
@@ -0,0 +1,55 @@
1
+ import { CONTENT_TYPE } from "@jsenv/utils/src/content_type/content_type.js"
2
+
3
+ export const jsenvPluginReferenceExpectedTypes = () => {
4
+ const redirectJsUrls = (reference) => {
5
+ const urlObject = new URL(reference.url)
6
+ const { searchParams } = urlObject
7
+
8
+ if (searchParams.has("entry_point")) {
9
+ reference.isEntryPoint = true
10
+ }
11
+ if (searchParams.has("js_classic")) {
12
+ searchParams.delete("js_classic")
13
+ reference.expectedType = "js_classic"
14
+ } else if (
15
+ searchParams.has("as_js_classic") ||
16
+ searchParams.has("as_js_classic_library")
17
+ ) {
18
+ reference.expectedType = "js_classic"
19
+ } else if (searchParams.has("js_module")) {
20
+ searchParams.delete("js_module")
21
+ reference.expectedType = "js_module"
22
+ } else if (
23
+ reference.type === "js_url_specifier" &&
24
+ reference.expectedType === undefined &&
25
+ CONTENT_TYPE.fromUrlExtension(reference.url) === "text/javascript"
26
+ ) {
27
+ // by default, js referenced by new URL is considered as "js_module"
28
+ // in case this is not desired code must use "?js_classic" like
29
+ // new URL('./file.js?js_classic', import.meta.url)
30
+ reference.expectedType = "js_module"
31
+ }
32
+
33
+ if (searchParams.has("worker")) {
34
+ reference.expectedSubtype = "worker"
35
+ searchParams.delete("worker")
36
+ } else if (searchParams.has("service_worker")) {
37
+ reference.expectedSubtype = "service_worker"
38
+ searchParams.delete("service_worker")
39
+ } else if (searchParams.has("shared_worker")) {
40
+ reference.expectedSubtype = "shared_worker"
41
+ searchParams.delete("shared_worker")
42
+ }
43
+
44
+ return urlObject.href
45
+ }
46
+
47
+ return {
48
+ name: "jsenv:reference_expected_types",
49
+ appliesDuring: "*",
50
+ redirectUrl: {
51
+ script_src: redirectJsUrls,
52
+ js_url_specifier: redirectJsUrls,
53
+ },
54
+ }
55
+ }
@@ -1,6 +1,7 @@
1
1
  import { URL_META } from "@jsenv/url-meta"
2
2
  import { urlToRelativeUrl } from "@jsenv/urls"
3
3
 
4
+ import { jsenvPluginReferenceExpectedTypes } from "./jsenv_plugin_reference_expected_types.js"
4
5
  import { parseAndTransformHtmlUrls } from "./html/html_urls.js"
5
6
  import { parseAndTransformCssUrls } from "./css/css_urls.js"
6
7
  import { parseAndTransformJsUrls } from "./js/js_urls.js"
@@ -24,69 +25,72 @@ export const jsenvPluginUrlAnalysis = ({
24
25
  }
25
26
  }
26
27
 
27
- return {
28
- name: "jsenv:url_analysis",
29
- appliesDuring: "*",
30
- redirectUrl: (reference) => {
31
- if (reference.shouldHandle !== undefined) {
32
- return
33
- }
34
- if (
35
- reference.specifier[0] === "#" &&
36
- // For Html, css and in general "#" refer to a resource in the page
37
- // so that urls must be kept intact
38
- // However for js import specifiers they have a different meaning and we want
39
- // to resolve them (https://nodejs.org/api/packages.html#imports for instance)
40
- reference.type !== "js_import_export"
41
- ) {
42
- reference.shouldHandle = false
43
- return
44
- }
45
- const includeInfo = getIncludeInfo(reference.url)
46
- if (includeInfo === true) {
47
- reference.shouldHandle = true
48
- return
49
- }
50
- if (includeInfo === false) {
51
- reference.shouldHandle = false
52
- return
53
- }
54
- const { protocol } = new URL(reference.url)
55
- const protocolIsSupported = supportedProtocols.some(
56
- (supportedProtocol) => protocol === supportedProtocol,
57
- )
58
- if (protocolIsSupported) {
59
- reference.shouldHandle = true
60
- }
61
- },
62
- transformUrlContent: {
63
- html: parseAndTransformHtmlUrls,
64
- css: parseAndTransformCssUrls,
65
- js_classic: parseAndTransformJsUrls,
66
- js_module: parseAndTransformJsUrls,
67
- webmanifest: parseAndTransformWebmanifestUrls,
68
- directory: (urlInfo, context) => {
69
- const originalDirectoryReference = findOriginalDirectoryReference(
70
- urlInfo,
71
- context,
72
- )
73
- const directoryRelativeUrl = urlToRelativeUrl(
74
- urlInfo.url,
75
- context.rootDirectoryUrl,
28
+ return [
29
+ {
30
+ name: "jsenv:url_analysis",
31
+ appliesDuring: "*",
32
+ redirectUrl: (reference) => {
33
+ if (reference.shouldHandle !== undefined) {
34
+ return
35
+ }
36
+ if (
37
+ reference.specifier[0] === "#" &&
38
+ // For Html, css and in general "#" refer to a resource in the page
39
+ // so that urls must be kept intact
40
+ // However for js import specifiers they have a different meaning and we want
41
+ // to resolve them (https://nodejs.org/api/packages.html#imports for instance)
42
+ reference.type !== "js_import_export"
43
+ ) {
44
+ reference.shouldHandle = false
45
+ return
46
+ }
47
+ const includeInfo = getIncludeInfo(reference.url)
48
+ if (includeInfo === true) {
49
+ reference.shouldHandle = true
50
+ return
51
+ }
52
+ if (includeInfo === false) {
53
+ reference.shouldHandle = false
54
+ return
55
+ }
56
+ const { protocol } = new URL(reference.url)
57
+ const protocolIsSupported = supportedProtocols.some(
58
+ (supportedProtocol) => protocol === supportedProtocol,
76
59
  )
77
- JSON.parse(urlInfo.content).forEach((directoryEntryName) => {
78
- context.referenceUtils.found({
79
- type: "filesystem",
80
- subtype: "directory_entry",
81
- specifier: directoryEntryName,
82
- trace: {
83
- message: `"${directoryRelativeUrl}${directoryEntryName}" entry in directory referenced by ${originalDirectoryReference.trace.message}`,
84
- },
60
+ if (protocolIsSupported) {
61
+ reference.shouldHandle = true
62
+ }
63
+ },
64
+ transformUrlContent: {
65
+ html: parseAndTransformHtmlUrls,
66
+ css: parseAndTransformCssUrls,
67
+ js_classic: parseAndTransformJsUrls,
68
+ js_module: parseAndTransformJsUrls,
69
+ webmanifest: parseAndTransformWebmanifestUrls,
70
+ directory: (urlInfo, context) => {
71
+ const originalDirectoryReference = findOriginalDirectoryReference(
72
+ urlInfo,
73
+ context,
74
+ )
75
+ const directoryRelativeUrl = urlToRelativeUrl(
76
+ urlInfo.url,
77
+ context.rootDirectoryUrl,
78
+ )
79
+ JSON.parse(urlInfo.content).forEach((directoryEntryName) => {
80
+ context.referenceUtils.found({
81
+ type: "filesystem",
82
+ subtype: "directory_entry",
83
+ specifier: directoryEntryName,
84
+ trace: {
85
+ message: `"${directoryRelativeUrl}${directoryEntryName}" entry in directory referenced by ${originalDirectoryReference.trace.message}`,
86
+ },
87
+ })
85
88
  })
86
- })
89
+ },
87
90
  },
88
91
  },
89
- }
92
+ jsenvPluginReferenceExpectedTypes(),
93
+ ]
90
94
  }
91
95
 
92
96
  const findOriginalDirectoryReference = (urlInfo, context) => {