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/plugin.js CHANGED
@@ -85265,7 +85265,7 @@
85265
85265
  if (func.name === 'define') {
85266
85266
  let index = 0;
85267
85267
  let arg = args[index];
85268
- let name, deps;
85268
+ let name, deps, params;
85269
85269
  if (arg.type === 'Literal') {
85270
85270
  if (length <= ++index || typeof arg.value !== 'string') return false
85271
85271
  name = arg;
@@ -85277,21 +85277,91 @@
85277
85277
  arg = args[index];
85278
85278
  }
85279
85279
  if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
85280
- return { namespace, func, name, deps, factory: arg }
85280
+ if (!deps) {
85281
+ ({ params, deps } = detectCjsDeps(arg) || {});
85282
+ } else {
85283
+ params = arg.params;
85284
+ }
85285
+ return { namespace, func, name, deps, params, factory: arg }
85281
85286
  }
85282
85287
  return arg.type === 'ObjectExpression' && { namespace, func, name, deps, output: arg }
85283
85288
  }
85284
85289
 
85285
- // require([deps], success, error)
85290
+ // require([deps], success, [error])
85286
85291
  if (func.name === 'require') {
85287
- const deps = args[0];
85288
- if (deps.type === 'ArrayExpression' && length >= 2) {
85292
+ const arg = args[0];
85293
+ if (arg.type === 'ArrayExpression') {
85294
+ if (length < 2) return false
85289
85295
  const body = args[1];
85290
85296
  if (body.type === 'FunctionExpression' || body.type === 'ArrowFunctionExpression') {
85291
- return { func, deps, body }
85297
+ return { func, deps: arg, params: body.params, body }
85298
+ }
85299
+ } else if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
85300
+ const { params, deps } = detectCjsDeps(arg) || {};
85301
+ return { func, params, deps, body: arg }
85302
+ }
85303
+ }
85304
+ }
85305
+
85306
+ // Checks if the expression parameters can support CJS-like require calls
85307
+ // in the expression body.
85308
+ function isCjsExpression(expr) {
85309
+ const { params } = expr;
85310
+ if (!params.length) return false
85311
+ const param = params[0];
85312
+ return param.type === 'Identifier' && param.name === 'require'
85313
+ }
85314
+
85315
+ // Detects variable declarations with CJS-like require calls and such require
85316
+ // calls on the first level of the expression body.
85317
+ function detectCjsDeps(expr) {
85318
+ if (!isCjsExpression(expr)) return
85319
+ const params = [];
85320
+ const namedDeps = [];
85321
+ const unnamedDeps = [];
85322
+ for (const statement of expr.body.body || []) {
85323
+ const { type } = statement;
85324
+ if (type === 'VariableDeclaration') {
85325
+ for (const declarator of statement.declarations) {
85326
+ const { id } = declarator;
85327
+ if (id && id.type === 'Identifier') {
85328
+ const { init } = declarator;
85329
+ if (init && init.type === 'CallExpression') {
85330
+ const { callee } = init;
85331
+ if (callee.type === 'Identifier' && callee.name === 'require') {
85332
+ const { arguments: args } = init;
85333
+ if (args.length === 1) {
85334
+ const arg = args[0];
85335
+ if (arg.type === 'Literal') {
85336
+ params.push(id);
85337
+ namedDeps.push(arg);
85338
+ }
85339
+ }
85340
+ }
85341
+ }
85342
+ }
85343
+ }
85344
+ } else if (type === 'ExpressionStatement') {
85345
+ const { expression } = statement;
85346
+ if (expression.type === 'CallExpression') {
85347
+ const { callee } = expression;
85348
+ if (callee.type === 'Identifier' && callee.name === 'require') {
85349
+ const { arguments: args } = expression;
85350
+ if (args.length === 1) {
85351
+ const arg = args[0];
85352
+ if (arg.type === 'Literal') {
85353
+ unnamedDeps.push(arg);
85354
+ }
85355
+ }
85356
+ }
85292
85357
  }
85293
85358
  }
85294
85359
  }
85360
+ let deps = [...namedDeps, ...unnamedDeps];
85361
+ if (deps.length) {
85362
+ deps = { type: 'ArrayExpression', elements: deps };
85363
+ return { params, deps }
85364
+ }
85295
85365
  }
85296
85366
 
85297
85367
  // Detects if a program contains statements calling define or require function.