Skip to content

Commit

Permalink
fix(commonjs): Improve error message for unsupported dynamic requires
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 24, 2020
1 parent 800667b commit 627f801
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/commonjs/src/helpers.js
Expand Up @@ -54,8 +54,8 @@ export function createCommonjsModule(fn) {
return fn(module, module.exports), module.exports;
}
export function commonjsRequire () {
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
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.');
}
`;

Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'throws for unsupported dynamic require calls'
};
@@ -0,0 +1,8 @@
t.throws(() => require(getRequireTarget()), {
message:
'Could not dynamically require "foo-bar". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.'
});

function getRequireTarget() {
return 'foo-bar';
}
36 changes: 32 additions & 4 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -5477,8 +5477,8 @@ Generated by [AVA](https://avajs.dev).
return fn(module, module.exports), module.exports;␊
}␊
␊
function commonjsRequire () {␊
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');␊
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.');␊
}␊
␊
var foo = createCommonjsModule(function (module) {␊
Expand All @@ -5505,8 +5505,8 @@ Generated by [AVA](https://avajs.dev).
return fn(module, module.exports), module.exports;␊
}␊
␊
function commonjsRequire () {␊
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');␊
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.');␊
}␊
␊
var foo = createCommonjsModule(function (module) {␊
Expand Down Expand Up @@ -5543,3 +5543,31 @@ Generated by [AVA](https://avajs.dev).
module.exports = main;␊
`,
}

## unsupported-dynamic-require

> Snapshot 1
{
'main.js': `'use strict';␊
␊
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.');␊
}␊
␊
t.throws(() => commonjsRequire(getRequireTarget()), {␊
message:␊
'Could not dynamically require "foo-bar". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.'␊
});␊
␊
function getRequireTarget() {␊
return 'foo-bar';␊
}␊
␊
var main = {␊
␊
};␊
␊
module.exports = main;␊
`,
}
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 627f801

Please sign in to comment.