@rollup/plugin-commonjs 28.0.8 → 29.0.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/README.md CHANGED
@@ -63,6 +63,34 @@ You can also provide a [picomatch pattern](https://github.com/micromatch/picomat
63
63
 
64
64
  `"debug"` works like `"auto"` but after bundling, it will display a warning containing a list of ids that have been wrapped which can be used as picomatch pattern for fine-tuning or to avoid the potential race conditions mentioned for `"auto"`.
65
65
 
66
+ ### `requireNodeBuiltins`
67
+
68
+ Type: `boolean`<br>
69
+ Default: `false`
70
+
71
+ When enabled, external Node built-ins (e.g., `node:fs`, `node:path`) required from wrapped CommonJS modules will use `createRequire(import.meta.url)` instead of being hoisted as ESM imports. This prevents eager loading of Node built-ins at module initialization time and preserves the lazy execution semantics of `require()`.
72
+
73
+ **Important:** Enabling this option adds a dependency on `node:module` in the output bundle, which may not be available in some environments like edge runtimes (Cloudflare Workers, Vercel Edge Runtime). Only enable this option if you are targeting Node.js environments and need the lazy loading behavior for Node built-ins.
74
+
75
+ Example:
76
+
77
+ ```js
78
+ commonjs({
79
+ strictRequires: true,
80
+ requireNodeBuiltins: true
81
+ });
82
+ ```
83
+
84
+ With `requireNodeBuiltins: true`, code like:
85
+
86
+ ```js
87
+ if (condition) {
88
+ require('node:fs');
89
+ }
90
+ ```
91
+
92
+ will generate output using `createRequire` instead of hoisting the import to the top of the file.
93
+
66
94
  ### `dynamicRequireTargets`
67
95
 
68
96
  Type: `string | string[]`<br>
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.8";
14
+ var version = "29.0.0";
15
15
  var peerDependencies = {
16
16
  rollup: "^2.68.0||^3.0.0||^4.0.0"
17
17
  };
@@ -655,7 +655,12 @@ function getResolveId(extensions, isPossibleCjsId) {
655
655
  };
656
656
  }
657
657
 
658
- function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
658
+ function getRequireResolver(
659
+ extensions,
660
+ detectCyclesAndConditional,
661
+ currentlyResolving,
662
+ requireNodeBuiltins
663
+ ) {
659
664
  const knownCjsModuleTypes = Object.create(null);
660
665
  const requiredIds = Object.create(null);
661
666
  const unconditionallyRequiredIds = Object.create(null);
@@ -840,16 +845,24 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
840
845
  getTypeForFullyAnalyzedModule(dependencyId));
841
846
  // Special-case external Node built-ins to be handled via a lazy __require
842
847
  // helper instead of hoisted ESM imports when strict wrapping is used.
848
+ // Only apply this when requireNodeBuiltins option is enabled.
843
849
  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;
850
+ let resolvedDependencyId = dependencyId;
851
+ if (requireNodeBuiltins === true) {
852
+ if (parentMeta.isCommonJS === IS_WRAPPED_COMMONJS && !allowProxy && isExternalWrapped) {
853
+ const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX);
854
+ if (actualExternalId.startsWith('node:')) {
855
+ isCommonJS = IS_WRAPPED_COMMONJS;
856
+ parentMeta.isRequiredCommonJS[dependencyId] = isCommonJS;
857
+ }
858
+ } else if (isExternalWrapped && !allowProxy) {
859
+ // If the parent is not wrapped but the dependency is a node: builtin external,
860
+ // unwrap the EXTERNAL_SUFFIX so it's treated as a normal external.
861
+ // This avoids trying to load the lazy __require proxy for non-wrapped contexts.
862
+ const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX);
863
+ if (actualExternalId.startsWith('node:')) {
864
+ resolvedDependencyId = actualExternalId;
865
+ }
853
866
  }
854
867
  }
855
868
  const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
@@ -871,8 +884,8 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
871
884
  wrappedModuleSideEffects,
872
885
  source: sources[index].source,
873
886
  id: allowProxy
874
- ? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
875
- : dependencyId,
887
+ ? wrapId(resolvedDependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
888
+ : resolvedDependencyId,
876
889
  isCommonJS
877
890
  };
878
891
  });
@@ -2085,7 +2098,8 @@ function commonjs(options = {}) {
2085
2098
  ignoreDynamicRequires,
2086
2099
  requireReturnsDefault: requireReturnsDefaultOption,
2087
2100
  defaultIsModuleExports: defaultIsModuleExportsOption,
2088
- esmExternals
2101
+ esmExternals,
2102
+ requireNodeBuiltins = false
2089
2103
  } = options;
2090
2104
  const extensions = options.extensions || ['.js'];
2091
2105
  const filter = pluginutils.createFilter(options.include, options.exclude);
@@ -2258,7 +2272,8 @@ function commonjs(options = {}) {
2258
2272
  requireResolver = getRequireResolver(
2259
2273
  extensions,
2260
2274
  detectCyclesAndConditional,
2261
- currentlyResolving
2275
+ currentlyResolving,
2276
+ requireNodeBuiltins
2262
2277
  );
2263
2278
  },
2264
2279
 
@@ -2306,7 +2321,7 @@ function commonjs(options = {}) {
2306
2321
 
2307
2322
  if (isWrappedId(id, EXTERNAL_SUFFIX)) {
2308
2323
  const actualId = unwrapId(id, EXTERNAL_SUFFIX);
2309
- if (actualId.startsWith('node:')) {
2324
+ if (requireNodeBuiltins === true && actualId.startsWith('node:')) {
2310
2325
  return getExternalBuiltinRequireProxy(actualId);
2311
2326
  }
2312
2327
  return getUnknownRequireProxy(
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.8";
10
+ var version = "29.0.0";
11
11
  var peerDependencies = {
12
12
  rollup: "^2.68.0||^3.0.0||^4.0.0"
13
13
  };
@@ -651,7 +651,12 @@ function getResolveId(extensions, isPossibleCjsId) {
651
651
  };
652
652
  }
653
653
 
654
- function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
654
+ function getRequireResolver(
655
+ extensions,
656
+ detectCyclesAndConditional,
657
+ currentlyResolving,
658
+ requireNodeBuiltins
659
+ ) {
655
660
  const knownCjsModuleTypes = Object.create(null);
656
661
  const requiredIds = Object.create(null);
657
662
  const unconditionallyRequiredIds = Object.create(null);
@@ -836,16 +841,24 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
836
841
  getTypeForFullyAnalyzedModule(dependencyId));
837
842
  // Special-case external Node built-ins to be handled via a lazy __require
838
843
  // helper instead of hoisted ESM imports when strict wrapping is used.
844
+ // Only apply this when requireNodeBuiltins option is enabled.
839
845
  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;
846
+ let resolvedDependencyId = dependencyId;
847
+ if (requireNodeBuiltins === true) {
848
+ if (parentMeta.isCommonJS === IS_WRAPPED_COMMONJS && !allowProxy && isExternalWrapped) {
849
+ const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX);
850
+ if (actualExternalId.startsWith('node:')) {
851
+ isCommonJS = IS_WRAPPED_COMMONJS;
852
+ parentMeta.isRequiredCommonJS[dependencyId] = isCommonJS;
853
+ }
854
+ } else if (isExternalWrapped && !allowProxy) {
855
+ // If the parent is not wrapped but the dependency is a node: builtin external,
856
+ // unwrap the EXTERNAL_SUFFIX so it's treated as a normal external.
857
+ // This avoids trying to load the lazy __require proxy for non-wrapped contexts.
858
+ const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX);
859
+ if (actualExternalId.startsWith('node:')) {
860
+ resolvedDependencyId = actualExternalId;
861
+ }
849
862
  }
850
863
  }
851
864
  const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
@@ -867,8 +880,8 @@ function getRequireResolver(extensions, detectCyclesAndConditional, currentlyRes
867
880
  wrappedModuleSideEffects,
868
881
  source: sources[index].source,
869
882
  id: allowProxy
870
- ? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
871
- : dependencyId,
883
+ ? wrapId(resolvedDependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX)
884
+ : resolvedDependencyId,
872
885
  isCommonJS
873
886
  };
874
887
  });
@@ -2081,7 +2094,8 @@ function commonjs(options = {}) {
2081
2094
  ignoreDynamicRequires,
2082
2095
  requireReturnsDefault: requireReturnsDefaultOption,
2083
2096
  defaultIsModuleExports: defaultIsModuleExportsOption,
2084
- esmExternals
2097
+ esmExternals,
2098
+ requireNodeBuiltins = false
2085
2099
  } = options;
2086
2100
  const extensions = options.extensions || ['.js'];
2087
2101
  const filter = createFilter(options.include, options.exclude);
@@ -2254,7 +2268,8 @@ function commonjs(options = {}) {
2254
2268
  requireResolver = getRequireResolver(
2255
2269
  extensions,
2256
2270
  detectCyclesAndConditional,
2257
- currentlyResolving
2271
+ currentlyResolving,
2272
+ requireNodeBuiltins
2258
2273
  );
2259
2274
  },
2260
2275
 
@@ -2302,7 +2317,7 @@ function commonjs(options = {}) {
2302
2317
 
2303
2318
  if (isWrappedId(id, EXTERNAL_SUFFIX)) {
2304
2319
  const actualId = unwrapId(id, EXTERNAL_SUFFIX);
2305
- if (actualId.startsWith('node:')) {
2320
+ if (requireNodeBuiltins === true && actualId.startsWith('node:')) {
2306
2321
  return getExternalBuiltinRequireProxy(actualId);
2307
2322
  }
2308
2323
  return getUnknownRequireProxy(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rollup/plugin-commonjs",
3
- "version": "28.0.8",
3
+ "version": "29.0.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/types/index.d.ts CHANGED
@@ -225,6 +225,17 @@ interface RollupCommonJSOptions {
225
225
  * home directory name. By default, it uses the current working directory.
226
226
  */
227
227
  dynamicRequireRoot?: string;
228
+ /**
229
+ * When enabled, external Node built-ins (e.g., `node:fs`) required from wrapped CommonJS modules
230
+ * will use `createRequire(import.meta.url)` instead of being hoisted as ESM imports. This prevents
231
+ * eager loading of Node built-ins at module initialization time.
232
+ *
233
+ * Note: This option adds a dependency on `node:module` in the output, which may not be available
234
+ * in some environments like edge runtimes (Cloudflare Workers, Vercel Edge Runtime).
235
+ *
236
+ * @default false
237
+ */
238
+ requireNodeBuiltins?: boolean;
228
239
  }
229
240
 
230
241
  /**