@jsenv/plugin-commonjs 1.0.0 → 1.0.3

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": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,21 +13,21 @@
13
13
  "type": "module",
14
14
  "main": "./main.js",
15
15
  "dependencies": {
16
- "@jsenv/filesystem": "4.1.2",
17
- "@jsenv/log": "3.2.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
- "@rollup/plugin-commonjs": "22.0.1",
20
+ "@rollup/plugin-commonjs": "22.0.2",
21
21
  "@rollup/plugin-json": "4.1.0",
22
- "@rollup/plugin-node-resolve": "13.3.0",
22
+ "@rollup/plugin-node-resolve": "14.0.0",
23
23
  "@rollup/plugin-replace": "4.0.0",
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.2",
27
+ "rollup": "2.79.0",
28
28
  "rollup-plugin-node-globals": "1.4.0",
29
29
  "rollup-plugin-polyfill-node": "0.10.2",
30
- "vm2": "3.9.10"
30
+ "vm2": "3.9.11"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@jsenv/core": "../../"
@@ -1,5 +1,6 @@
1
1
  import { URL_META } from "@jsenv/url-meta"
2
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,30 +53,32 @@ 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
83
  processEnvNodeEnv: context.scenarios.dev ? "development" : "production",
60
84
  ...urlInfo.data.commonjs,
@@ -62,22 +86,13 @@ export const jsenvPluginCommonJs = ({
62
86
  if (isValid) {
63
87
  urlInfo.isValid = isValid
64
88
  }
65
- const originalReference = context.reference.original
66
- ? context.reference.original
67
- : context.reference
68
- const filename = originalReference.dependsOnPackageJson
69
- ? `${originalReference.specifier}${urlToExtension(
70
- originalReference.parentUrl,
71
- )}`
72
- : undefined
73
89
  return {
74
90
  content,
75
91
  contentType: "text/javascript",
76
92
  type: "js_module",
77
- originalUrl: originalUrlInfo.originalUrl,
78
- originalContent: originalUrlInfo.originalContent,
93
+ originalUrl: commonJsUrlInfo.originalUrl,
94
+ originalContent: commonJsUrlInfo.originalContent,
79
95
  sourcemap,
80
- filename,
81
96
  }
82
97
  },
83
98
  }