@rollup/plugin-commonjs 28.0.9 → 29.0.1
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 +28 -0
- package/dist/cjs/index.js +38 -19
- package/dist/es/index.js +38 -19
- package/package.json +1 -1
- package/types/index.d.ts +11 -0
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 = "
|
|
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(
|
|
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,21 +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
850
|
let resolvedDependencyId = dependencyId;
|
|
845
|
-
if (
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
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
|
+
}
|
|
858
866
|
}
|
|
859
867
|
}
|
|
860
868
|
const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
|
|
@@ -1033,6 +1041,10 @@ function isShorthandProperty(parent) {
|
|
|
1033
1041
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
1034
1042
|
}
|
|
1035
1043
|
|
|
1044
|
+
function isPropertyDefinitionKey(parent, node) {
|
|
1045
|
+
return parent && parent.type === 'PropertyDefinition' && parent.key === node;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1036
1048
|
function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
|
|
1037
1049
|
const args = [];
|
|
1038
1050
|
const passedArgs = [];
|
|
@@ -1787,7 +1799,7 @@ async function transformCommonjs(
|
|
|
1787
1799
|
if (
|
|
1788
1800
|
!isReference(node, parent) ||
|
|
1789
1801
|
scope.contains(name) ||
|
|
1790
|
-
(parent
|
|
1802
|
+
isPropertyDefinitionKey(parent, node)
|
|
1791
1803
|
)
|
|
1792
1804
|
return;
|
|
1793
1805
|
switch (name) {
|
|
@@ -1814,6 +1826,10 @@ async function transformCommonjs(
|
|
|
1814
1826
|
case 'global':
|
|
1815
1827
|
uses.global = true;
|
|
1816
1828
|
if (!ignoreGlobal) {
|
|
1829
|
+
if (isShorthandProperty(parent)) {
|
|
1830
|
+
skippedNodes.add(parent.value);
|
|
1831
|
+
magicString.prependRight(node.start, 'global: ');
|
|
1832
|
+
}
|
|
1817
1833
|
replacedGlobal.push(node);
|
|
1818
1834
|
}
|
|
1819
1835
|
return;
|
|
@@ -1934,6 +1950,7 @@ async function transformCommonjs(
|
|
|
1934
1950
|
|
|
1935
1951
|
for (const node of replacedGlobal) {
|
|
1936
1952
|
magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
|
|
1953
|
+
contentOnly: true,
|
|
1937
1954
|
storeName: true
|
|
1938
1955
|
});
|
|
1939
1956
|
}
|
|
@@ -2090,7 +2107,8 @@ function commonjs(options = {}) {
|
|
|
2090
2107
|
ignoreDynamicRequires,
|
|
2091
2108
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
2092
2109
|
defaultIsModuleExports: defaultIsModuleExportsOption,
|
|
2093
|
-
esmExternals
|
|
2110
|
+
esmExternals,
|
|
2111
|
+
requireNodeBuiltins = false
|
|
2094
2112
|
} = options;
|
|
2095
2113
|
const extensions = options.extensions || ['.js'];
|
|
2096
2114
|
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
@@ -2263,7 +2281,8 @@ function commonjs(options = {}) {
|
|
|
2263
2281
|
requireResolver = getRequireResolver(
|
|
2264
2282
|
extensions,
|
|
2265
2283
|
detectCyclesAndConditional,
|
|
2266
|
-
currentlyResolving
|
|
2284
|
+
currentlyResolving,
|
|
2285
|
+
requireNodeBuiltins
|
|
2267
2286
|
);
|
|
2268
2287
|
},
|
|
2269
2288
|
|
|
@@ -2311,7 +2330,7 @@ function commonjs(options = {}) {
|
|
|
2311
2330
|
|
|
2312
2331
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
2313
2332
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
2314
|
-
if (actualId.startsWith('node:')) {
|
|
2333
|
+
if (requireNodeBuiltins === true && actualId.startsWith('node:')) {
|
|
2315
2334
|
return getExternalBuiltinRequireProxy(actualId);
|
|
2316
2335
|
}
|
|
2317
2336
|
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 = "
|
|
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(
|
|
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,21 +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
846
|
let resolvedDependencyId = dependencyId;
|
|
841
|
-
if (
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
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
|
+
}
|
|
854
862
|
}
|
|
855
863
|
}
|
|
856
864
|
const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS;
|
|
@@ -1029,6 +1037,10 @@ function isShorthandProperty(parent) {
|
|
|
1029
1037
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
1030
1038
|
}
|
|
1031
1039
|
|
|
1040
|
+
function isPropertyDefinitionKey(parent, node) {
|
|
1041
|
+
return parent && parent.type === 'PropertyDefinition' && parent.key === node;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1032
1044
|
function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
|
|
1033
1045
|
const args = [];
|
|
1034
1046
|
const passedArgs = [];
|
|
@@ -1783,7 +1795,7 @@ async function transformCommonjs(
|
|
|
1783
1795
|
if (
|
|
1784
1796
|
!isReference(node, parent) ||
|
|
1785
1797
|
scope.contains(name) ||
|
|
1786
|
-
(parent
|
|
1798
|
+
isPropertyDefinitionKey(parent, node)
|
|
1787
1799
|
)
|
|
1788
1800
|
return;
|
|
1789
1801
|
switch (name) {
|
|
@@ -1810,6 +1822,10 @@ async function transformCommonjs(
|
|
|
1810
1822
|
case 'global':
|
|
1811
1823
|
uses.global = true;
|
|
1812
1824
|
if (!ignoreGlobal) {
|
|
1825
|
+
if (isShorthandProperty(parent)) {
|
|
1826
|
+
skippedNodes.add(parent.value);
|
|
1827
|
+
magicString.prependRight(node.start, 'global: ');
|
|
1828
|
+
}
|
|
1813
1829
|
replacedGlobal.push(node);
|
|
1814
1830
|
}
|
|
1815
1831
|
return;
|
|
@@ -1930,6 +1946,7 @@ async function transformCommonjs(
|
|
|
1930
1946
|
|
|
1931
1947
|
for (const node of replacedGlobal) {
|
|
1932
1948
|
magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
|
|
1949
|
+
contentOnly: true,
|
|
1933
1950
|
storeName: true
|
|
1934
1951
|
});
|
|
1935
1952
|
}
|
|
@@ -2086,7 +2103,8 @@ function commonjs(options = {}) {
|
|
|
2086
2103
|
ignoreDynamicRequires,
|
|
2087
2104
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
2088
2105
|
defaultIsModuleExports: defaultIsModuleExportsOption,
|
|
2089
|
-
esmExternals
|
|
2106
|
+
esmExternals,
|
|
2107
|
+
requireNodeBuiltins = false
|
|
2090
2108
|
} = options;
|
|
2091
2109
|
const extensions = options.extensions || ['.js'];
|
|
2092
2110
|
const filter = createFilter(options.include, options.exclude);
|
|
@@ -2259,7 +2277,8 @@ function commonjs(options = {}) {
|
|
|
2259
2277
|
requireResolver = getRequireResolver(
|
|
2260
2278
|
extensions,
|
|
2261
2279
|
detectCyclesAndConditional,
|
|
2262
|
-
currentlyResolving
|
|
2280
|
+
currentlyResolving,
|
|
2281
|
+
requireNodeBuiltins
|
|
2263
2282
|
);
|
|
2264
2283
|
},
|
|
2265
2284
|
|
|
@@ -2307,7 +2326,7 @@ function commonjs(options = {}) {
|
|
|
2307
2326
|
|
|
2308
2327
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
2309
2328
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
2310
|
-
if (actualId.startsWith('node:')) {
|
|
2329
|
+
if (requireNodeBuiltins === true && actualId.startsWith('node:')) {
|
|
2311
2330
|
return getExternalBuiltinRequireProxy(actualId);
|
|
2312
2331
|
}
|
|
2313
2332
|
return getUnknownRequireProxy(
|
package/package.json
CHANGED
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
|
/**
|