From 766228e3a3c55e801807997666830d3c7b336132 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 22 Jun 2020 21:17:47 +0200 Subject: [PATCH 1/4] Strip rollup-plugin prefix to find named plugin exports, throw when export cannot be found --- cli/run/commandPlugins.ts | 26 ++++++++++++++----- docs/01-command-line-reference.md | 4 +-- .../plugin/export-not-found/_config.js | 17 ++++++++++++ .../plugin/export-not-found/_expected.js | 1 + .../export-not-found/my-missing-plugin.js | 0 .../samples/plugin/named-export/_config.js | 5 ++++ .../plugin/named-export/_expected/cjs.js | 1 + .../plugin/named-export/_expected/es.js | 1 + test/cli/samples/plugin/named-export/main.js | 12 +++++++++ .../plugin/named-export/rollup.config.js | 18 +++++++++++++ .../plugin/relative-camelized/_config.js | 5 ++++ .../plugin/relative-camelized/_expected.js | 1 + .../nested/my-super-plugin.js | 5 ++++ 13 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 test/cli/samples/plugin/export-not-found/_config.js create mode 100644 test/cli/samples/plugin/export-not-found/_expected.js create mode 100644 test/cli/samples/plugin/export-not-found/my-missing-plugin.js create mode 100644 test/cli/samples/plugin/named-export/_config.js create mode 100644 test/cli/samples/plugin/named-export/_expected/cjs.js create mode 100644 test/cli/samples/plugin/named-export/_expected/es.js create mode 100644 test/cli/samples/plugin/named-export/main.js create mode 100644 test/cli/samples/plugin/named-export/rollup.config.js create mode 100644 test/cli/samples/plugin/relative-camelized/_config.js create mode 100644 test/cli/samples/plugin/relative-camelized/_expected.js create mode 100644 test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 4e1116d9340..c83b998092c 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -3,10 +3,7 @@ import { InputOptions } from '../../src/rollup/types'; import { stdinPlugin } from './stdin'; import { waitForInputPlugin } from './waitForInput'; -export function addCommandPluginsToInputOptions( - inputOptions: InputOptions, - command: any -) { +export function addCommandPluginsToInputOptions(inputOptions: InputOptions, command: any) { if (command.stdin !== false) { inputOptions.plugins!.push(stdinPlugin()); } @@ -63,16 +60,33 @@ function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { if (pluginText[0] == '.') pluginText = path.resolve(pluginText); plugin = require(pluginText); } catch (ex) { - throw new Error(`Cannot load plugin "${pluginText}"`); + throw new Error(`Cannot load plugin "${pluginText}": ${ex.message}.`); } } } // 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] + plugin = plugin.default || plugin[getCamelizedPluginBaseName(pluginText)]; + } + if (!plugin) { + throw new Error( + `Cannot find entry for plugin "${pluginText}". The plugin needs to export a function either as "default" or "${getCamelizedPluginBaseName( + pluginText + )}" for Rollup to recognize it.` + ); } inputOptions.plugins!.push( typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin ); } + +function getCamelizedPluginBaseName(pluginText: string): string { + return pluginText + .match(/^(@rollup\/plugin-|rollup-plugin-)?(.*)$/)![2] + .split(/[\\/]/) + .slice(-1)[0] + .split('-') + .map((part, index) => (index === 0 || !part ? part : part[0].toUpperCase() + part.slice(1))) + .join(''); +} diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 057b6e8cdc0..1e4c4747ea1 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -342,7 +342,7 @@ Use the specified plugin. There are several ways to specify plugins here: rollup -i input.js -f es -p ./my-plugin.js ``` - The file should export a plugin object or a function returning such an object. + The file should export a function returning a plugin object. - Via the name of a plugin that is installed in a local or global `node_modules` folder: ``` @@ -367,7 +367,7 @@ If you want to load more than one plugin, you can repeat the option or supply a rollup -i input.js -f es -p node-resolve -p commonjs,json ``` -By default, plugins that export functions will be called with no argument to create the plugin. You can however pass a custom argument as well: +By default, plugin functions be called with no argument to create the plugin. You can however pass a custom argument as well: ``` rollup -i input.js -f es -p 'terser={output: {beautify: true, indent_level: 2}}' diff --git a/test/cli/samples/plugin/export-not-found/_config.js b/test/cli/samples/plugin/export-not-found/_config.js new file mode 100644 index 00000000000..014fb0827c0 --- /dev/null +++ b/test/cli/samples/plugin/export-not-found/_config.js @@ -0,0 +1,17 @@ +const { assertIncludes } = require('../../../../utils.js'); +const path = require('path'); + +module.exports = { + description: 'Throws when the plugin export cannot be found', + skipIfWindows: true, + command: `echo 'console.log("ignored");' | rollup -p "./my-missing-plugin"`, + error(err) { + assertIncludes( + err.message, + `[!] Error: Cannot find entry for plugin "${path.join( + __dirname, + 'my-missing-plugin' + )}". The plugin needs to export a function either as "default" or "myMissingPlugin" for Rollup to recognize it.` + ); + } +}; diff --git a/test/cli/samples/plugin/export-not-found/_expected.js b/test/cli/samples/plugin/export-not-found/_expected.js new file mode 100644 index 00000000000..55c40195e84 --- /dev/null +++ b/test/cli/samples/plugin/export-not-found/_expected.js @@ -0,0 +1 @@ +console.log('transformed'); diff --git a/test/cli/samples/plugin/export-not-found/my-missing-plugin.js b/test/cli/samples/plugin/export-not-found/my-missing-plugin.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/cli/samples/plugin/named-export/_config.js b/test/cli/samples/plugin/named-export/_config.js new file mode 100644 index 00000000000..92a84aeb1db --- /dev/null +++ b/test/cli/samples/plugin/named-export/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'supports stripping "rollup-config" prefix to find named plugin export', + skipIfWindows: true, + command: `rollup -c -p rollup-plugin-terser` +}; diff --git a/test/cli/samples/plugin/named-export/_expected/cjs.js b/test/cli/samples/plugin/named-export/_expected/cjs.js new file mode 100644 index 00000000000..112c5d4af42 --- /dev/null +++ b/test/cli/samples/plugin/named-export/_expected/cjs.js @@ -0,0 +1 @@ +"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(t){this.x=t};t.prototype.output=function(){console.log(this.x)},new t(123).output(),exports.Bar=t; diff --git a/test/cli/samples/plugin/named-export/_expected/es.js b/test/cli/samples/plugin/named-export/_expected/es.js new file mode 100644 index 00000000000..6b973ec0d27 --- /dev/null +++ b/test/cli/samples/plugin/named-export/_expected/es.js @@ -0,0 +1 @@ +var t=function(t){this.x=t};t.prototype.output=function(){console.log(this.x)},new t(123).output();export{t as Bar}; diff --git a/test/cli/samples/plugin/named-export/main.js b/test/cli/samples/plugin/named-export/main.js new file mode 100644 index 00000000000..649a9dfefdc --- /dev/null +++ b/test/cli/samples/plugin/named-export/main.js @@ -0,0 +1,12 @@ +class Foo { + constructor(x) { + this.x = x; + } + output() { + console.log(this.x); + } +} + +const foo = new Foo(123); +foo.output(); +export { Foo as Bar }; diff --git a/test/cli/samples/plugin/named-export/rollup.config.js b/test/cli/samples/plugin/named-export/rollup.config.js new file mode 100644 index 00000000000..f2db773934b --- /dev/null +++ b/test/cli/samples/plugin/named-export/rollup.config.js @@ -0,0 +1,18 @@ +const buble = require('@rollup/plugin-buble'); + +export default { + input: 'main.js', + plugins: [ + buble() + ], + output: [ + { + file: '_actual/cjs.js', + format: 'cjs' + }, + { + file: '_actual/es.js', + format: 'esm' + } + ] +}; diff --git a/test/cli/samples/plugin/relative-camelized/_config.js b/test/cli/samples/plugin/relative-camelized/_config.js new file mode 100644 index 00000000000..031de5a9a9c --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'handles plugins where the export name is the camelized file name', + skipIfWindows: true, + command: `echo 'console.log("ignored");' | rollup -p "./nested/my-super-plugin"` +}; diff --git a/test/cli/samples/plugin/relative-camelized/_expected.js b/test/cli/samples/plugin/relative-camelized/_expected.js new file mode 100644 index 00000000000..55c40195e84 --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/_expected.js @@ -0,0 +1 @@ +console.log('transformed'); diff --git a/test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js b/test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js new file mode 100644 index 00000000000..08dbac32c5e --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js @@ -0,0 +1,5 @@ +exports.mySuperPlugin = () => ({ + transform() { + return "console.log('transformed')"; + } +}); From 60d28d103ec17ab91e02325878e790decacff458 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 22 Jun 2020 21:26:22 +0200 Subject: [PATCH 2/4] Also ignore anything after the first "." --- cli/run/commandPlugins.ts | 1 + test/cli/samples/plugin/relative-camelized/_config.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index c83b998092c..a0d5be2fbb6 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -86,6 +86,7 @@ function getCamelizedPluginBaseName(pluginText: string): string { .match(/^(@rollup\/plugin-|rollup-plugin-)?(.*)$/)![2] .split(/[\\/]/) .slice(-1)[0] + .split('.')[0] .split('-') .map((part, index) => (index === 0 || !part ? part : part[0].toUpperCase() + part.slice(1))) .join(''); diff --git a/test/cli/samples/plugin/relative-camelized/_config.js b/test/cli/samples/plugin/relative-camelized/_config.js index 031de5a9a9c..a9c039eb262 100644 --- a/test/cli/samples/plugin/relative-camelized/_config.js +++ b/test/cli/samples/plugin/relative-camelized/_config.js @@ -1,5 +1,5 @@ module.exports = { description: 'handles plugins where the export name is the camelized file name', skipIfWindows: true, - command: `echo 'console.log("ignored");' | rollup -p "./nested/my-super-plugin"` + command: `echo 'console.log("ignored");' | rollup -p "./nested/my-super-plugin.js"` }; From 476a7ba67a5d679b02d81d13ec311e6602667d64 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 22 Jun 2020 21:53:20 +0200 Subject: [PATCH 3/4] Update dependencies --- package-lock.json | 6 +++--- package.json | 2 +- rollup.config.js | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7b92b50291d..e383a0da12f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4103,9 +4103,9 @@ } }, "rollup": { - "version": "2.14.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.14.0.tgz", - "integrity": "sha512-SUsFh2bBemQqCXOCBWRayjK3/6kNHVR8PbSKKYWIdI6e4zuGSW5B1hGVkFi40805dUrqosMLLxMuEyJMylC9YA==", + "version": "2.18.0", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.18.0.tgz", + "integrity": "sha512-LhuQQp3WpnHo3HlKCRrdMXpB6jdLsGOoXXSfMjbv74s5VdV3WZhkYJT0Z6w/EH3UgPH+g/S9T4GJrKW/5iD8TA==", "dev": true, "requires": { "fsevents": "~2.1.2" diff --git a/package.json b/package.json index 289ffc6cf81..93740e42e8d 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,7 @@ "pretty-ms": "^7.0.0", "require-relative": "^0.8.7", "requirejs": "^2.3.6", - "rollup": "^2.14.0", + "rollup": "^2.18.0", "rollup-plugin-license": "^2.1.0", "rollup-plugin-string": "^3.0.0", "rollup-plugin-terser": "^6.1.0", diff --git a/rollup.config.js b/rollup.config.js index 3ea255a09c2..a8e4efe195c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -98,9 +98,7 @@ export default command => { 'util' ], treeshake, - manualChunks: { rollup: ['src/node-entry.ts'] }, - // TODO set this to `true` again once manualChunks has been moved to the output - strictDeprecations: false, + strictDeprecations: true, output: { banner, chunkFileNames: 'shared/[name].js', @@ -110,6 +108,7 @@ export default command => { format: 'cjs', freeze: false, interop: false, + manualChunks: { rollup: ['src/node-entry.ts'] }, sourcemap: true } }; From 6b9b2eba13e3462d44e692c8613fa8c5e542d4bb Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Tue, 23 Jun 2020 21:19:44 +0200 Subject: [PATCH 4/4] Cover more cases --- cli/run/commandPlugins.ts | 3 +-- test/cli/samples/plugin/relative-camelized/_config.js | 7 ++++++- test/cli/samples/plugin/relative-camelized/_expected.js | 6 +++++- .../plugin/relative-camelized/nested/my-super-plugin.js | 5 ----- .../relative-camelized/plugins/@rollup/plugin-supreme.js | 5 +++++ .../plugin/relative-camelized/plugins/my-super-plugin1.js | 5 +++++ .../plugin/relative-camelized/plugins/rollup-plugin- | 5 +++++ .../plugins/rollup-plugin-my-super-plugin2.js | 5 +++++ 8 files changed, 32 insertions(+), 9 deletions(-) delete mode 100644 test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js create mode 100644 test/cli/samples/plugin/relative-camelized/plugins/@rollup/plugin-supreme.js create mode 100644 test/cli/samples/plugin/relative-camelized/plugins/my-super-plugin1.js create mode 100644 test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin- create mode 100644 test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin-my-super-plugin2.js diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index a0d5be2fbb6..9e3847b64dc 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -82,8 +82,7 @@ function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { } function getCamelizedPluginBaseName(pluginText: string): string { - return pluginText - .match(/^(@rollup\/plugin-|rollup-plugin-)?(.*)$/)![2] + return (pluginText.match(/(@rollup\/plugin-|rollup-plugin-)(.+)$/)?.[2] || pluginText) .split(/[\\/]/) .slice(-1)[0] .split('.')[0] diff --git a/test/cli/samples/plugin/relative-camelized/_config.js b/test/cli/samples/plugin/relative-camelized/_config.js index a9c039eb262..6ee87968574 100644 --- a/test/cli/samples/plugin/relative-camelized/_config.js +++ b/test/cli/samples/plugin/relative-camelized/_config.js @@ -1,5 +1,10 @@ module.exports = { description: 'handles plugins where the export name is the camelized file name', skipIfWindows: true, - command: `echo 'console.log("ignored");' | rollup -p "./nested/my-super-plugin.js"` + command: + `echo 'console.log("initial");' | rollup ` + + '-p "./plugins/my-super-plugin1.js" ' + + '-p "./plugins/rollup-plugin-my-super-plugin2.js" ' + + '-p "./plugins/rollup-plugin-" ' + + '-p "./plugins/@rollup/plugin-supreme"' }; diff --git a/test/cli/samples/plugin/relative-camelized/_expected.js b/test/cli/samples/plugin/relative-camelized/_expected.js index 55c40195e84..f7682827743 100644 --- a/test/cli/samples/plugin/relative-camelized/_expected.js +++ b/test/cli/samples/plugin/relative-camelized/_expected.js @@ -1 +1,5 @@ -console.log('transformed'); +console.log("initial"); +console.log("plugin1"); +console.log("plugin2"); +console.log("plugin3"); +console.log("plugin4"); \ No newline at end of file diff --git a/test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js b/test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js deleted file mode 100644 index 08dbac32c5e..00000000000 --- a/test/cli/samples/plugin/relative-camelized/nested/my-super-plugin.js +++ /dev/null @@ -1,5 +0,0 @@ -exports.mySuperPlugin = () => ({ - transform() { - return "console.log('transformed')"; - } -}); diff --git a/test/cli/samples/plugin/relative-camelized/plugins/@rollup/plugin-supreme.js b/test/cli/samples/plugin/relative-camelized/plugins/@rollup/plugin-supreme.js new file mode 100644 index 00000000000..6491f1c9e0f --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/plugins/@rollup/plugin-supreme.js @@ -0,0 +1,5 @@ +exports.supreme = () => ({ + transform(code) { + return `${code}console.log("plugin4");\n`; + } +}); diff --git a/test/cli/samples/plugin/relative-camelized/plugins/my-super-plugin1.js b/test/cli/samples/plugin/relative-camelized/plugins/my-super-plugin1.js new file mode 100644 index 00000000000..f7e4e42c1b8 --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/plugins/my-super-plugin1.js @@ -0,0 +1,5 @@ +exports.mySuperPlugin1 = () => ({ + transform(code) { + return `${code}console.log("plugin1");\n`; + } +}); diff --git a/test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin- b/test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin- new file mode 100644 index 00000000000..cde8d8ab892 --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin- @@ -0,0 +1,5 @@ +exports.rollupPlugin = () => ({ + transform(code) { + return `${code}console.log("plugin3");\n`; + } +}); diff --git a/test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin-my-super-plugin2.js b/test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin-my-super-plugin2.js new file mode 100644 index 00000000000..7e53db9ed8b --- /dev/null +++ b/test/cli/samples/plugin/relative-camelized/plugins/rollup-plugin-my-super-plugin2.js @@ -0,0 +1,5 @@ +exports.mySuperPlugin2 = () => ({ + transform(code) { + return `${code}console.log("plugin2");\n`; + } +});