@jsenv/core 20.0.4 → 20.0.5

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.
package/dist/.DS_Store ADDED
Binary file
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/core",
3
- "version": "20.0.4",
3
+ "version": "20.0.5",
4
4
  "description": "Tool to develop, test and build js projects",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,8 +11,7 @@
11
11
  "node": ">=14.9.0"
12
12
  },
13
13
  "publishConfig": {
14
- "access": "public",
15
- "registry": "https://registry.npmjs.org"
14
+ "access": "public"
16
15
  },
17
16
  "type": "module",
18
17
  "exports": {
@@ -387,7 +387,7 @@ export const createAssetBuilder = (
387
387
  return
388
388
  }
389
389
 
390
- const targetBuildBuffer = buildFileInfo.code
390
+ const targetBuildBuffer = Buffer.from(buildFileInfo.code)
391
391
  const targetFileName = buildFileInfo.fileName
392
392
  const targetBuildRelativeUrl =
393
393
  buildManifest[targetFileName] || targetFileName
@@ -543,7 +543,7 @@ export const createAssetBuilder = (
543
543
  const transform = assetTransformMap[targetUrl]
544
544
  if (typeof transform !== "function") {
545
545
  target.targetBuildEnd(
546
- target.targetBuffer,
546
+ target.targetBuildBuffer || target.targetBuffer,
547
547
  target.targetBuildRelativeUrl,
548
548
  )
549
549
  return
@@ -1,49 +1,9 @@
1
- import { resolveUrl, urlToRelativeUrl } from "@jsenv/filesystem"
2
-
3
- import { setJavaScriptSourceMappingUrl } from "@jsenv/core/src/internal/sourceMappingURLUtils.js"
4
-
5
- export const createBuildFileContents = ({ rollupBuild, buildDirectoryUrl }) => {
1
+ export const createBuildFileContents = ({ rollupBuild }) => {
6
2
  const buildFileContents = {}
7
3
  Object.keys(rollupBuild).forEach((buildRelativeUrl) => {
8
- const rollupFileInfo = rollupBuild[buildRelativeUrl]
9
- const fileBuildUrl = resolveUrl(buildRelativeUrl, buildDirectoryUrl)
10
-
11
- if (rollupFileInfo.type === "asset") {
12
- buildFileContents[buildRelativeUrl] = rollupFileInfo.source
13
- return
14
- }
15
-
16
- const { code, map } = rollupFileInfo
17
-
18
- if (!map) {
19
- buildFileContents[buildRelativeUrl] = code
20
- return
21
- }
22
-
23
- const sourcemapBuildRelativeUrl = `${buildRelativeUrl}.map`
24
- const sourcemapRollupFileInfo = rollupBuild[sourcemapBuildRelativeUrl]
25
- if (sourcemapRollupFileInfo) {
26
- // already in rollup build, sourcemap will be found
27
- buildFileContents[buildRelativeUrl] = code
28
- return
29
- }
30
-
31
- const sourcemapBuildUrl = resolveUrl(
32
- sourcemapBuildRelativeUrl,
33
- buildDirectoryUrl,
34
- )
35
- const fileSourcemapString = JSON.stringify(map, null, " ")
36
- buildFileContents[sourcemapBuildRelativeUrl] = fileSourcemapString
4
+ const { type, source, code } = rollupBuild[buildRelativeUrl]
37
5
 
38
- const sourcemapBuildUrlRelativeToFileBuildUrl = urlToRelativeUrl(
39
- sourcemapBuildUrl,
40
- fileBuildUrl,
41
- )
42
- const codeWithSourcemapComment = setJavaScriptSourceMappingUrl(
43
- code,
44
- sourcemapBuildUrlRelativeToFileBuildUrl,
45
- )
46
- buildFileContents[buildRelativeUrl] = codeWithSourcemapComment
6
+ buildFileContents[buildRelativeUrl] = type === "asset" ? source : code
47
7
  })
48
8
  return buildFileContents
49
9
  }
@@ -44,6 +44,7 @@ import { minifyJs } from "./js/minifyJs.js"
44
44
  import { createImportResolverForNode } from "../import-resolution/import-resolver-node.js"
45
45
  import { createImportResolverForImportmap } from "../import-resolution/import-resolver-importmap.js"
46
46
  import { getDefaultImportMap } from "../import-resolution/importmap-default.js"
47
+ import { injectSourcemapInRollupBuild } from "./rollup_build_sourcemap.js"
47
48
  import { createBuildFileContents } from "./build_file_contents.js"
48
49
  import { createBuildStats } from "./build_stats.js"
49
50
 
@@ -901,12 +902,14 @@ building ${entryFileRelativeUrls.length} entry files...`)
901
902
  ...jsModuleBuild,
902
903
  ...assetBuild,
903
904
  }
905
+ rollupBuild = injectSourcemapInRollupBuild(rollupBuild, {
906
+ buildDirectoryUrl,
907
+ })
904
908
  rollupBuild = sortObjectByPathnames(rollupBuild)
905
909
  buildManifest = sortObjectByPathnames(buildManifest)
906
910
  buildMappings = sortObjectByPathnames(buildMappings)
907
911
  buildFileContents = createBuildFileContents({
908
912
  rollupBuild,
909
- buildDirectoryUrl,
910
913
  })
911
914
  const buildDuration = Date.now() - buildStartMs
912
915
  buildStats = createBuildStats({
@@ -38,7 +38,12 @@ import {
38
38
  parseSrcset,
39
39
  stringifySrcset,
40
40
  } from "@jsenv/core/src/internal/compiling/compileHtml.js"
41
- import { setJavaScriptSourceMappingUrl } from "@jsenv/core/src/internal/sourceMappingURLUtils.js"
41
+ import {
42
+ getJavaScriptSourceMappingUrl,
43
+ setJavaScriptSourceMappingUrl,
44
+ getCssSourceMappingUrl,
45
+ setCssSourceMappingUrl,
46
+ } from "@jsenv/core/src/internal/sourceMappingURLUtils.js"
42
47
  import {
43
48
  getTargetAsBase64Url,
44
49
  targetIsReferencedOnlyByRessourceHint,
@@ -146,7 +151,11 @@ export const parseHtmlAsset = async (
146
151
  }
147
152
  }
148
153
 
149
- const regularScriptSrcVisitor = (script, { notifyReferenceFound }) => {
154
+ const regularScriptSrcVisitor = (
155
+ script,
156
+ { notifyReferenceFound },
157
+ htmlTarget,
158
+ ) => {
150
159
  const typeAttribute = getHtmlNodeAttributeByName(script, "type")
151
160
  if (
152
161
  typeAttribute &&
@@ -172,8 +181,21 @@ const regularScriptSrcVisitor = (script, { notifyReferenceFound }) => {
172
181
 
173
182
  if (shouldInline({ reference: remoteScriptReference, htmlNode: script })) {
174
183
  removeHtmlNodeAttribute(script, srcAttribute)
175
- const { targetBuildBuffer } = remoteScriptReference.target
176
- setHtmlNodeText(script, targetBuildBuffer)
184
+ const { target } = remoteScriptReference
185
+ const { targetBuildBuffer } = target
186
+ let jsString = String(targetBuildBuffer)
187
+
188
+ const sourcemapRelativeUrl = getJavaScriptSourceMappingUrl(jsString)
189
+ if (sourcemapRelativeUrl) {
190
+ const { targetBuildRelativeUrl } = target
191
+ const jsBuildUrl = resolveUrl(targetBuildRelativeUrl, "file:///")
192
+ const sourcemapBuildUrl = resolveUrl(sourcemapRelativeUrl, jsBuildUrl)
193
+ const htmlUrl = resolveUrl(htmlTarget.targetFileNamePattern, "file:///")
194
+ const sourcemapInlineUrl = urlToRelativeUrl(sourcemapBuildUrl, htmlUrl)
195
+ jsString = setJavaScriptSourceMappingUrl(jsString, sourcemapInlineUrl)
196
+ }
197
+
198
+ setHtmlNodeText(script, jsString)
177
199
  return
178
200
  }
179
201
 
@@ -259,14 +281,19 @@ const moduleScriptSrcVisitor = (script, { format, notifyReferenceFound }) => {
259
281
  // here put a warning if we cannot inline importmap because it would mess
260
282
  // the remapping (note that it's feasible) but not yet supported
261
283
  removeHtmlNodeAttribute(script, srcAttribute)
262
- const { targetBuildBuffer } = remoteScriptReference.target
263
- const jsString = String(targetBuildBuffer)
264
-
265
- const codeWithSourcemapComment = setJavaScriptSourceMappingUrl(
266
- jsString,
267
- `${remoteScriptReference.target.targetFileName}.map`
268
- )
269
- setHtmlNodeText(script, codeWithSourcemapComment)
284
+ const { target } = remoteScriptReference
285
+ const { targetBuildBuffer } = target
286
+ let jsString = String(targetBuildBuffer)
287
+
288
+ // at this stage, for some reason the sourcemap url is not in the js
289
+ // (it will be added sshortly after by "injectSourcemapInRollupBuild")
290
+ // but we know that a script type module have a sourcemap
291
+ // and will be next to html file
292
+ // with these assumptions we can force the sourcemap url
293
+ const sourcemapUrl = `${target.targetBuildRelativeUrl}.map`
294
+ jsString = setJavaScriptSourceMappingUrl(jsString, sourcemapUrl)
295
+
296
+ setHtmlNodeText(script, jsString)
270
297
  return
271
298
  }
272
299
 
@@ -433,7 +460,11 @@ const importmapScriptTextNodeVisitor = (
433
460
  }
434
461
  }
435
462
 
436
- const linkStylesheetHrefVisitor = (link, { notifyReferenceFound }) => {
463
+ const linkStylesheetHrefVisitor = (
464
+ link,
465
+ { notifyReferenceFound },
466
+ htmlTarget,
467
+ ) => {
437
468
  const hrefAttribute = getHtmlNodeAttributeByName(link, "href")
438
469
  if (!hrefAttribute) {
439
470
  return null
@@ -457,8 +488,19 @@ const linkStylesheetHrefVisitor = (link, { notifyReferenceFound }) => {
457
488
  }
458
489
 
459
490
  if (shouldInline({ reference: cssReference, htmlNode: link })) {
460
- const { targetBuildBuffer } = cssReference.target
461
- replaceHtmlNode(link, `<style>${targetBuildBuffer}</style>`)
491
+ const { target } = cssReference
492
+ const { targetBuildBuffer } = target
493
+ let cssString = String(targetBuildBuffer)
494
+ const sourcemapRelativeUrl = getCssSourceMappingUrl(cssString)
495
+ if (sourcemapRelativeUrl) {
496
+ const { targetBuildRelativeUrl } = target
497
+ const cssBuildUrl = resolveUrl(targetBuildRelativeUrl, "file:///")
498
+ const sourcemapBuildUrl = resolveUrl(sourcemapRelativeUrl, cssBuildUrl)
499
+ const htmlUrl = resolveUrl(htmlTarget.targetFileNamePattern, "file:///")
500
+ const sourcemapInlineUrl = urlToRelativeUrl(sourcemapBuildUrl, htmlUrl)
501
+ cssString = setCssSourceMappingUrl(cssString, sourcemapInlineUrl)
502
+ }
503
+ replaceHtmlNode(link, `<style>${cssString}</style>`)
462
504
  return
463
505
  }
464
506
 
@@ -0,0 +1,54 @@
1
+ import { resolveUrl, urlToRelativeUrl } from "@jsenv/filesystem"
2
+
3
+ import { setJavaScriptSourceMappingUrl } from "@jsenv/core/src/internal/sourceMappingURLUtils.js"
4
+
5
+ export const injectSourcemapInRollupBuild = (
6
+ rollupBuild,
7
+ { buildDirectoryUrl },
8
+ ) => {
9
+ const rollupBuildWithSourcemap = {}
10
+
11
+ Object.keys(rollupBuild).forEach((buildRelativeUrl) => {
12
+ const rollupFileInfo = rollupBuild[buildRelativeUrl]
13
+ const { type, code, map } = rollupFileInfo
14
+
15
+ if (type === "asset" || !map) {
16
+ rollupBuildWithSourcemap[buildRelativeUrl] = rollupFileInfo
17
+ return
18
+ }
19
+
20
+ const sourcemapBuildRelativeUrl = `${buildRelativeUrl}.map`
21
+ const sourcemapRollupFileInfo = rollupBuild[sourcemapBuildRelativeUrl]
22
+ if (sourcemapRollupFileInfo) {
23
+ rollupBuildWithSourcemap[buildRelativeUrl] = rollupFileInfo
24
+ return
25
+ }
26
+
27
+ const fileBuildUrl = resolveUrl(buildRelativeUrl, buildDirectoryUrl)
28
+ const sourcemapBuildUrl = resolveUrl(
29
+ sourcemapBuildRelativeUrl,
30
+ buildDirectoryUrl,
31
+ )
32
+ const fileSourcemapString = JSON.stringify(map, null, " ")
33
+ const sourcemapBuildUrlRelativeToFileBuildUrl = urlToRelativeUrl(
34
+ sourcemapBuildUrl,
35
+ fileBuildUrl,
36
+ )
37
+ const codeWithSourcemapComment = setJavaScriptSourceMappingUrl(
38
+ code,
39
+ sourcemapBuildUrlRelativeToFileBuildUrl,
40
+ )
41
+
42
+ rollupBuildWithSourcemap[sourcemapBuildRelativeUrl] = {
43
+ type: "asset",
44
+ fileName: sourcemapBuildRelativeUrl,
45
+ source: fileSourcemapString,
46
+ }
47
+ rollupBuildWithSourcemap[buildRelativeUrl] = {
48
+ ...rollupFileInfo,
49
+ code: codeWithSourcemapComment,
50
+ }
51
+ })
52
+
53
+ return rollupBuildWithSourcemap
54
+ }