@rollup/plugin-commonjs 15.0.0 → 17.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,58 @@
1
1
  # @rollup/plugin-commonjs ChangeLog
2
2
 
3
+ ## v17.1.0
4
+
5
+ _2021-01-29_
6
+
7
+ ### Bugfixes
8
+
9
+ - fix: correctly replace shorthand `require` (#764)
10
+
11
+ ### Features
12
+
13
+ - feature: load dynamic commonjs modules from es `import` (#766)
14
+ - feature: support cache/resolve access inside dynamic modules (#728)
15
+ - feature: allow keeping `require` calls inside try-catch (#729)
16
+
17
+ ### Updates
18
+
19
+ - chore: fix lint error (#719)
20
+
21
+ ## v17.0.0
22
+
23
+ _2020-11-30_
24
+
25
+ ### Breaking Changes
26
+
27
+ - feat!: reconstruct real es module from \_\_esModule marker (#537)
28
+
29
+ ## v16.0.0
30
+
31
+ _2020-10-27_
32
+
33
+ ### Breaking Changes
34
+
35
+ - feat!: Expose cjs detection and support offline caching (#604)
36
+
37
+ ### Bugfixes
38
+
39
+ - fix: avoid wrapping `commonjsRegister` call in `createCommonjsModule(...)` (#602)
40
+ - fix: register dynamic modules when a different loader (i.e typescript) loads the entry file (#599)
41
+ - fix: fixed access to node_modules dynamic module with subfolder (i.e 'logform/json') (#601)
42
+
43
+ ### Features
44
+
45
+ - feat: pass type of import to node-resolve (#611)
46
+
47
+ ## v15.1.0
48
+
49
+ _2020-09-21_
50
+
51
+ ### Features
52
+
53
+ - feat: inject \_\_esModule marker into ES namespaces and add Object prototype (#552)
54
+ - feat: add requireReturnsDefault to types (#579)
55
+
3
56
  ## v15.0.0
4
57
 
5
58
  _2020-08-13_
package/README.md CHANGED
@@ -76,21 +76,21 @@ commonjs({
76
76
  Type: `string | string[]`<br>
77
77
  Default: `null`
78
78
 
79
- A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default non-CommonJS modules are ignored.
79
+ A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default, all files with extensions other than those in `extensions` or `".cjs"` are ignored, but you can exclude additional files. See also the `include` option.
80
80
 
81
81
  ### `include`
82
82
 
83
83
  Type: `string | string[]`<br>
84
84
  Default: `null`
85
85
 
86
- A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default CommonJS modules are targeted.
86
+ A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default, all files with extension `".cjs"` or those in `extensions` are included, but you can narrow this list by only including specific files. These files will be analyzed and transpiled if either the analysis does not find ES module specific statements or `transformMixedEsModules` is `true`.
87
87
 
88
88
  ### `extensions`
89
89
 
90
90
  Type: `string[]`<br>
91
91
  Default: `['.js']`
92
92
 
93
- Search for extensions other than .js in the order specified.
93
+ For extensionless imports, search for extensions other than .js in the order specified. Note that you need to make sure that non-JavaScript files are transpiled by another plugin first.
94
94
 
95
95
  ### `ignoreGlobal`
96
96
 
@@ -104,28 +104,42 @@ If true, uses of `global` won't be dealt with by this plugin.
104
104
  Type: `boolean`<br>
105
105
  Default: `true`
106
106
 
107
- If false, skips source map generation for CommonJS modules.
107
+ If false, skips source map generation for CommonJS modules. This will improve performance.
108
108
 
109
109
  ### `transformMixedEsModules`
110
110
 
111
111
  Type: `boolean`<br>
112
112
  Default: `false`
113
113
 
114
- Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation.
114
+ Instructs the plugin whether to enable mixed module transformations. This is useful in scenarios with modules that contain a mix of ES `import` statements and CommonJS `require` expressions. Set to `true` if `require` calls should be transformed to imports in mixed modules, or `false` if the `require` expressions should survive the transformation. The latter can be important if the code contains environment detection, or you are coding for an environment with special treatment for `require` calls such as [ElectronJS](https://www.electronjs.org/). See also the "ignore" option.
115
115
 
116
116
  ### `ignore`
117
117
 
118
118
  Type: `string[] | ((id: string) => boolean)`<br>
119
119
  Default: `[]`
120
120
 
121
- Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function. Only use this option if you know what you're doing!
121
+ Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function.
122
+
123
+ ### `ignoreTryCatch`
124
+
125
+ Type: `boolean | 'remove' | string[] | ((id: string) => boolean)`<br>
126
+ Default: `false`
127
+
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.
129
+ 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
+
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.
134
+ - `string[]`: Pass an array containing the IDs to left unconverted.
135
+ - `((id: string) => boolean|'remove')`: Pass a function that control individual IDs.
122
136
 
123
137
  ### `esmExternals`
124
138
 
125
- Type: `boolean | string[] || ((id: string) => boolean)`
139
+ Type: `boolean | string[] | ((id: string) => boolean)`
126
140
  Default: `false`
127
141
 
128
- Controls how imports from external dependencies are rendered. By default, all external dependencies are assumed to be CommonJS. This means they are rendered as default imports to be compatible with e.g. NodeJS where ES modules can only import a default export from a CommonJS dependency:
142
+ Controls how to render imports from external dependencies. By default, this plugin assumes that all external dependencies are CommonJS. This means they are rendered as default imports to be compatible with e.g. NodeJS where ES modules can only import a default export from a CommonJS dependency:
129
143
 
130
144
  ```js
131
145
  // input
@@ -137,16 +151,16 @@ import foo from 'foo';
137
151
 
138
152
  This is likely not desired for ES module dependencies: Here `require` should usually return the namespace to be compatible with how bundled modules are handled.
139
153
 
140
- If you set `esmExternals` to `true`, all external dependencies are assumed to be ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports.
154
+ If you set `esmExternals` to `true`, this plugins assumes that all external dependencies are ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports.
141
155
 
142
- You can also supply an array of ids that are to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module.
156
+ You can also supply an array of ids to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module.
143
157
 
144
158
  ### `requireReturnsDefault`
145
159
 
146
- Type: `boolean | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`<br>
160
+ Type: `boolean | "namespace" | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`<br>
147
161
  Default: `false`
148
162
 
149
- Controls what is returned when requiring an ES module or external dependency from a CommonJS file. By default, this plugin will render it as a namespace import, i.e.
163
+ Controls what is returned when requiring an ES module from a CommonJS file. When using the `esmExternals` option, this will also apply to external modules. By default, this plugin will render those imports as namespace imports, i.e.
150
164
 
151
165
  ```js
152
166
  // input
@@ -182,13 +196,44 @@ This is in line with how other bundlers handle this situation and is also the mo
182
196
 
183
197
  For these situations, you can change Rollup's behaviour either globally or per module. To change it globally, set the `requireReturnsDefault` option to one of the following values:
184
198
 
185
- - `false`: This is the default, requiring an ES module returns its namespace. For external dependencies when using `esmExternals: true`, no additional interop code is generated:
199
+ - `false`: This is the default, requiring an ES module returns its namespace. This is the only option that will also add a marker `__esModule: true` to the namespace to support interop patterns in CommonJS modules that are transpiled ES modules.
186
200
 
187
201
  ```js
188
202
  // input
189
203
  const dep = require('dep');
190
204
  console.log(dep);
191
205
 
206
+ // output
207
+ import * as dep$1 from 'dep';
208
+
209
+ function getAugmentedNamespace(n) {
210
+ var a = Object.defineProperty({}, '__esModule', { value: true });
211
+ Object.keys(n).forEach(function (k) {
212
+ var d = Object.getOwnPropertyDescriptor(n, k);
213
+ Object.defineProperty(
214
+ a,
215
+ k,
216
+ d.get
217
+ ? d
218
+ : {
219
+ enumerable: true,
220
+ get: function () {
221
+ return n[k];
222
+ }
223
+ }
224
+ );
225
+ });
226
+ return a;
227
+ }
228
+
229
+ var dep = /*@__PURE__*/ getAugmentedNamespace(dep$1);
230
+
231
+ console.log(dep);
232
+ ```
233
+
234
+ - `"namespace"`: Like `false`, requiring an ES module returns its namespace, but the plugin does not add the `__esModule` marker and thus creates more efficient code. For external dependencies when using `esmExternals: true`, no additional interop code is generated.
235
+
236
+ ```js
192
237
  // output
193
238
  import * as dep from 'dep';
194
239
 
@@ -202,11 +247,7 @@ For these situations, you can change Rollup's behaviour either globally or per m
202
247
  import * as dep$1 from 'dep';
203
248
 
204
249
  function getDefaultExportFromNamespaceIfNotNamed(n) {
205
- return n &&
206
- Object.prototype.hasOwnProperty.call(n, 'default') &&
207
- Object.keys(n).length === 1
208
- ? n['default']
209
- : n;
250
+ return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
210
251
  }
211
252
 
212
253
  var dep = getDefaultExportFromNamespaceIfNotNamed(dep$1);
@@ -221,9 +262,7 @@ For these situations, you can change Rollup's behaviour either globally or per m
221
262
  import * as dep$1 from 'dep';
222
263
 
223
264
  function getDefaultExportFromNamespaceIfPresent(n) {
224
- return n && Object.prototype.hasOwnProperty.call(n, 'default')
225
- ? n['default']
226
- : n;
265
+ return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
227
266
  }
228
267
 
229
268
  var dep = getDefaultExportFromNamespaceIfPresent(dep$1);
@@ -280,6 +319,26 @@ ES modules are _always_ parsed in strict mode. That means that certain non-stric
280
319
 
281
320
  Luckily, there is absolutely no good reason _not_ to use strict mode for everything — so the solution to this problem is to lobby the authors of those modules to update them.
282
321
 
322
+ ## Inter-plugin-communication
323
+
324
+ This plugin exposes the result of its CommonJS file type detection for other plugins to use. You can access it via `this.getModuleInfo` or the `moduleParsed` hook:
325
+
326
+ ```js
327
+ function cjsDetectionPlugin() {
328
+ return {
329
+ name: 'cjs-detection',
330
+ moduleParsed({
331
+ id,
332
+ meta: {
333
+ commonjs: { isCommonJS }
334
+ }
335
+ }) {
336
+ console.log(`File ${id} is CommonJS: ${isCommonJS}`);
337
+ }
338
+ };
339
+ }
340
+ ```
341
+
283
342
  ## Meta
284
343
 
285
344
  [CONTRIBUTING](/.github/CONTRIBUTING.md)