From 1330e6ad7a514469a99a6807e6c86446417a318e Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 24 Nov 2021 06:35:45 +0100 Subject: [PATCH] feat(commonjs): expose plugin version (#1038) --- packages/commonjs/README.md | 4 ++-- packages/commonjs/src/index.js | 4 +++- packages/commonjs/test/test.js | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/commonjs/README.md b/packages/commonjs/README.md index a835f8e48..c126e4ac2 100644 --- a/packages/commonjs/README.md +++ b/packages/commonjs/README.md @@ -51,9 +51,9 @@ When used together with the node-resolve plugin Type: `"auto" | boolean | "debug" | string[]`
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"`. diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index 337f41830..1d0fdcc59 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -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'; @@ -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. diff --git a/packages/commonjs/test/test.js b/packages/commonjs/test/test.js index f528e1213..ea0afb695 100644 --- a/packages/commonjs/test/test.js +++ b/packages/commonjs/test/test.js @@ -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({