@rollup/plugin-commonjs 22.0.0-3 → 22.0.0-4

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/index.es.js CHANGED
@@ -7,9 +7,9 @@ import { walk } from 'estree-walker';
7
7
  import MagicString from 'magic-string';
8
8
  import isReference from 'is-reference';
9
9
 
10
- var version = "21.0.1";
10
+ var version = "22.0.0-4";
11
11
  var peerDependencies = {
12
- rollup: "^2.60.0"
12
+ rollup: "^2.61.1"
13
13
  };
14
14
 
15
15
  function tryParse(parse, code, id) {
@@ -194,6 +194,9 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
194
194
 
195
195
  const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
196
196
 
197
+ const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
198
+ const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
199
+
197
200
  function getDynamicModuleRegistry(
198
201
  isDynamicRequireModulesEnabled,
199
202
  dynamicRequireModules,
@@ -201,7 +204,7 @@ function getDynamicModuleRegistry(
201
204
  ignoreDynamicRequires
202
205
  ) {
203
206
  if (!isDynamicRequireModulesEnabled) {
204
- return `export function commonjsRequire(path) {
207
+ return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
205
208
  ${FAILED_REQUIRE_ERROR}
206
209
  }`;
207
210
  }
@@ -231,25 +234,25 @@ ${dynamicModuleProps}
231
234
  });
232
235
  }
233
236
 
234
- export function commonjsRequire(path, originalModuleDir) {
235
- var resolvedPath = commonjsResolveImpl(path, originalModuleDir);
236
- if (resolvedPath !== null) {
237
- return getDynamicModules()[resolvedPath]();
237
+ export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
238
+ function handleRequire(path) {
239
+ var resolvedPath = commonjsResolve(path, originalModuleDir);
240
+ if (resolvedPath !== null) {
241
+ return getDynamicModules()[resolvedPath]();
242
+ }
243
+ ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
238
244
  }
239
- ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
240
- }
241
-
242
- function commonjsResolve (path, originalModuleDir) {
243
- const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
244
- if (resolvedPath !== null) {
245
- return resolvedPath;
245
+ handleRequire.resolve = function (path) {
246
+ var resolvedPath = commonjsResolve(path, originalModuleDir);
247
+ if (resolvedPath !== null) {
248
+ return resolvedPath;
249
+ }
250
+ return require.resolve(path);
246
251
  }
247
- return require.resolve(path);
252
+ return handleRequire;
248
253
  }
249
254
 
250
- commonjsRequire.resolve = commonjsResolve;
251
-
252
- function commonjsResolveImpl (path, originalModuleDir) {
255
+ function commonjsResolve (path, originalModuleDir) {
253
256
  var shouldTryNodeModules = isPossibleNodeModulesPath(path);
254
257
  path = normalize(path);
255
258
  var relPath;
@@ -661,23 +664,33 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
661
664
  };
662
665
  }
663
666
 
664
- function validateRollupVersion(rollupVersion, peerDependencyVersion) {
665
- const [major, minor] = rollupVersion.split('.').map(Number);
666
- const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
667
+ function validateVersion(actualVersion, peerDependencyVersion, name) {
668
+ const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
667
669
  let minMajor = Infinity;
668
670
  let minMinor = Infinity;
671
+ let minPatch = Infinity;
669
672
  let foundVersion;
670
673
  // eslint-disable-next-line no-cond-assign
671
674
  while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
672
- const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
675
+ const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
673
676
  if (foundMajor < minMajor) {
674
677
  minMajor = foundMajor;
675
678
  minMinor = foundMinor;
679
+ minPatch = foundPatch;
676
680
  }
677
681
  }
678
- if (major < minMajor || (major === minMajor && minor < minMinor)) {
682
+ if (!actualVersion) {
683
+ throw new Error(
684
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
685
+ );
686
+ }
687
+ const [major, minor, patch] = actualVersion.split('.').map(Number);
688
+ if (
689
+ major < minMajor ||
690
+ (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
691
+ ) {
679
692
  throw new Error(
680
- `Insufficient Rollup version: "@rollup/plugin-commonjs" requires at least rollup@${minMajor}.${minMinor} but found rollup@${rollupVersion}.`
693
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
681
694
  );
682
695
  }
683
696
  }
@@ -1123,14 +1136,16 @@ function getRequireHandlers() {
1123
1136
  resolveRequireSourcesAndGetMeta,
1124
1137
  needsRequireWrapper,
1125
1138
  isEsModule,
1126
- usesRequire,
1139
+ isDynamicRequireModulesEnabled,
1127
1140
  getIgnoreTryCatchRequireStatementMode
1128
1141
  ) {
1129
1142
  const imports = [];
1130
1143
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
1131
- if (usesRequire) {
1144
+ if (dynamicRequireName) {
1132
1145
  imports.push(
1133
- `import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1146
+ `import { ${
1147
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1148
+ } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1134
1149
  );
1135
1150
  }
1136
1151
  if (exportMode === 'module') {
@@ -1419,12 +1434,6 @@ async function transformCommonjs(
1419
1434
  checkDynamicRequire(node.start);
1420
1435
  uses.require = true;
1421
1436
  const requireNode = node.callee.object;
1422
- magicString.appendLeft(
1423
- node.end - 1,
1424
- `,${JSON.stringify(
1425
- dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1426
- )}`
1427
- );
1428
1437
  replacedDynamicRequires.push(requireNode);
1429
1438
  return;
1430
1439
  }
@@ -1439,12 +1448,6 @@ async function transformCommonjs(
1439
1448
  if (hasDynamicArguments(node)) {
1440
1449
  if (isDynamicRequireModulesEnabled) {
1441
1450
  checkDynamicRequire(node.start);
1442
- magicString.appendLeft(
1443
- node.end - 1,
1444
- `, ${JSON.stringify(
1445
- dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1446
- )}`
1447
- );
1448
1451
  }
1449
1452
  if (!ignoreDynamicRequires) {
1450
1453
  replacedDynamicRequires.push(node.callee);
@@ -1618,7 +1621,13 @@ async function transformCommonjs(
1618
1621
  const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
1619
1622
  const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
1620
1623
  const helpersName = deconflict([scope], globals, 'commonjsHelpers');
1621
- const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
1624
+ const dynamicRequireName =
1625
+ replacedDynamicRequires.length > 0 &&
1626
+ deconflict(
1627
+ [scope],
1628
+ globals,
1629
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1630
+ );
1622
1631
  const deconflictedExportNames = Object.create(null);
1623
1632
  for (const [exportName, { scopes }] of exportsAssignmentsByName) {
1624
1633
  deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
@@ -1630,10 +1639,17 @@ async function transformCommonjs(
1630
1639
  });
1631
1640
  }
1632
1641
  for (const node of replacedDynamicRequires) {
1633
- magicString.overwrite(node.start, node.end, dynamicRequireName, {
1634
- contentOnly: true,
1635
- storeName: true
1636
- });
1642
+ magicString.overwrite(
1643
+ node.start,
1644
+ node.end,
1645
+ isDynamicRequireModulesEnabled
1646
+ ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
1647
+ : dynamicRequireName,
1648
+ {
1649
+ contentOnly: true,
1650
+ storeName: true
1651
+ }
1652
+ );
1637
1653
  }
1638
1654
 
1639
1655
  // We cannot wrap ES/mixed modules
@@ -1688,7 +1704,7 @@ async function transformCommonjs(
1688
1704
  resolveRequireSourcesAndGetMeta,
1689
1705
  needsRequireWrapper,
1690
1706
  isEsModule,
1691
- uses.require,
1707
+ isDynamicRequireModulesEnabled,
1692
1708
  getIgnoreTryCatchRequireStatementMode
1693
1709
  );
1694
1710
  const exportBlock = isEsModule
@@ -1907,8 +1923,12 @@ function commonjs(options = {}) {
1907
1923
  return { ...rawOptions, plugins };
1908
1924
  },
1909
1925
 
1910
- buildStart() {
1911
- validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
1926
+ buildStart({ plugins }) {
1927
+ validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
1928
+ const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
1929
+ if (nodeResolve) {
1930
+ validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
1931
+ }
1912
1932
  if (options.namedExports != null) {
1913
1933
  this.warn(
1914
1934
  'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'