Skip to content
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

add ability to specify stdin file extension via --stdin=ext #3775

Merged
merged 3 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 };