@rollup/plugin-commonjs 21.0.1 → 22.0.0-3
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 +34 -6
- package/dist/index.es.js +898 -795
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +896 -793
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/types/index.d.ts +52 -10
package/README.md
CHANGED
|
@@ -42,15 +42,34 @@ export default {
|
|
|
42
42
|
|
|
43
43
|
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
|
|
44
44
|
|
|
45
|
+
When used together with the node-resolve plugin
|
|
46
|
+
|
|
45
47
|
## Options
|
|
46
48
|
|
|
49
|
+
### `strictRequires`
|
|
50
|
+
|
|
51
|
+
Type: `"auto" | boolean | "debug" | string[]`<br>
|
|
52
|
+
Default: `"auto"`
|
|
53
|
+
|
|
54
|
+
By default, this plugin will try to hoist `require` statements as imports to the top of each file. While this works well for many code bases and allows for very efficient ESM output, it does not perfectly capture CommonJS semantics as the initialisation order of required modules will be different. The resultant side effects can include log statements being emitted in a different order, and some code that is dependent on the initialisation order of polyfills in require statements may not work. But it is especially problematic when there are circular `require` calls between CommonJS modules as those often rely on the lazy execution of nested `require` calls.
|
|
55
|
+
|
|
56
|
+
Setting this option to `true` will wrap all CommonJS files in functions which are executed when they are required for the first time, preserving NodeJS semantics. This is the safest setting and should be used if the generated code does not work correctly with `"auto"`. Note that `strictRequires: true` can have a small impact on the size and performance of generated code, but less so if the code is minified.
|
|
57
|
+
|
|
58
|
+
The default value of `"auto"` will only wrap CommonJS files when they are part of a CommonJS dependency cycle, e.g. an index file that is required by some of its dependencies, or if they are only required in a potentially "conditional" way like from within an if-statement or a function. All other CommonJS files are hoisted. This is the recommended setting for most code bases. Note that the detection of conditional requires can be subject to race conditions if there are both conditional and unconditional requires of the same file, which in edge cases may result in inconsistencies between builds. If you think this is a problem for you, you can avoid this by using any value other than `"auto"` or `"debug"`.
|
|
59
|
+
|
|
60
|
+
`false` will entirely prevent wrapping and hoist all files. This may still work depending on the nature of cyclic dependencies but will often cause problems.
|
|
61
|
+
|
|
62
|
+
You can also provide a [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, to only specify a subset of files which should be wrapped in functions for proper `require` semantics.
|
|
63
|
+
|
|
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 minimatch pattern for fine-tuning or to avoid the potential race conditions mentioned for `"auto"`.
|
|
65
|
+
|
|
47
66
|
### `dynamicRequireTargets`
|
|
48
67
|
|
|
49
68
|
Type: `string | string[]`<br>
|
|
50
69
|
Default: `[]`
|
|
51
70
|
|
|
52
71
|
Some modules contain dynamic `require` calls, or require modules that contain circular dependencies, which are not handled well by static imports.
|
|
53
|
-
Including those modules as `dynamicRequireTargets` will simulate a CommonJS (NodeJS-like) environment for them with support for dynamic
|
|
72
|
+
Including those modules as `dynamicRequireTargets` will simulate a CommonJS (NodeJS-like) environment for them with support for dynamic dependencies. It also enables `strictRequires` for those modules, see above.
|
|
54
73
|
|
|
55
74
|
_Note: In extreme cases, this feature may result in some paths being rendered as absolute in the final bundle. The plugin tries to avoid exposing paths from the local machine, but if you are `dynamicRequirePaths` with paths that are far away from your project's folder, that may require replacing strings like `"/Users/John/Desktop/foo-project/"` -> `"/"`._
|
|
56
75
|
|
|
@@ -71,6 +90,13 @@ commonjs({
|
|
|
71
90
|
});
|
|
72
91
|
```
|
|
73
92
|
|
|
93
|
+
### `dynamicRequireRoot`
|
|
94
|
+
|
|
95
|
+
Type: `string`<br>
|
|
96
|
+
Default: `process.cwd()`
|
|
97
|
+
|
|
98
|
+
To avoid long paths when using the `dynamicRequireTargets` option, you can use this option to specify a directory that is a common parent for all files that use dynamic require statements. Using a directory higher up such as `/` may lead to unnecessarily long paths in the generated code and may expose directory names on your machine like your home directory name. By default it uses the current working directory.
|
|
99
|
+
|
|
74
100
|
### `exclude`
|
|
75
101
|
|
|
76
102
|
Type: `string | string[]`<br>
|
|
@@ -125,15 +151,17 @@ Sometimes you have to leave require statements unconverted. Pass an array contai
|
|
|
125
151
|
Type: `boolean | 'remove' | string[] | ((id: string) => boolean)`<br>
|
|
126
152
|
Default: `true`
|
|
127
153
|
|
|
128
|
-
In most cases, where `require` calls are inside a `try-catch` clause, they should be left unconverted as it requires an optional dependency that may or may not be installed beside the rolled up package.
|
|
154
|
+
In most cases, where `require` calls to external dependencies are inside a `try-catch` clause, they should be left unconverted as it requires an optional dependency that may or may not be installed beside the rolled up package.
|
|
129
155
|
Due to the conversion of `require` to a static `import` - the call is hoisted to the top of the file, outside of the `try-catch` clause.
|
|
130
156
|
|
|
131
|
-
- `true`: All `require` calls inside a `try` will be left unconverted.
|
|
132
|
-
- `false`: All `require` calls inside a `try` will be converted as if the `try-catch` clause is not there.
|
|
133
|
-
- `remove`: Remove all `require` calls from inside any `try` block.
|
|
157
|
+
- `true`: All external `require` calls inside a `try` will be left unconverted.
|
|
158
|
+
- `false`: All external `require` calls inside a `try` will be converted as if the `try-catch` clause is not there.
|
|
159
|
+
- `remove`: Remove all external `require` calls from inside any `try` block.
|
|
134
160
|
- `string[]`: Pass an array containing the IDs to left unconverted.
|
|
135
161
|
- `((id: string) => boolean|'remove')`: Pass a function that control individual IDs.
|
|
136
162
|
|
|
163
|
+
Note that non-external requires will not be ignored by this option.
|
|
164
|
+
|
|
137
165
|
### `ignoreDynamicRequires`
|
|
138
166
|
|
|
139
167
|
Type: `boolean`
|
|
@@ -361,7 +389,7 @@ export default {
|
|
|
361
389
|
format: 'iife',
|
|
362
390
|
name: 'MyModule'
|
|
363
391
|
},
|
|
364
|
-
plugins: [
|
|
392
|
+
plugins: [commonjs(), resolve()]
|
|
365
393
|
};
|
|
366
394
|
```
|
|
367
395
|
|