Skip to content

Commit

Permalink
Add --use-file-extension option to babel-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Feb 26, 2019
1 parent d0e196d commit cce3797
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/babel-cli/src/babel/dir.js
Expand Up @@ -17,8 +17,12 @@ export default async function({ cliOptions, babelOptions }) {
return false;
}

// remove extension and then append back on .js
relative = util.adjustRelative(relative, cliOptions.keepFileExtension);
// remove extension and then append back on the configured extension
relative = util.adjustRelative(
relative,
cliOptions.keepFileExtension,
cliOptions.useFileExtension,
);

const dest = getDest(relative, base);

Expand Down
5 changes: 5 additions & 0 deletions packages/babel-cli/src/babel/options.js
Expand Up @@ -147,6 +147,10 @@ commander.option(
"--delete-dir-on-start",
"Delete the out directory before compilation",
);
commander.option(
"--use-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 @@ -267,6 +271,7 @@ export default function parseArgv(args: Array<string>) {
filenames,
extensions: opts.extensions,
keepFileExtension: opts.keepFileExtension,
useFileExtension: opts.useFileExtension,
watch: opts.watch,
skipInitialBuild: opts.skipInitialBuild,
outFile: opts.outFile,
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-cli/src/babel/util.js
Expand Up @@ -118,9 +118,9 @@ export function requireChokidar() {
}
}

export function adjustRelative(relative, keepFileExtension) {
export function adjustRelative(relative, keepFileExtension, extension = ".js") {
if (keepFileExtension) {
return relative;
}
return relative.replace(/\.(\w*?)$/, "") + ".js";
return relative.replace(/\.(\w*?)$/, "") + extension;
}
@@ -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",
"--use-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 comments on commit cce3797

Please sign in to comment.