Skip to content

Commit 1330e6a

Browse files
committed
feat(commonjs): expose plugin version (#1038)
1 parent 335a013 commit 1330e6a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

packages/commonjs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ When used together with the node-resolve plugin
5151
Type: `"auto" | boolean | "debug" | string[]`<br>
5252
Default: `"auto"`
5353

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 order of side effects like log statements may change. 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.
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.
5555

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. Note that this can have an impact on the size and performance of the generated code.
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.
5757

5858
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"`.
5959

packages/commonjs/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { extname, relative, resolve, dirname } from 'path';
22

33
import { createFilter } from '@rollup/pluginutils';
44

5-
import { peerDependencies } from '../package.json';
5+
import { peerDependencies, version } from '../package.json';
66

77
import analyzeTopLevelStatements from './analyze-top-level-statements';
88
import { getDynamicModuleRegistry, getDynamicRequireModules } from './dynamic-modules';
@@ -166,6 +166,8 @@ export default function commonjs(options = {}) {
166166
return {
167167
name: 'commonjs',
168168

169+
version,
170+
169171
options(rawOptions) {
170172
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
171173
// do not prevent our plugin from resolving entry points ot proxies.

packages/commonjs/test/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ test('Rollup peer dependency has correct format', (t) => {
2323
t.regex(peerDependencies.rollup, /^\^\d+\.\d+\.\d+(\|\|\^\d+\.\d+\.\d+)*$/);
2424
});
2525

26+
test('exposes plugin version', (t) => {
27+
const plugin = commonjs();
28+
t.regex(plugin.version, /^\d+\.\d+\.\d+/);
29+
});
30+
2631
// most of these should be moved over to function...
2732
test('generates a sourcemap', async (t) => {
2833
const bundle = await rollup({

0 commit comments

Comments
 (0)