diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 55b9aa3ce37..1a358c3d316 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -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 @@ -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 diff --git a/test/cli/samples/plugin/default-export/_config.js b/test/cli/samples/plugin/default-export/_config.js new file mode 100644 index 00000000000..2a74334e40b --- /dev/null +++ b/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}"` +}; diff --git a/test/cli/samples/plugin/default-export/_expected.js b/test/cli/samples/plugin/default-export/_expected.js new file mode 100644 index 00000000000..b4889737d60 --- /dev/null +++ b/test/cli/samples/plugin/default-export/_expected.js @@ -0,0 +1 @@ +console.log("default"); diff --git a/test/cli/samples/plugin/default-export/my-plugin.js b/test/cli/samples/plugin/default-export/my-plugin.js new file mode 100644 index 00000000000..2f6a021c39e --- /dev/null +++ b/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; + } + }; +};