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 CHANGED
@@ -85170,7 +85170,7 @@
85170
85170
  if (func.name === 'define') {
85171
85171
  let index = 0;
85172
85172
  let arg = args[index];
85173
- let name, deps;
85173
+ let name, deps, params;
85174
85174
  if (arg.type === 'Literal') {
85175
85175
  if (length <= ++index || typeof arg.value !== 'string') return false
85176
85176
  name = arg;
@@ -85182,21 +85182,91 @@
85182
85182
  arg = args[index];
85183
85183
  }
85184
85184
  if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
85185
- return { namespace, func, name, deps, factory: arg }
85185
+ if (!deps) {
85186
+ ({ params, deps } = detectCjsDeps(arg) || {});
85187
+ } else {
85188
+ params = arg.params;
85189
+ }
85190
+ return { namespace, func, name, deps, params, factory: arg }
85186
85191
  }
85187
85192
  return arg.type === 'ObjectExpression' && { namespace, func, name, deps, output: arg }
85188
85193
  }
85189
85194
 
85190
- // require([deps], success, error)
85195
+ // require([deps], success, [error])
85191
85196
  if (func.name === 'require') {
85192
- const deps = args[0];
85193
- if (deps.type === 'ArrayExpression' && length >= 2) {
85197
+ const arg = args[0];
85198
+ if (arg.type === 'ArrayExpression') {
85199
+ if (length < 2) return false
85194
85200
  const body = args[1];
85195
85201
  if (body.type === 'FunctionExpression' || body.type === 'ArrowFunctionExpression') {
85196
- return { func, deps, body }
85202
+ return { func, deps: arg, params: body.params, body }
85203
+ }
85204
+ } else if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
85205
+ const { params, deps } = detectCjsDeps(arg) || {};
85206
+ return { func, params, deps, body: arg }
85207
+ }
85208
+ }
85209
+ }
85210
+
85211
+ // Checks if the expression parameters can support CJS-like require calls
85212
+ // in the expression body.
85213
+ function isCjsExpression(expr) {
85214
+ const { params } = expr;
85215
+ if (!params.length) return false
85216
+ const param = params[0];
85217
+ return param.type === 'Identifier' && param.name === 'require'
85218
+ }
85219
+
85220
+ // Detects variable declarations with CJS-like require calls and such require
85221
+ // calls on the first level of the expression body.
85222
+ function detectCjsDeps(expr) {
85223
+ if (!isCjsExpression(expr)) return
85224
+ const params = [];
85225
+ const namedDeps = [];
85226
+ const unnamedDeps = [];
85227
+ for (const statement of expr.body.body || []) {
85228
+ const { type } = statement;
85229
+ if (type === 'VariableDeclaration') {
85230
+ for (const declarator of statement.declarations) {
85231
+ const { id } = declarator;
85232
+ if (id && id.type === 'Identifier') {
85233
+ const { init } = declarator;
85234
+ if (init && init.type === 'CallExpression') {
85235
+ const { callee } = init;
85236
+ if (callee.type === 'Identifier' && callee.name === 'require') {
85237
+ const { arguments: args } = init;
85238
+ if (args.length === 1) {
85239
+ const arg = args[0];
85240
+ if (arg.type === 'Literal') {
85241
+ params.push(id);
85242
+ namedDeps.push(arg);
85243
+ }
85244
+ }
85245
+ }
85246
+ }
85247
+ }
85248
+ }
85249
+ } else if (type === 'ExpressionStatement') {
85250
+ const { expression } = statement;
85251
+ if (expression.type === 'CallExpression') {
85252
+ const { callee } = expression;
85253
+ if (callee.type === 'Identifier' && callee.name === 'require') {
85254
+ const { arguments: args } = expression;
85255
+ if (args.length === 1) {
85256
+ const arg = args[0];
85257
+ if (arg.type === 'Literal') {
85258
+ unnamedDeps.push(arg);
85259
+ }
85260
+ }
85261
+ }
85197
85262
  }
85198
85263
  }
85199
85264
  }
85265
+ let deps = [...namedDeps, ...unnamedDeps];
85266
+ if (deps.length) {
85267
+ deps = { type: 'ArrayExpression', elements: deps };
85268
+ return { params, deps }
85269
+ }
85200
85270
  }
85201
85271
 
85202
85272
  // Detects if a program contains statements calling define or require function.