Add --out-file-extension option to babel-cli (#9144)
* Add --use-file-extension option to babel-cli * refactor file extension logic use existing methods from path module * Rename use-file-extension -> out-file-extension * add error message if bot keep- and out-file-extension is used * add test for error when mixing --keep- and --out-file-extension * poke travis
This commit is contained in:
parent
c3388ea42f
commit
3af02f63de
@ -27,8 +27,12 @@ export default async function({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove extension and then append back on .js
|
relative = util.withExtension(
|
||||||
relative = util.adjustRelative(relative, cliOptions.keepFileExtension);
|
relative,
|
||||||
|
cliOptions.keepFileExtension
|
||||||
|
? path.extname(relative)
|
||||||
|
: cliOptions.outFileExtension,
|
||||||
|
);
|
||||||
|
|
||||||
const dest = getDest(relative, base);
|
const dest = getDest(relative, base);
|
||||||
|
|
||||||
|
|||||||
@ -160,6 +160,10 @@ commander.option(
|
|||||||
"--delete-dir-on-start",
|
"--delete-dir-on-start",
|
||||||
"Delete the out directory before compilation.",
|
"Delete the out directory before compilation.",
|
||||||
);
|
);
|
||||||
|
commander.option(
|
||||||
|
"--out-file-extension [string]",
|
||||||
|
"Use a specific extension for the output files",
|
||||||
|
);
|
||||||
|
|
||||||
commander.version(pkg.version + " (@babel/core " + version + ")");
|
commander.version(pkg.version + " (@babel/core " + version + ")");
|
||||||
commander.usage("[options] <files ...>");
|
commander.usage("[options] <files ...>");
|
||||||
@ -237,6 +241,12 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (commander.keepFileExtension && commander.outFileExtension) {
|
||||||
|
errors.push(
|
||||||
|
"--out-file-extension cannot be used with --keep-file-extension",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
console.error("babel:");
|
console.error("babel:");
|
||||||
errors.forEach(function(e) {
|
errors.forEach(function(e) {
|
||||||
@ -293,6 +303,7 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
|
|||||||
filenames,
|
filenames,
|
||||||
extensions: opts.extensions,
|
extensions: opts.extensions,
|
||||||
keepFileExtension: opts.keepFileExtension,
|
keepFileExtension: opts.keepFileExtension,
|
||||||
|
outFileExtension: opts.outFileExtension,
|
||||||
watch: opts.watch,
|
watch: opts.watch,
|
||||||
skipInitialBuild: opts.skipInitialBuild,
|
skipInitialBuild: opts.skipInitialBuild,
|
||||||
outFile: opts.outFile,
|
outFile: opts.outFile,
|
||||||
|
|||||||
@ -127,12 +127,7 @@ export function requireChokidar(): Object {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function adjustRelative(
|
export function withExtension(filename: string, ext: string = ".js") {
|
||||||
relative: string,
|
const newBasename = path.basename(filename, path.extname(filename)) + ext;
|
||||||
keepFileExtension: boolean,
|
return path.join(path.dirname(filename), newBasename);
|
||||||
): string {
|
|
||||||
if (keepFileExtension) {
|
|
||||||
return relative;
|
|
||||||
}
|
|
||||||
return relative.replace(/\.(\w*?)$/, "") + ".js";
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
arr.map(x => x / DIVIDER);
|
||||||
@ -0,0 +1 @@
|
|||||||
|
arr.map(x => x * MULTIPLIER);
|
||||||
12
packages/babel-cli/test/fixtures/babel/dir --out-dir --out-file-extension/options.json
vendored
Normal file
12
packages/babel-cli/test/fixtures/babel/dir --out-dir --out-file-extension/options.json
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"args": [
|
||||||
|
"src",
|
||||||
|
"--out-dir",
|
||||||
|
"lib",
|
||||||
|
"--out-file-extension",
|
||||||
|
".mjs",
|
||||||
|
"--extensions",
|
||||||
|
".jsx,.mjs",
|
||||||
|
"--verbose"
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
arr.map(function (x) {
|
||||||
|
return x / DIVIDER;
|
||||||
|
});
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
arr.map(function (x) {
|
||||||
|
return x * MULTIPLIER;
|
||||||
|
});
|
||||||
3
packages/babel-cli/test/fixtures/babel/dir --out-dir --out-file-extension/stdout.txt
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/dir --out-dir --out-file-extension/stdout.txt
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
src/bar.mjs -> lib/bar.mjs
|
||||||
|
src/foo.jsx -> lib/foo.mjs
|
||||||
|
Successfully compiled 2 files with Babel.
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"args": ["--keep-file-extension", "--out-file-extension", ".mjs"],
|
||||||
|
"stderrContains": true
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
--out-file-extension cannot be used with --keep-file-extension
|
||||||
Loading…
x
Reference in New Issue
Block a user