Skip to content

Commit

Permalink
Merge branch 'master' into node-resolve-external-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson committed Mar 14, 2021
2 parents 995d616 + 2d06c44 commit b2d5c6b
Show file tree
Hide file tree
Showing 86 changed files with 1,173 additions and 628 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Expand Up @@ -70,6 +70,9 @@ jobs:
- run:
name: Run linting.
command: pnpm run ci:lint
- run:
name: Check TypeScript types.
command: pnpm run test:ts
- save_cache:
key: dependency-cache-{{ checksum "pnpm-lock.yaml" }}
paths:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -15,7 +15,8 @@
"pub": "node scripts/pub.js",
"publish": "node scripts/publish.js",
"security": "pnpm audit --audit-level=moderate",
"test": "node scripts/run-changed.js test"
"test": "node scripts/run-changed.js test",
"test:ts": "node scripts/run-changed.js test:ts"
},
"dependencies": {
"conventional-commits-parser": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/alias/package.json
Expand Up @@ -29,7 +29,8 @@
"prepare": "pnpm run build",
"prepublishOnly": "pnpm run lint && pnpm run test",
"pretest": "pnpm run build",
"test": "ava"
"test": "ava",
"test:ts": "tsc --noEmit"
},
"files": [
"dist",
Expand Down
3 changes: 2 additions & 1 deletion packages/auto-install/package.json
Expand Up @@ -26,7 +26,8 @@
"prepare": "pnpm run build",
"prepublishOnly": "pnpm run lint",
"pretest": "pnpm run build",
"test": "ava"
"test": "ava",
"test:ts": "tsc --noEmit"
},
"files": [
"dist",
Expand Down
14 changes: 14 additions & 0 deletions packages/babel/CHANGELOG.md
@@ -1,5 +1,19 @@
# @rollup/plugin-babel ChangeLog

## v5.3.0

_2021-02-14_

### Features

- feat: add custom filter option (#767)
- feat: pass rollup context as this context into override config function (#784)

### Updates

- docs: typo in README.md (#800)
- chore: commit updated readme format (bfda6d8)

## v5.2.3

_2021-01-29_
Expand Down
2 changes: 1 addition & 1 deletion packages/babel/README.md
Expand Up @@ -84,7 +84,7 @@ All options are as per the [Babel documentation](https://babeljs.io/docs/en/opti

Type: `String | RegExp | Array[...String|RegExp]`<br>

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. When relaying on Babel configuration files you can only exclude additional files with this option, you cannot override what you have configured for Babel itself.
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. When relying on Babel configuration files you can only exclude additional files with this option, you cannot override what you have configured for Babel itself.

### `include`

Expand Down
2 changes: 1 addition & 1 deletion packages/babel/package.json
@@ -1,6 +1,6 @@
{
"name": "@rollup/plugin-babel",
"version": "5.2.3",
"version": "5.3.0",
"publishConfig": {
"access": "public"
},
Expand Down
19 changes: 19 additions & 0 deletions packages/commonjs/README.md
Expand Up @@ -134,6 +134,25 @@ Due to the conversion of `require` to a static `import` - the call is hoisted to
- `string[]`: Pass an array containing the IDs to left unconverted.
- `((id: string) => boolean|'remove')`: Pass a function that control individual IDs.

### `ignoreDynamicRequires`

Type: `boolean`
Default: false

Some `require` calls cannot be resolved statically to be translated to imports, e.g.

```js
function wrappedRequire(target) {
return require(target);
}
wrappedRequire('foo');
wrappedRequire('bar');
```

When this option is set to `false`, the generated code will either directly throw an error when such a call is encountered or, when `dynamicRequireTargets` is used, when such a call cannot be resolved with a configured dynamic require target.

Setting this option to `true` will instead leave the `require` call in the code or use it as a fallback for `dynamicRequireTargets`.

### `esmExternals`

Type: `boolean | string[] | ((id: string) => boolean)`
Expand Down
16 changes: 10 additions & 6 deletions packages/commonjs/src/helpers.js
Expand Up @@ -48,18 +48,20 @@ export function getAugmentedNamespace(n) {
}
`;

const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;

const HELPER_NON_DYNAMIC = `
export function createCommonjsModule(fn) {
var module = { exports: {} }
return fn(module, module.exports), module.exports;
}
export function commonjsRequire (target) {
throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.');
export function commonjsRequire (path) {
${FAILED_REQUIRE_ERROR}
}
`;

const HELPERS_DYNAMIC = `
const getDynamicHelpers = (ignoreDynamicRequires) => `
export function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
Expand Down Expand Up @@ -231,13 +233,15 @@ export function commonjsRequire (path, originalModuleDir) {
return cachedModule.exports;
};
}
return require(path);
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
}
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
commonjsRequire.resolve = commonjsResolve;
`;

export function getHelpersModule(isDynamicRequireModulesEnabled) {
return `${HELPERS}${isDynamicRequireModulesEnabled ? HELPERS_DYNAMIC : HELPER_NON_DYNAMIC}`;
export function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires) {
return `${HELPERS}${
isDynamicRequireModulesEnabled ? getDynamicHelpers(ignoreDynamicRequires) : HELPER_NON_DYNAMIC
}`;
}
7 changes: 6 additions & 1 deletion packages/commonjs/src/index.js
Expand Up @@ -44,6 +44,7 @@ export default function commonjs(options = {}) {
const filter = createFilter(options.include, options.exclude);
const {
ignoreGlobal,
ignoreDynamicRequires,
requireReturnsDefault: requireReturnsDefaultOption,
esmExternals
} = options;
Expand Down Expand Up @@ -97,6 +98,7 @@ export default function commonjs(options = {}) {

function transformAndCheckExports(code, id) {
if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) {
// eslint-disable-next-line no-param-reassign
code =
getDynamicPackagesEntryIntro(dynamicRequireModuleDirPaths, dynamicRequireModuleSet) + code;
}
Expand Down Expand Up @@ -125,6 +127,7 @@ export default function commonjs(options = {}) {
// avoid wrapping in createCommonjsModule, as this is a commonjsRegister call
if (isModuleRegisterProxy(id)) {
disableWrap = true;
// eslint-disable-next-line no-param-reassign
id = unwrapModuleRegisterProxy(id);
}

Expand All @@ -135,6 +138,7 @@ export default function commonjs(options = {}) {
isEsModule,
ignoreGlobal || isEsModule,
ignoreRequire,
ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
getIgnoreTryCatchRequireStatementMode,
sourceMap,
isDynamicRequireModulesEnabled,
Expand All @@ -161,7 +165,7 @@ export default function commonjs(options = {}) {

load(id) {
if (id === HELPERS_ID) {
return getHelpersModule(isDynamicRequireModulesEnabled);
return getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires);
}

if (id.startsWith(HELPERS_ID)) {
Expand Down Expand Up @@ -232,6 +236,7 @@ export default function commonjs(options = {}) {
}
},

// eslint-disable-next-line no-shadow
moduleParsed({ id, meta: { commonjs } }) {
if (commonjs) {
const isCjs = commonjs.isCommonJS;
Expand Down
18 changes: 11 additions & 7 deletions packages/commonjs/src/transform-commonjs.js
Expand Up @@ -46,6 +46,7 @@ export default function transformCommonjs(
isEsModule,
ignoreGlobal,
ignoreRequire,
ignoreDynamicRequires,
getIgnoreTryCatchRequireStatementMode,
sourceMap,
isDynamicRequireModulesEnabled,
Expand Down Expand Up @@ -320,12 +321,14 @@ export default function transformCommonjs(
)}`
);
}
if (isShorthandProperty(parent)) {
magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
} else {
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
storeName: true
});
if (!ignoreDynamicRequires) {
if (isShorthandProperty(parent)) {
magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
} else {
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
storeName: true
});
}
}

uses.commonjsHelpers = true;
Expand Down Expand Up @@ -437,7 +440,8 @@ export default function transformCommonjs(
uses.exports ||
uses.require ||
uses.commonjsHelpers ||
hasRemovedRequire
hasRemovedRequire ||
isRestorableCompiledEsm
) &&
(ignoreGlobal || !uses.global)
) {
Expand Down
@@ -0,0 +1 @@
Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
var input = /*#__PURE__*/Object.defineProperty({

}, '__esModule', {value: true});

export default input;
export { input as __moduleExports };

This file was deleted.

This file was deleted.

This file was deleted.

@@ -0,0 +1,6 @@
module.exports = {
description: 'keeps a require call as fallback when configured',
pluginOptions: {
ignoreDynamicRequires: true
}
};
@@ -0,0 +1 @@
module.exports = 'dep';
@@ -0,0 +1,8 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require(withName);
}

// The bundled code will run from test/helpers/util.js
t.is(takeModule('../fixtures/function/dynamic-require-fallback/dep.js'), 'dep');
Expand Up @@ -7,12 +7,7 @@ function takeModule(withName) {
t.is(takeModule('submodule1.js'), 'submodule1');
t.is(takeModule('submodule2.js'), 'submodule2');
t.is(takeModule('extramodule1.js'), 'extramodule1');

let hasThrown = false;
try {
takeModule('extramodule2.js');
} catch (error) {
t.truthy(/Cannot find module '\.\/extramodule2\.js'/.test(error.message));
hasThrown = true;
}
t.truthy(hasThrown);
t.throws(() => takeModule('extramodule2.js'), {
message:
'Could not dynamically require "./extramodule2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
@@ -0,0 +1,3 @@
module.exports = {
description: 'throws when there is no fallback for a dynamic require call'
};
@@ -0,0 +1,10 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require(withName);
}

t.throws(() => takeModule('./dep.js'), {
message:
'Could not dynamically require "./dep.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
@@ -0,0 +1,7 @@
module.exports = {
description: 'keeps a require call as fallback when configured for dynamicRequireTargets',
pluginOptions: {
ignoreDynamicRequires: true,
dynamicRequireTargets: ['fixtures/function/dynamic-require-targets-fallback/dep1.js']
}
};
@@ -0,0 +1 @@
module.exports = 'dep';
@@ -0,0 +1 @@
module.exports = 'dep';
@@ -0,0 +1,9 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require(withName);
}

t.is(takeModule('./dep1.js'), 'dep');
// The bundled code will run from test/helpers/util.js
t.is(takeModule('../fixtures/function/dynamic-require-targets-fallback/dep2.js'), 'dep');
@@ -0,0 +1,7 @@
module.exports = {
description:
'throws when there is no require call configured as fallback for dynamicRequireTargets',
pluginOptions: {
dynamicRequireTargets: ['fixtures/function/dynamic-require-targets-no-fallback/dep1.js']
}
};
@@ -0,0 +1 @@
module.exports = 'dep';
@@ -0,0 +1,11 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require(withName);
}

t.is(takeModule('./dep1.js'), 'dep');
t.throws(() => takeModule('./dep2.js'), {
message:
'Could not dynamically require "./dep2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
@@ -1,4 +1,4 @@
exports.encodeURIComponent = function() {
exports.encodeURIComponent = function () {
return encodeURIComponent(this.str);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/commonjs/test/fixtures/function/inline/main.js
@@ -1,4 +1,4 @@
/* eslint-disable global-require */
module.exports = function() {
module.exports = function () {
return require('./multiply')(2, require('./foo'));
};
@@ -1,3 +1,3 @@
module.exports = function(a, b) {
module.exports = function (a, b) {
return a * b;
};
@@ -1,3 +1,3 @@
module.exports = function() {
module.exports = function () {
return true;
};

This file was deleted.

This file was deleted.

@@ -1,3 +1,3 @@
module.exports = function() {
module.exports = function () {
return 'Hello there';
};

0 comments on commit b2d5c6b

Please sign in to comment.