@jsenv/plugin-commonjs 0.4.0 → 1.0.2

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/plugin-commonjs",
3
- "version": "0.4.0",
3
+ "version": "1.0.2",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,8 +13,8 @@
13
13
  "type": "module",
14
14
  "main": "./main.js",
15
15
  "dependencies": {
16
- "@jsenv/filesystem": "4.1.1",
17
- "@jsenv/log": "3.1.0",
16
+ "@jsenv/filesystem": "4.1.3",
17
+ "@jsenv/log": "3.3.0",
18
18
  "@jsenv/url-meta": "7.0.0",
19
19
  "@jsenv/urls": "1.2.7",
20
20
  "@rollup/plugin-commonjs": "22.0.1",
@@ -24,9 +24,9 @@
24
24
  "cjs-module-lexer": "1.2.2",
25
25
  "is-valid-identifier": "2.0.2",
26
26
  "resolve": "1.22.1",
27
- "rollup": "2.77.0",
27
+ "rollup": "2.77.2",
28
28
  "rollup-plugin-node-globals": "1.4.0",
29
- "rollup-plugin-polyfill-node": "0.10.1",
29
+ "rollup-plugin-polyfill-node": "0.10.2",
30
30
  "vm2": "3.9.10"
31
31
  },
32
32
  "devDependencies": {
@@ -9,7 +9,7 @@ import { validateCompileCache } from "./validate_compile_cache.js"
9
9
  import { createLockRegistry } from "./file_lock_registry.js"
10
10
  import { updateCompileCache } from "./update_compile_cache.js"
11
11
 
12
- const { lockForRessource } = createLockRegistry()
12
+ const { lockForResource } = createLockRegistry()
13
13
 
14
14
  export const reuseOrCreateCompiledFile = async ({
15
15
  logLevel,
@@ -165,7 +165,7 @@ const readCompileContextFile = ({ logger, compileContextJsonFileUrl }) => {
165
165
  }
166
166
 
167
167
  const startAsap = async (fn, { compiledFileUrl }) => {
168
- const unlockLocal = await lockForRessource(compiledFileUrl)
168
+ const unlockLocal = await lockForResource(compiledFileUrl)
169
169
  try {
170
170
  return await fn()
171
171
  } finally {
@@ -1,13 +1,13 @@
1
1
  export const createLockRegistry = () => {
2
2
  let lockArray = []
3
- const lockForRessource = async (ressource) => {
4
- const currentLock = lockArray.find((lock) => lock.ressource === ressource)
3
+ const lockForResource = async (resource) => {
4
+ const currentLock = lockArray.find((lock) => lock.resource === resource)
5
5
  let unlockResolve
6
6
  const unlocked = new Promise((resolve) => {
7
7
  unlockResolve = resolve
8
8
  })
9
9
  const lock = {
10
- ressource,
10
+ resource,
11
11
  unlocked,
12
12
  }
13
13
  lockArray = [...lockArray, lock]
@@ -20,5 +20,5 @@ export const createLockRegistry = () => {
20
20
  }
21
21
  return unlock
22
22
  }
23
- return { lockForRessource }
23
+ return { lockForResource }
24
24
  }
@@ -1,5 +1,6 @@
1
1
  import { URL_META } from "@jsenv/url-meta"
2
- import { injectQueryParams } from "@jsenv/urls"
2
+ import { injectQueryParams, urlToExtension } from "@jsenv/urls"
3
+ import { defaultLookupPackageScope } from "@jsenv/node-esm-resolution"
3
4
 
4
5
  import { commonJsToJsModule } from "./cjs_to_esm.js"
5
6
 
@@ -8,6 +9,23 @@ export const jsenvPluginCommonJs = ({
8
9
  logLevel,
9
10
  include,
10
11
  }) => {
12
+ const markAsJsModuleProxy = (reference) => {
13
+ reference.expectedType = "js_module"
14
+ const packageFileUrl = defaultLookupPackageScope(reference.url)
15
+ if (packageFileUrl) {
16
+ reference.filename = `${reference.specifier}${urlToExtension(
17
+ reference.parentUrl,
18
+ )}`
19
+ }
20
+ }
21
+ const turnIntoJsModuleProxy = (reference) => {
22
+ const urlTransformed = injectQueryParams(reference.url, {
23
+ cjs_as_js_module: "",
24
+ })
25
+ markAsJsModuleProxy(reference)
26
+ return urlTransformed
27
+ }
28
+
11
29
  let associations
12
30
 
13
31
  return {
@@ -23,6 +41,10 @@ export const jsenvPluginCommonJs = ({
23
41
  },
24
42
  redirectUrl: {
25
43
  js_import_export: (reference) => {
44
+ if (reference.searchParams.has("cjs_as_js_module")) {
45
+ markAsJsModuleProxy(reference)
46
+ return null
47
+ }
26
48
  const { commonjs } = URL_META.applyAssociations({
27
49
  url: reference.url,
28
50
  associations,
@@ -31,35 +53,34 @@ export const jsenvPluginCommonJs = ({
31
53
  return null
32
54
  }
33
55
  reference.data.commonjs = commonjs
34
- return injectQueryParams(reference.url, {
35
- cjs_as_js_module: "",
36
- })
56
+ return turnIntoJsModuleProxy(reference)
37
57
  },
38
58
  },
39
59
  fetchUrlContent: async (urlInfo, context) => {
40
- const originalUrlInfo = await context.fetchOriginalUrlInfo({
41
- urlInfo,
42
- context,
43
- searchParam: "cjs_as_js_module",
44
- // during this fetch we don't want to alter the original file
45
- // so we consider it as text
46
- expectedType: "text",
47
- })
48
- if (!originalUrlInfo) {
60
+ const [commonJsReference, commonJsUrlInfo] =
61
+ context.getWithoutSearchParam({
62
+ urlInfo,
63
+ context,
64
+ searchParam: "cjs_as_js_module",
65
+ // during this fetch we don't want to alter the original file
66
+ // so we consider it as text
67
+ expectedType: "text",
68
+ })
69
+ if (!commonJsReference) {
49
70
  return null
50
71
  }
72
+ await context.fetchUrlContent(commonJsUrlInfo, {
73
+ reference: commonJsReference,
74
+ })
51
75
  const nodeRuntimeEnabled = Object.keys(context.runtimeCompat).includes(
52
76
  "node",
53
77
  )
54
78
  const { content, sourcemap, isValid } = await commonJsToJsModule({
55
79
  logLevel,
56
80
  rootDirectoryUrl: context.rootDirectoryUrl,
57
- sourceFileUrl: originalUrlInfo.url,
81
+ sourceFileUrl: commonJsUrlInfo.url,
58
82
  browsers: !nodeRuntimeEnabled,
59
- processEnvNodeEnv:
60
- context.scenario === "dev" || context.scenario === "test"
61
- ? "development"
62
- : "production",
83
+ processEnvNodeEnv: context.scenarios.dev ? "development" : "production",
63
84
  ...urlInfo.data.commonjs,
64
85
  })
65
86
  if (isValid) {
@@ -69,8 +90,8 @@ export const jsenvPluginCommonJs = ({
69
90
  content,
70
91
  contentType: "text/javascript",
71
92
  type: "js_module",
72
- originalUrl: originalUrlInfo.originalUrl,
73
- originalContent: originalUrlInfo.originalContent,
93
+ originalUrl: commonJsUrlInfo.originalUrl,
94
+ originalContent: commonJsUrlInfo.originalContent,
74
95
  sourcemap,
75
96
  }
76
97
  },
@@ -13,5 +13,5 @@ await startDevServer({
13
13
  }),
14
14
  ],
15
15
  clientAutoreload: false,
16
- htmlSupervisor: false,
16
+ supervisor: false,
17
17
  })
@@ -29,7 +29,7 @@ const devServer = await startDevServer({
29
29
  }),
30
30
  ],
31
31
  clientAutoreload: false,
32
- htmlSupervisor: false,
32
+ supervisor: false,
33
33
  })
34
34
  const browser = await chromium.launch({
35
35
  headless: !debug,