requirejs-esm 2.2.1 → 2.3.1

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": "requirejs-esm",
3
- "version": "2.2.1",
3
+ "version": "2.3.1",
4
4
  "description": "A RequireJS plugin converting JavaScript modules from ESM to AMD.",
5
5
  "author": "Ferdinand Prantl <prantlf@gmail.com>",
6
6
  "license": "MIT",
@@ -84,29 +84,29 @@
84
84
  },
85
85
  "dependencies": {
86
86
  "@prantlf/convert-source-map": "^2.0.0",
87
- "astring": "^1.8.3",
87
+ "astring": "^1.8.4",
88
88
  "charcodes": "^0.2.0",
89
- "commander": "^9.3.0",
90
- "meriyah": "^4.2.1",
91
- "punycode": "^2.1.1",
89
+ "commander": "^10.0.1",
90
+ "meriyah": "^4.3.5",
91
+ "punycode": "^2.3.0",
92
92
  "source-map": "^0.8.0-beta.0",
93
93
  "tiny-glob": "^0.2.9"
94
94
  },
95
95
  "devDependencies": {
96
- "@prantlf/requirejs": "^3.0.0",
97
- "@rollup/plugin-commonjs": "^22.0.1",
98
- "@rollup/plugin-json": "^4.1.0",
99
- "@rollup/plugin-node-resolve": "^13.3.0",
100
- "@semantic-release/changelog": "^6.0.1",
96
+ "@prantlf/requirejs": "^3.0.2",
97
+ "@rollup/plugin-commonjs": "^24.1.0",
98
+ "@rollup/plugin-json": "^6.0.0",
99
+ "@rollup/plugin-node-resolve": "^15.0.2",
100
+ "@semantic-release/changelog": "^6.0.3",
101
101
  "@semantic-release/git": "^10.0.1",
102
- "c8": "^7.11.3",
103
- "eslint": "^8.19.0",
102
+ "c8": "^7.13.0",
103
+ "eslint": "^8.40.0",
104
104
  "lit-html": "^1",
105
- "rollup": "^2.76.0",
105
+ "rollup": "^3.21.5",
106
106
  "tehanu": "^1.0.1",
107
107
  "tehanu-repo-coco": "^1.0.0",
108
108
  "tehanu-teru": "^1.0.0",
109
- "terser": "^5.14.1"
109
+ "terser": "^5.17.1"
110
110
  },
111
111
  "keywords": [
112
112
  "requirejs-plugin",
package/src/api.d.ts CHANGED
@@ -19,7 +19,8 @@ declare function detectDefinesOrRequires(ast: object): {
19
19
  namespace?: object
20
20
  func: object
21
21
  name?: object
22
- deps?: object[]
22
+ deps?: object
23
+ params?: object[]
23
24
  factory?: object
24
25
  output?: object
25
26
  body?: object
@@ -26,7 +26,7 @@ function detectDefineOrRequireCall(expr) {
26
26
  if (func.name === 'define') {
27
27
  let index = 0
28
28
  let arg = args[index]
29
- let name, deps
29
+ let name, deps, params
30
30
  if (arg.type === 'Literal') {
31
31
  if (length <= ++index || typeof arg.value !== 'string') return false
32
32
  name = arg
@@ -38,23 +38,93 @@ function detectDefineOrRequireCall(expr) {
38
38
  arg = args[index]
39
39
  }
40
40
  if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
41
- return { namespace, func, name, deps, factory: arg }
41
+ if (!deps) {
42
+ ({ params, deps } = detectCjsDeps(arg) || {})
43
+ } else {
44
+ params = arg.params
45
+ }
46
+ return { namespace, func, name, deps, params, factory: arg }
42
47
  }
43
48
  return arg.type === 'ObjectExpression' && { namespace, func, name, deps, output: arg }
44
49
  }
45
50
 
46
- // require([deps], success, error)
51
+ // require([deps], success, [error])
47
52
  if (func.name === 'require') {
48
- const deps = args[0]
49
- if (deps.type === 'ArrayExpression' && length >= 2) {
53
+ const arg = args[0]
54
+ if (arg.type === 'ArrayExpression') {
55
+ if (length < 2) return false
50
56
  const body = args[1]
51
57
  if (body.type === 'FunctionExpression' || body.type === 'ArrowFunctionExpression') {
52
- return { func, deps, body }
58
+ return { func, deps: arg, params: body.params, body }
53
59
  }
60
+ } else if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
61
+ const { params, deps } = detectCjsDeps(arg) || {}
62
+ return { func, params, deps, body: arg }
54
63
  }
55
64
  }
56
65
  }
57
66
 
67
+ // Checks if the expression parameters can support CJS-like require calls
68
+ // in the expression body.
69
+ function isCjsExpression(expr) {
70
+ const { params } = expr;
71
+ if (!params.length) return false
72
+ const param = params[0]
73
+ return param.type === 'Identifier' && param.name === 'require'
74
+ }
75
+
76
+ // Detects variable declarations with CJS-like require calls and such require
77
+ // calls on the first level of the expression body.
78
+ function detectCjsDeps(expr) {
79
+ if (!isCjsExpression(expr)) return
80
+ const params = []
81
+ const namedDeps = []
82
+ const unnamedDeps = []
83
+ for (const statement of expr.body.body || []) {
84
+ const { type } = statement
85
+ if (type === 'VariableDeclaration') {
86
+ for (const declarator of statement.declarations) {
87
+ const { id } = declarator
88
+ if (id && id.type === 'Identifier') {
89
+ const { init } = declarator
90
+ if (init && init.type === 'CallExpression') {
91
+ const { callee } = init
92
+ if (callee.type === 'Identifier' && callee.name === 'require') {
93
+ const { arguments: args } = init
94
+ if (args.length === 1) {
95
+ const arg = args[0]
96
+ if (arg.type === 'Literal') {
97
+ params.push(id)
98
+ namedDeps.push(arg)
99
+ }
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ } else if (type === 'ExpressionStatement') {
106
+ const { expression } = statement
107
+ if (expression.type === 'CallExpression') {
108
+ const { callee } = expression
109
+ if (callee.type === 'Identifier' && callee.name === 'require') {
110
+ const { arguments: args } = expression
111
+ if (args.length === 1) {
112
+ const arg = args[0]
113
+ if (arg.type === 'Literal') {
114
+ unnamedDeps.push(arg)
115
+ }
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
121
+ let deps = [...namedDeps, ...unnamedDeps]
122
+ if (deps.length) {
123
+ deps = { type: 'ArrayExpression', elements: deps }
124
+ return { params, deps }
125
+ }
126
+ }
127
+
58
128
  // Detects if a program contains statements calling define or require function.
59
129
  // Returns information about AMD modules false, { deps } or [{ deps }, ...].
60
130
  function detectDefineOrRequire(stat) {