@jsenv/core 24.5.1 → 24.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/core",
3
- "version": "24.5.1",
3
+ "version": "24.5.5",
4
4
  "description": "Tool to develop, test and build js projects",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -20,14 +20,31 @@ const fileNamePatternFromRessource = (ressource) => {
20
20
  }
21
21
 
22
22
  if (ressource.urlVersioningDisabled) {
23
- if (ressource.isEntryPoint || ressource.isJsModule) {
24
- return `[name][extname]`
23
+ if (canMoveToAssetsDirectory(ressource)) {
24
+ return assetFileNamePatternWithoutHash
25
25
  }
26
- return assetFileNamePatternWithoutHash
26
+ return `[name][extname]`
27
27
  }
28
28
 
29
- if (ressource.isEntryPoint || ressource.isJsModule) {
30
- return `[name]-[hash][extname]`
29
+ if (canMoveToAssetsDirectory(ressource)) {
30
+ return assetFileNamePattern
31
31
  }
32
- return assetFileNamePattern
32
+ return `[name]-[hash][extname]`
33
+ }
34
+
35
+ const canMoveToAssetsDirectory = (ressource) => {
36
+ if (ressource.isEntryPoint) {
37
+ return false
38
+ }
39
+ // in theory js module can be moved to assets directory
40
+ // but that needs to be tested
41
+ if (ressource.isJsModule) {
42
+ return false
43
+ }
44
+ // service worker MUST be at the root (same level than the HTML file)
45
+ // otherwise it might be registered for the scope "/assets/" instead of "/"
46
+ if (ressource.isServiceWorker) {
47
+ return false
48
+ }
49
+ return true
33
50
  }
@@ -69,7 +69,7 @@ export const parseJsRessource = async (
69
69
  if (minify) {
70
70
  const result = await minifyJs({
71
71
  url: map ? asProjectUrl(jsCompiledUrl) : jsOriginalUrl,
72
- code: jsString,
72
+ code,
73
73
  map,
74
74
  toplevel: false,
75
75
  })
@@ -1560,21 +1560,20 @@ const finalizeServiceWorkers = async ({
1560
1560
  Object.keys(serviceWorkers).map(async (projectRelativeUrl) => {
1561
1561
  const projectUrl = resolveUrl(projectRelativeUrl, "file://")
1562
1562
  projectRelativeUrl = urlToRelativeUrl(projectUrl, "file://")
1563
- const buildRelativeUrl = buildMappings[projectRelativeUrl]
1564
- if (!buildRelativeUrl) {
1563
+ const serviceWorkerBuildRelativeUrl = buildMappings[projectRelativeUrl]
1564
+ if (!serviceWorkerBuildRelativeUrl) {
1565
1565
  throw new Error(
1566
1566
  `"${projectRelativeUrl}" service worker file missing in the build`,
1567
1567
  )
1568
1568
  }
1569
- const buildFileContent = rollupBuild[buildRelativeUrl].source
1570
- rollupBuild[buildRelativeUrl].source = serviceWorkerFinalizer(
1571
- buildFileContent,
1572
- {
1569
+ const buildFileContent = rollupBuild[serviceWorkerBuildRelativeUrl].source
1570
+ rollupBuild[serviceWorkerBuildRelativeUrl].source =
1571
+ serviceWorkerFinalizer(buildFileContent, {
1572
+ serviceWorkerBuildRelativeUrl,
1573
1573
  buildManifest,
1574
1574
  rollupBuild,
1575
1575
  lineBreakNormalization,
1576
- },
1577
- )
1576
+ })
1578
1577
  }),
1579
1578
  )
1580
1579
  }
@@ -1,19 +1,40 @@
1
+ import { resolveUrl, urlToRelativeUrl } from "@jsenv/filesystem"
2
+
1
3
  import { generateContentHash } from "./internal/building/url-versioning.js"
2
4
 
3
5
  export const jsenvServiceWorkerFinalizer = (
4
6
  code,
5
- { buildManifest, rollupBuild, lineBreakNormalization },
7
+ {
8
+ serviceWorkerBuildRelativeUrl,
9
+ buildManifest,
10
+ rollupBuild,
11
+ lineBreakNormalization,
12
+ },
6
13
  ) => {
7
14
  const generatedUrlsConfig = {}
8
15
  Object.keys(buildManifest).forEach((projectRelativeUrl) => {
9
16
  if (projectRelativeUrl.endsWith(".map")) {
10
17
  return
11
18
  }
19
+
12
20
  const buildRelativeUrl = buildManifest[projectRelativeUrl]
21
+ const buildUrl = resolveUrl(buildRelativeUrl, "file://")
22
+ const serviceWorkerBuildUrl = resolveUrl(
23
+ serviceWorkerBuildRelativeUrl,
24
+ "file://",
25
+ )
26
+ const urlRelativeToServiceWorker = urlToRelativeUrl(
27
+ buildUrl,
28
+ serviceWorkerBuildUrl,
29
+ )
30
+ if (urlRelativeToServiceWorker === "") {
31
+ // don't put the service worker itself
32
+ return
33
+ }
13
34
  const versioned = fileNameContainsHash(buildRelativeUrl)
14
35
  const rollupFile = rollupBuild[buildRelativeUrl]
15
36
 
16
- generatedUrlsConfig[buildRelativeUrl] = {
37
+ generatedUrlsConfig[urlRelativeToServiceWorker] = {
17
38
  versioned,
18
39
  ...(versioned
19
40
  ? {}