Skip to content

Commit

Permalink
Merge branch 'master' into feat/yargs-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 6, 2020
2 parents 5d690cc + eeda078 commit fe89647
Show file tree
Hide file tree
Showing 95 changed files with 714 additions and 320 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
@@ -1,10 +1,16 @@
# rollup changelog

## 1.31.2
*unreleased*
## 1.32.0
*2020-02-28*

### Features
* Allow adding plugins on the command line via `--plugin <plugin>` (#3379)

### Pull Requests
* [#3379](https://github.com/rollup/rollup/pull/3379): introduce CLI --plugin support (@kzc)
* [#3390](https://github.com/rollup/rollup/pull/3390): fix typo: this.addWatchfile (@mistlog)
* [#3392](https://github.com/rollup/rollup/pull/3392): Bump codecov from 3.6.1 to 3.6.5
* [#3404](https://github.com/rollup/rollup/pull/3404): Update resolveFileUrl docs (@jakearchibald)

## 1.31.1
*2020-02-14*
Expand Down
1 change: 1 addition & 0 deletions cli/help.md
Expand Up @@ -16,6 +16,7 @@ Basic options:
-m, --sourcemap Generate sourcemap (`-m inline` for inline map)
-n, --name <name> Name for UMD export
-o, --file <output> Single output file (if absent, prints to stdout)
-p, --plugin <plugin> Use the plugin specified (may be repeated)
-v, --version Show version number
-w, --watch Watch files in bundle and rebuild on changes
--amd.id <id> ID for AMD module (default is anonymous)
Expand Down
16 changes: 4 additions & 12 deletions cli/run/build.ts
Expand Up @@ -13,13 +13,11 @@ export default function build(
outputOptions: OutputOptions[],
warnings: BatchWarnings,
silent = false
) {
): Promise<unknown> {
const useStdout = !outputOptions[0].file && !outputOptions[0].dir;

const start = Date.now();
const files = useStdout
? ['stdout']
: outputOptions.map(t => relativeId(t.file || t.dir!));
const files = useStdout ? ['stdout'] : outputOptions.map(t => relativeId(t.file || t.dir!));
if (!silent) {
let inputFiles: string | undefined;
if (typeof inputOptions.input === 'string') {
Expand Down Expand Up @@ -61,13 +59,11 @@ export default function build(
process.stdout.write('\n' + tc.cyan(tc.bold('//→ ' + file.fileName + ':')) + '\n');
process.stdout.write(source);
}
return null
return null;
});
}

return Promise.all(outputOptions.map(output => bundle.write(output))).then(
() => bundle
);
return Promise.all(outputOptions.map(output => bundle.write(output))).then(() => bundle);
})
.then((bundle: RollupBuild | null) => {
if (!silent) {
Expand All @@ -79,9 +75,5 @@ export default function build(
printTimings(bundle.getTimings());
}
}
})
.catch((err: Error) => {
warnings.flush();
handleError(err);
});
}
91 changes: 80 additions & 11 deletions cli/run/index.ts
@@ -1,6 +1,7 @@
import { realpathSync } from 'fs';
import * as path from 'path';
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';
Expand Down Expand Up @@ -107,18 +108,86 @@ async function execute(
} else {
for (const config of configs) {
const warnings = batchWarnings();
const { inputOptions, outputOptions, optionError } = mergeOptions({
command,
config,
defaultOnWarnHandler: warnings.add
});
if (optionError) {
(inputOptions.onwarn as WarningHandler)({ code: 'UNKNOWN_OPTION', message: optionError });
try {
const { inputOptions, outputOptions, optionError } = mergeOptions({
command,
config,
defaultOnWarnHandler: warnings.add
});
if (optionError) {
(inputOptions.onwarn as WarningHandler)({ code: 'UNKNOWN_OPTION', message: optionError });
}
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);
} catch (err) {
warnings.flush();
handleError(err);
}
}
}
}

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 (!/^\.|^rollup-plugin-|[@\/\\]/.test(pluginText)) {
// Try using plugin prefix variations first if applicable.
// Prefix order is significant - left has higher precedence.
for (const prefix of ['@rollup/plugin-', 'rollup-plugin-']) {
try {
plugin = require(prefix + pluginText);
break;
} catch (ex) {
// if this does not work, we try requiring the actual name below
}
}
if (command.stdin !== false) {
inputOptions.plugins!.push(stdinPlugin());
}
if (!plugin) {
try {
if (pluginText[0] == '.') pluginText = path.resolve(pluginText);
plugin = require(pluginText);
} catch (ex) {
throw new Error(`Cannot load plugin "${pluginText}"`);
}
await build(inputOptions, outputOptions, warnings, command.silent);
}
}
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
);
}
42 changes: 42 additions & 0 deletions docs/01-command-line-reference.md
Expand Up @@ -220,6 +220,7 @@ Many options have command line equivalents. In those cases, any arguments passed
-m, --sourcemap Generate sourcemap (`-m inline` for inline map)
-n, --name <name> Name for UMD export
-o, --file <output> Single output file (if absent, prints to stdout)
-p, --plugin <plugin> Use the plugin specified (may be repeated)
-v, --version Show version number
-w, --watch Watch files in bundle and rebuild on changes
--amd.id <id> ID for AMD module (default is anonymous)
Expand Down Expand Up @@ -268,6 +269,47 @@ The flags listed below are only available via the command line interface. All ot

Print the help document.

#### `-p <plugin>`, `--plugin <plugin>`

Use the specified plugin. There are several ways to specify plugins here:

- Via a relative path:

```
rollup -i input.js -f es -p ./my-plugin.js
```

The file should export a plugin object or a function returning such an object.
- Via the name of a plugin that is installed in a local or global `node_modules` folder:

```
rollup -i input.js -f es -p @rollup/plugin-node-resolve
```

If the plugin name does not start with `rollup-plugin-` or `@rollup/plugin-`, Rollup will automatically try adding these prefixes:

```
rollup -i input.js -f es -p node-resolve
```

- Via an inline implementation:

```
rollup -i input.js -f es -p '{transform: (c, i) => `/* ${JSON.stringify(i)} */\n${c}`}'
```

If you want to load more than one plugin, you can repeat the option or supply a comma-separated list of names:

```
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:

```
rollup -i input.js -f es -p 'terser={output: {beautify: true, indent_level: 2}}'
```

#### `-v`/`--version`

Print the installed version number.
Expand Down
2 changes: 1 addition & 1 deletion docs/04-tutorial.md
Expand Up @@ -240,7 +240,7 @@ _Note: Only the data we actually need gets imported – `name` and `devDependenc

### Using output plugins

Some plugins can also be applied specifically to some outputs. See [plugin hooks](guide/en/#hooks) for the technical details of what output-specific plugins can do. In a nut-shell, those plugins can only modify code after the main analysis of Rollup has completed. Rollup will warn if an incompatible plugin is used as an output-specific plugin. One possible use-case is minification of bundles to be consumed in a browser.
Some plugins can also be applied specifically to some outputs. See [plugin hooks](guide/en/#build-hooks) for the technical details of what output-specific plugins can do. In a nut-shell, those plugins can only modify code after the main analysis of Rollup has completed. Rollup will warn if an incompatible plugin is used as an output-specific plugin. One possible use-case is minification of bundles to be consumed in a browser.

Let us extend the previous example to provide a minified build together with the non-minified one. To that end, we install `rollup-plugin-terser`:

Expand Down

0 comments on commit fe89647

Please sign in to comment.