Skip to content

Commit

Permalink
feat(commonjs): expose plugin version (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 24, 2022
1 parent 335a013 commit 1330e6a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/commonjs/README.md
Expand Up @@ -51,9 +51,9 @@ When used together with the node-resolve plugin
Type: `"auto" | boolean | "debug" | string[]`<br>
Default: `"auto"`

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.
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.

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.
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.

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

Expand Down
4 changes: 3 additions & 1 deletion packages/commonjs/src/index.js
Expand Up @@ -2,7 +2,7 @@ import { extname, relative, resolve, dirname } from 'path';

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

import { peerDependencies } from '../package.json';
import { peerDependencies, version } from '../package.json';

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

version,

options(rawOptions) {
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
// do not prevent our plugin from resolving entry points ot proxies.
Expand Down
5 changes: 5 additions & 0 deletions packages/commonjs/test/test.js
Expand Up @@ -23,6 +23,11 @@ test('Rollup peer dependency has correct format', (t) => {
t.regex(peerDependencies.rollup, /^\^\d+\.\d+\.\d+(\|\|\^\d+\.\d+\.\d+)*$/);
});

test('exposes plugin version', (t) => {
const plugin = commonjs();
t.regex(plugin.version, /^\d+\.\d+\.\d+/);
});

// most of these should be moved over to function...
test('generates a sourcemap', async (t) => {
const bundle = await rollup({
Expand Down

0 comments on commit 1330e6a

Please sign in to comment.