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 --out-file-extension option to babel-cli #9144

Merged
merged 6 commits into from Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions packages/babel-cli/src/babel/dir.js
Expand Up @@ -23,8 +23,12 @@ export default async function({
return false;
}

// remove extension and then append back on .js
relative = util.adjustRelative(relative, cliOptions.keepFileExtension);
relative = util.withExtension(
relative,
cliOptions.keepFileExtension
? path.extname(relative)
: cliOptions.outFileExtension,
);

const dest = getDest(relative, base);

Expand Down
11 changes: 11 additions & 0 deletions packages/babel-cli/src/babel/options.js
Expand Up @@ -149,6 +149,10 @@ commander.option(
"--delete-dir-on-start",
"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.usage("[options] <files ...>");
Expand Down Expand Up @@ -218,6 +222,12 @@ export default function parseArgv(args: Array<string>): CmdOptions {
);
}

if (commander.keepFileExtension && commander.outFileExtension) {
errors.push(
"--out-file-extension cannot be used with --keep-file-extension",
);
}

if (errors.length) {
console.error("babel:");
errors.forEach(function(e) {
Expand Down Expand Up @@ -274,6 +284,7 @@ export default function parseArgv(args: Array<string>): CmdOptions {
filenames,
extensions: opts.extensions,
keepFileExtension: opts.keepFileExtension,
outFileExtension: opts.outFileExtension,
watch: opts.watch,
skipInitialBuild: opts.skipInitialBuild,
outFile: opts.outFile,
Expand Down
11 changes: 3 additions & 8 deletions packages/babel-cli/src/babel/util.js
Expand Up @@ -127,12 +127,7 @@ export function requireChokidar(): Object {
}
}

export function adjustRelative(
relative: string,
keepFileExtension: boolean,
): string {
if (keepFileExtension) {
return relative;
}
return relative.replace(/\.(\w*?)$/, "") + ".js";
export function withExtension(filename: string, ext: string = ".js") {
const newBasename = path.basename(filename, path.extname(filename)) + ext;
return path.join(path.dirname(filename), newBasename);
}
@@ -0,0 +1 @@
arr.map(x => x / DIVIDER);
@@ -0,0 +1 @@
arr.map(x => x * MULTIPLIER);
@@ -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;
});
@@ -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