Skip to content

Commit

Permalink
add ability to specify stdin file extension via --stdin=ext (#3775)
Browse files Browse the repository at this point in the history
* add ability to specify the CLI --stdin file extension

* Update documentation

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 16, 2020
1 parent 1a6cb46 commit 916718f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cli/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Basic options:
--failAfterWarnings Exit with an error code if there was a warning during the build
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--no-stdin do not read "-" from stdin
--stdin=ext Specify file extension used for stdin input - default is none
--no-stdin Do not read "-" from stdin
--no-strict Don't emit `"use strict";` in the generated modules
--strictDeprecations Throw errors for deprecated features
--systemNullSetters Replace empty SystemJS setters with `null`
Expand Down
2 changes: 1 addition & 1 deletion cli/run/commandPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { waitForInputPlugin } from './waitForInput';

export function addCommandPluginsToInputOptions(inputOptions: InputOptions, command: any) {
if (command.stdin !== false) {
inputOptions.plugins!.push(stdinPlugin());
inputOptions.plugins!.push(stdinPlugin(command.stdin));
}
if (command.waitForBundleInput === true) {
inputOptions.plugins!.push(waitForInputPlugin());
Expand Down
7 changes: 4 additions & 3 deletions cli/run/stdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ export const stdinName = '-';

let stdinResult: Promise<string> | null = null;

export function stdinPlugin(): Plugin {
export function stdinPlugin(arg: any): Plugin {
const suffix = typeof arg == 'string' && arg.length ? '.' + arg : '';
return {
name: 'stdin',
resolveId(id) {
if (id === stdinName) {
return id;
return id + suffix;
}
},
load(id) {
if (id === stdinName) {
if (id === stdinName || id.startsWith(stdinName + '.')) {
return stdinResult || (stdinResult = readStdin());
}
}
Expand Down
17 changes: 14 additions & 3 deletions docs/01-command-line-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ Many options have command line equivalents. In those cases, any arguments passed
--failAfterWarnings Exit with an error code if there was a warning during the build
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--no-stdin do not read "-" from stdin
--stdin=ext Specify file extension used for stdin input - default is none
--no-stdin Do not read "-" from stdin
--no-strict Don't emit `"use strict";` in the generated modules
--strictDeprecations Throw errors for deprecated features
--systemNullSetters Replace empty SystemJS setters with `null`
Expand Down Expand Up @@ -423,9 +424,13 @@ then the config file will receive `process.env.INCLUDE_DEPS === 'true'` and `pro

This will not throw an error if one of the entry point files is not available. Instead, it will wait until all files are present before starting the build. This is useful, especially in watch mode, when Rollup is consuming the output of another process.

#### `--stdin=ext`

Specify a virtual file extension when reading content from stdin. By default, Rollup will use the virtual file name `-` without an extension for content read from stdin. Some plugins, however, rely on file extensions to determine if they should process a file. See also [Reading a file from stdin](guide/en/#reading-a-file-from-stdin).

#### `--no-stdin`

Do not read files from `stdin`. Setting this flag will prevent piping content to Rollup and make sure Rollup interprets `-` as a regular file name instead of interpreting this as the name of `stdin`. See also [Reading a file from stdin](guide/en/#reading-a-file-from-stdin).
Do not read files from `stdin`. Setting this flag will prevent piping content to Rollup and make sure Rollup interprets `-` and `-.[ext]` as a regular file names instead of interpreting these as the name of `stdin`. See also [Reading a file from stdin](guide/en/#reading-a-file-from-stdin).

### Reading a file from stdin

Expand All @@ -443,4 +448,10 @@ import foo from "-";

in any file will prompt Rollup to try to read the imported file from `stdin` and assign the default export to `foo`. You can pass the [`--no-stdin`](guide/en/#--no-stdin) CLI flag to Rollup to treat `-` as a regular file name instead.

The JavaScript API will always treat `-` as a regular file name.
As some plugins rely on file extensions to process files, you can specify a file extension for stdin via `--stdin=ext` where `ext` is the desired extension. In that case, the virtual file name will be `-.ext`:

```
echo '{"foo": 42, "bar": "ok"}' | rollup --stdin=json -p json
```

The JavaScript API will always treat `-` and `-.ext` as regular file names.
5 changes: 5 additions & 0 deletions test/cli/samples/stdin/commonjs/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
description: 'uses stdin to inline a require()',
skipIfWindows: true,
command: `echo 'console.log(require("./add.js")(2, 1));' | rollup --stdin=js -p commonjs -f cjs --exports=auto`
};
11 changes: 11 additions & 0 deletions test/cli/samples/stdin/commonjs/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

var add = (x, y) => x + y;

console.log(add(2, 1));

var _ = {

};

module.exports = _;
1 change: 1 addition & 0 deletions test/cli/samples/stdin/commonjs/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = (x, y) => x + y;
5 changes: 5 additions & 0 deletions test/cli/samples/stdin/json/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
description: 'pipe JSON over stdin to create a module',
skipIfWindows: true,
command: `echo '{"foo": 42, "bar": "ok"}' | rollup --stdin=json -p json`
};
9 changes: 9 additions & 0 deletions test/cli/samples/stdin/json/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var foo = 42;
var bar = "ok";
var _ = {
foo: foo,
bar: bar
};

export default _;
export { bar, foo };

0 comments on commit 916718f

Please sign in to comment.