requirejs-esm 2.2.1 → 2.3.0
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/api.js +76 -6
- package/dist/api.js.map +1 -1
- package/dist/plugin.js +76 -6
- package/dist/plugin.js.map +1 -1
- package/package.json +1 -1
- package/src/api.d.ts +2 -1
- package/src/transformer/amd.js +76 -6
package/package.json
CHANGED
package/src/api.d.ts
CHANGED
package/src/transformer/amd.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
49
|
-
if (
|
|
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) {
|