Skip to content

Commit

Permalink
Use default named export with plugins (#3529)
Browse files Browse the repository at this point in the history
* Use default named export with plugins

* Simplify named export detection logic to prevent untested case

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
NotWoods and lukastaegert committed May 7, 2020
1 parent a2b961d commit e5cf74c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cli/run/commandPlugins.ts
Expand Up @@ -33,7 +33,7 @@ function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) {
// -p "{transform(c,i){...}}"
plugin = new Function('return ' + pluginText);
} else {
const match = pluginText.match(/^([@.\/\\\w|^{}|-]+)(=(.*))?$/);
const match = pluginText.match(/^([@.\/\\\w|^{}-]+)(=(.*))?$/);
if (match) {
// -p plugin
// -p plugin=arg
Expand Down Expand Up @@ -63,10 +63,10 @@ function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) {
}
}
}
if (typeof plugin === 'object' && pluginText in plugin) {
// some plugins do not use `export default` for their entry point.
// attempt to use the plugin name as the named import name.
plugin = plugin[pluginText];
// some plugins do not use `module.exports` for their entry point,
// in which case we try the named default export and the plugin name
if (typeof plugin === 'object') {
plugin = plugin.default || plugin[pluginText]
}
inputOptions.plugins!.push(
typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin
Expand Down
5 changes: 5 additions & 0 deletions test/cli/samples/plugin/default-export/_config.js
@@ -0,0 +1,5 @@
module.exports = {
description: 'CLI --plugin with default export',
skipIfWindows: true,
command: `echo 'console.log(VALUE);' | rollup -p "./my-plugin={VALUE: 'default', ZZZ: 1}"`
};
1 change: 1 addition & 0 deletions test/cli/samples/plugin/default-export/_expected.js
@@ -0,0 +1 @@
console.log("default");
14 changes: 14 additions & 0 deletions test/cli/samples/plugin/default-export/my-plugin.js
@@ -0,0 +1,14 @@
exports.default = function(options) {
if (options === void 0) options = {};
return {
transform(code) {
// dumb search and replace for test purposes
for (var key in options) {
const rx = new RegExp(key, 'g');
const value = JSON.stringify(options[key]);
code = code.replace(rx, value);
}
return code;
}
};
};

0 comments on commit e5cf74c

Please sign in to comment.