@rollup/plugin-commonjs 28.0.6 → 28.0.8

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/cjs/index.js CHANGED
@@ -11,7 +11,7 @@ var estreeWalker = require('estree-walker');
11
11
  var MagicString = require('magic-string');
12
12
  var isReference = require('is-reference');
13
13
 
14
- var version = "28.0.6";
14
+ var version = "28.0.8";
15
15
  var peerDependencies = {
16
16
  rollup: "^2.68.0||^3.0.0||^4.0.0"
17
17
  };
@@ -494,6 +494,19 @@ function getEsImportProxy(id, defaultIsModuleExports, moduleSideEffects) {
494
494
  };
495
495
  }
496
496
 
497
+ // For external Node built-ins required from wrapped CommonJS modules, we must not
498
+ // hoist an ESM import of the built-in (which would eagerly load it). Instead,
499
+ // expose a lazy `__require()` that resolves the built-in at runtime via
500
+ // `createRequire(import.meta.url)`.
501
+ function getExternalBuiltinRequireProxy(id) {
502
+ const stringifiedId = JSON.stringify(id);
503
+ return (
504
+ `import { createRequire } from 'node:module';\n` +
505
+ `const require = createRequire(import.meta.url);\n` +
506
+ `export function __require() { return require(${stringifiedId}); }`
507
+ );
508
+ }
509
+
497
510
  /* eslint-disable no-param-reassign, no-undefined */
498
511
 
499
512
 
@@ -823,13 +836,39 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
823
836
  fullyAnalyzedModules[parentId] = true;
824
837
  return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
825
838
  // eslint-disable-next-line no-multi-assign
826
- const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
839
+ let isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
827
840
  getTypeForFullyAnalyzedModule(dependencyId));
841
+ // Special-case external Node built-ins to be handled via a lazy __require
842
+ // helper instead of hoisted ESM imports when strict wrapping is used.
843
+ const isExternalWrapped = isWrappedId(dependencyId, EXTERNAL_SUFFIX);
844
+ if (
845
+ parentMeta.initialCommonJSType === IS_WRAPPED_COMMONJS &&
846
+ !allowProxy &&
847
+ isExternalWrapped
848
+ ) {
849
+ const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX);
850
+ if (actualExternalId.startsWith('node:')) {
851
+ isCommonJS = IS_WRAPPED_COMMONJS;
852
+ parentMeta.isRequiredCommonJS[dependencyId] = isCommonJS;
853
+ }
854
+ }
828
855
  const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
829
856
  fullyAnalyzedModules[dependencyId] = true;
857
+ const moduleInfo =
858
+ isWrappedCommonJS && !isExternalWrapped
859
+ ? rollupContext.getModuleInfo(dependencyId)
860
+ : null;
861
+ // For wrapped dependencies, annotate the generated require call as pure only
862
+ // when Rollup has module info and it explicitly reports no side effects.
863
+ // Note: For external Node built-ins (handled via EXTERNAL_SUFFIX), the module
864
+ // has not been loaded yet at this point and getModuleInfo returns null.
865
+ // Default to side effects = true in that case to be safe.
866
+ // Preserve Rollup's tri-state semantics (true | false | 'no-treeshake') when available.
867
+ const wrappedModuleSideEffects = !isWrappedCommonJS
868
+ ? false
869
+ : moduleInfo?.moduleSideEffects ?? true;
830
870
  return {
831
- wrappedModuleSideEffects:
832
- isWrappedCommonJS && rollupContext.getModuleInfo(dependencyId).moduleSideEffects,
871
+ wrappedModuleSideEffects,
833
872
  source: sources[index].source,
834
873
  id: allowProxy
835
874
  ? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
@@ -1814,7 +1853,7 @@ async function transformCommonjs(
1814
1853
  }
1815
1854
  return;
1816
1855
  case 'ThisExpression':
1817
- // rewrite top-level `this` as `commonjsHelpers.commonjsGlobal`
1856
+ // rewrite top-level `this` as `exportsName`
1818
1857
  if (lexicalDepth === 0 && !classBodyDepth) {
1819
1858
  uses.global = true;
1820
1859
  if (!ignoreGlobal) {
@@ -2267,6 +2306,9 @@ function commonjs(options = {}) {
2267
2306
 
2268
2307
  if (isWrappedId(id, EXTERNAL_SUFFIX)) {
2269
2308
  const actualId = unwrapId(id, EXTERNAL_SUFFIX);
2309
+ if (actualId.startsWith('node:')) {
2310
+ return getExternalBuiltinRequireProxy(actualId);
2311
+ }
2270
2312
  return getUnknownRequireProxy(
2271
2313
  actualId,
2272
2314
  isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true
package/dist/es/index.js CHANGED
@@ -7,7 +7,7 @@ import { walk } from 'estree-walker';
7
7
  import MagicString from 'magic-string';
8
8
  import isReference from 'is-reference';
9
9
 
10
- var version = "28.0.6";
10
+ var version = "28.0.8";
11
11
  var peerDependencies = {
12
12
  rollup: "^2.68.0||^3.0.0||^4.0.0"
13
13
  };
@@ -490,6 +490,19 @@ function getEsImportProxy(id, defaultIsModuleExports, moduleSideEffects) {
490
490
  };
491
491
  }
492
492
 
493
+ // For external Node built-ins required from wrapped CommonJS modules, we must not
494
+ // hoist an ESM import of the built-in (which would eagerly load it). Instead,
495
+ // expose a lazy `__require()` that resolves the built-in at runtime via
496
+ // `createRequire(import.meta.url)`.
497
+ function getExternalBuiltinRequireProxy(id) {
498
+ const stringifiedId = JSON.stringify(id);
499
+ return (
500
+ `import { createRequire } from 'node:module';\n` +
501
+ `const require = createRequire(import.meta.url);\n` +
502
+ `export function __require() { return require(${stringifiedId}); }`
503
+ );
504
+ }
505
+
493
506
  /* eslint-disable no-param-reassign, no-undefined */
494
507
 
495
508
 
@@ -819,13 +832,39 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
819
832
  fullyAnalyzedModules[parentId] = true;
820
833
  return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
821
834
  // eslint-disable-next-line no-multi-assign
822
- const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
835
+ let isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
823
836
  getTypeForFullyAnalyzedModule(dependencyId));
837
+ // Special-case external Node built-ins to be handled via a lazy __require
838
+ // helper instead of hoisted ESM imports when strict wrapping is used.
839
+ const isExternalWrapped = isWrappedId(dependencyId, EXTERNAL_SUFFIX);
840
+ if (
841
+ parentMeta.initialCommonJSType === IS_WRAPPED_COMMONJS &&
842
+ !allowProxy &&
843
+ isExternalWrapped
844
+ ) {
845
+ const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX);
846
+ if (actualExternalId.startsWith('node:')) {
847
+ isCommonJS = IS_WRAPPED_COMMONJS;
848
+ parentMeta.isRequiredCommonJS[dependencyId] = isCommonJS;
849
+ }
850
+ }
824
851
  const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
825
852
  fullyAnalyzedModules[dependencyId] = true;
853
+ const moduleInfo =
854
+ isWrappedCommonJS && !isExternalWrapped
855
+ ? rollupContext.getModuleInfo(dependencyId)
856
+ : null;
857
+ // For wrapped dependencies, annotate the generated require call as pure only
858
+ // when Rollup has module info and it explicitly reports no side effects.
859
+ // Note: For external Node built-ins (handled via EXTERNAL_SUFFIX), the module
860
+ // has not been loaded yet at this point and getModuleInfo returns null.
861
+ // Default to side effects = true in that case to be safe.
862
+ // Preserve Rollup's tri-state semantics (true | false | 'no-treeshake') when available.
863
+ const wrappedModuleSideEffects = !isWrappedCommonJS
864
+ ? false
865
+ : moduleInfo?.moduleSideEffects ?? true;
826
866
  return {
827
- wrappedModuleSideEffects:
828
- isWrappedCommonJS && rollupContext.getModuleInfo(dependencyId).moduleSideEffects,
867
+ wrappedModuleSideEffects,
829
868
  source: sources[index].source,
830
869
  id: allowProxy
831
870
  ? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
@@ -1810,7 +1849,7 @@ async function transformCommonjs(
1810
1849
  }
1811
1850
  return;
1812
1851
  case 'ThisExpression':
1813
- // rewrite top-level `this` as `commonjsHelpers.commonjsGlobal`
1852
+ // rewrite top-level `this` as `exportsName`
1814
1853
  if (lexicalDepth === 0 && !classBodyDepth) {
1815
1854
  uses.global = true;
1816
1855
  if (!ignoreGlobal) {
@@ -2263,6 +2302,9 @@ function commonjs(options = {}) {
2263
2302
 
2264
2303
  if (isWrappedId(id, EXTERNAL_SUFFIX)) {
2265
2304
  const actualId = unwrapId(id, EXTERNAL_SUFFIX);
2305
+ if (actualId.startsWith('node:')) {
2306
+ return getExternalBuiltinRequireProxy(actualId);
2307
+ }
2266
2308
  return getUnknownRequireProxy(
2267
2309
  actualId,
2268
2310
  isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rollup/plugin-commonjs",
3
- "version": "28.0.6",
3
+ "version": "28.0.8",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },