-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce CLI --plugin support #3379
Changes from 1 commit
ead93ed
5efed08
a179256
b3543a3
d6d94da
721beb8
dbd2f3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { realpathSync } from 'fs'; | ||
import relative from 'require-relative'; | ||
import { WarningHandler } from '../../src/rollup/types'; | ||
import { InputOptions, WarningHandler } from '../../src/rollup/types'; | ||
import mergeOptions, { GenericConfigObject } from '../../src/utils/mergeOptions'; | ||
import { getAliasName } from '../../src/utils/relativeId'; | ||
import { handleError } from '../logging'; | ||
|
@@ -118,7 +118,68 @@ async function execute( | |
if (command.stdin !== false) { | ||
inputOptions.plugins!.push(stdinPlugin()); | ||
} | ||
if (command.plugin) { | ||
const plugins = Array.isArray(command.plugin) ? command.plugin : [command.plugin]; | ||
for (const plugin of plugins) { | ||
if (/[={}]/.test(plugin)) { | ||
// -p plugin=value | ||
// -p "{transform(c,i){...}}" | ||
loadAndRegisterPlugin(inputOptions, plugin) | ||
} else { | ||
// split out plugins joined by commas | ||
// -p node-resolve,commonjs,buble | ||
plugin.split(",").forEach((plugin: string) => loadAndRegisterPlugin(inputOptions, plugin)); | ||
} | ||
} | ||
} | ||
await build(inputOptions, outputOptions, warnings, command.silent); | ||
} | ||
} | ||
|
||
function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { | ||
let plugin : any = null; | ||
let pluginArg : any = undefined; | ||
if (pluginText[0] === '{') { | ||
// -p "{transform(c,i){...}}" | ||
plugin = new Function('return ' + pluginText); | ||
} else { | ||
const match = pluginText.match(/^([@.\/\\\w|^{}|-]+)(=(.*))?$/); | ||
if (match) { | ||
// -p plugin | ||
// -p plugin=arg | ||
pluginText = match[1]; | ||
pluginArg = new Function('return ' + match[3])(); | ||
} else { | ||
throw new Error(`Invalid --plugin argument format: ${JSON.stringify(pluginText)}`); | ||
} | ||
if (!/^\.|[@\/\\]/.test(pluginText)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I completely understand this regexp. Why would I want to try adding prefixes to a path that starts with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you've missed the or. Only trying the plugin prefixes if it does not start with a dot, or if it does not contain an @ or slashes - only node resolve should be used in those cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upon re-reading your comments, you may have missed the leading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it is even worse, I saw the |
||
// Try using plugin prefix variations first if applicable. | ||
// Prefix order is significant - left has higher precedence. | ||
for (const prefix of ['@rollup/plugin-', 'rollup-plugin-']) { | ||
if (!RegExp(prefix).test(pluginText)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the only relevant case here is that the pluginText actually starts with one of the prefixes. Also, it makes no sense to try to require if (!['.', '@rollup/plugin-', 'rollup-plugin-'].some(prefix => pluginText.startsWith(prefix)) { //… and get rid of the second On further thought, it makes sense IMO to always treat a plugin starting with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to take over this PR as long as all the tests pass. The functionality is sufficient for my own projects. The idea was that plugins should use node resolve unless they were relative or absolute paths. The if in question prevents an unnecessary file system lookup in cases were the path is relative or absolute. If you can accomplish that through other simpler means, that's fine too. |
||
try { | ||
plugin = require(prefix + pluginText); | ||
break; | ||
} catch (ex) { | ||
// do nothing | ||
} | ||
} | ||
} | ||
} | ||
if (!plugin) { | ||
try { | ||
if (pluginText[0] == ".") pluginText = process.cwd() + "/" + pluginText; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you can get it to work, sure, please do so. I only want plugins with relative paths to work from the current working directory from which the command was run. |
||
plugin = require(pluginText); | ||
} catch (ex) { | ||
throw new Error(`Cannot load plugin '${pluginText}'`); | ||
} | ||
} | ||
} | ||
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]; | ||
} | ||
inputOptions.plugins!.push(typeof plugin === 'function' ? plugin.call(plugin, pluginArg) : plugin); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
description: 'CLI --plugin /absolute/path', | ||
skipIfWindows: true, | ||
command: `echo 'console.log(VALUE);' | rollup -p "\`pwd\`/my-plugin={VALUE: 'absolute', ZZZ: 1}"` | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("absolute"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = 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; | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
description: 'advanced CLI --plugin functionality with rollup config', | ||
skipIfWindows: true, | ||
command: `rollup -c -p node-resolve,commonjs -p "terser={output: {beautify: true, indent_level: 2}}"` | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: !0 | ||
}); | ||
|
||
var t = function() { | ||
function t(t) { | ||
this.x = t; | ||
} | ||
return t.prototype.output = function() { | ||
var t; | ||
t = this.x, console.log(t); | ||
}, t; | ||
}(); | ||
|
||
new t(123).output(), exports.Bar = t; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var t = function() { | ||
function t(t) { | ||
this.x = t; | ||
} | ||
return t.prototype.output = function() { | ||
var t; | ||
t = this.x, console.log(t); | ||
}, t; | ||
}(); | ||
|
||
new t(123).output(); | ||
|
||
export { t as Bar }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {Foo} from "foo"; | ||
var foo = new Foo(123); | ||
foo.output(); | ||
export {Foo as Bar}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} | ||
] | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'basic CLI --plugin functionality', | ||
command: `rollup main.js -f cjs --plugin rollup-plugin-buble` | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
var Bar = function Bar(x) { | ||
this.x = value; | ||
}; | ||
Bar.prototype.value = function value () { | ||
return this.x; | ||
}; | ||
var bar = new Bar(123); | ||
console.log(bar.value()); | ||
|
||
exports.Bar = Bar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class Bar { | ||
constructor(x) { | ||
this.x = value; | ||
} | ||
value() { | ||
return this.x; | ||
} | ||
} | ||
var bar = new Bar(123); | ||
console.log(bar.value()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { assertStderrIncludes } = require('../../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'unknown CLI --plugin results in an error', | ||
skipIfWindows: true, | ||
command: `echo "console.log(123);" | rollup --plugin foobar`, | ||
stderr(err) { | ||
assertStderrIncludes(err, "Cannot load plugin 'foobar'"); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { assertStderrIncludes } = require('../../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'invalid CLI --plugin argument format', | ||
skipIfWindows: true, | ||
command: `echo "console.log(123);" | rollup --plugin 'foo bar'`, | ||
stderr(err) { | ||
assertStderrIncludes(err, 'Invalid --plugin argument format: "foo bar"'); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
description: 'CLI --plugin object', | ||
skipIfWindows: true, | ||
command: `echo 'console.log(42);' | rollup -f cjs -p '{transform: c => c + String.fromCharCode(10) + c}'` | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
console.log(42); | ||
|
||
console.log(42); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
description: 'CLI --plugin ../relative/path', | ||
skipIfWindows: true, | ||
command: `echo 'console.log(VALUE);' | rollup -p "../absolute/my-plugin={VALUE: 'relative', ZZZ: 1}"` | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("relative"); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one!