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

feat(cli): add sourceType and comments #861

Merged
merged 1 commit into from
May 22, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/babel-minify/__tests__/__snapshots__/cli-tests.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ Object {
}
`;

exports[`babel-minify CLI should handle comments 1`] = `
Array [
Object {
"stderr": "",
"stdout": "import Foo from\\"foo\\";import pick from\\"lodash.pick\\";export const items=pick(Foo.all,[\\"a\\",\\"b\\",\\"c\\"]);export default Foo(items);",
},
Object {
"stderr": "",
"stdout": "// comment 1
import Foo from\\"foo\\";// comment 2
import pick from\\"lodash.pick\\";export const items/* comment 3 */=pick(Foo.all,[// comment 4
\\"a\\",// comment 5
\\"b\\",// comment 6
\\"c\\"]);export default Foo(items);",
},
]
`;

exports[`babel-minify CLI should throw on all invalid options 1`] = `
Object {
"code": 1,
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-minify/__tests__/cli-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function foo() {

const sampleInputFile = path.join(__dirname, "fixtures/out-file/foo.js");
const sampleInputDir = path.join(__dirname, "fixtures/out-dir/a");
const sampleInputModule = path.join(__dirname, "fixtures/module/mod.js");

const tempOutFile = path.join(__dirname, "fixtures/out-file/foo.min.js");
const tempOutDir = path.join(__dirname, "fixtures/out-dir/min");
Expand Down Expand Up @@ -101,4 +102,17 @@ describe("babel-minify CLI", () => {
await runCli([sampleInputDir, "--out-dir", tempOutDir]);
expect(await readFile(tempOutDirFile)).toMatchSnapshot();
});

it("should handle source type", async () => {
return expect(runCli([sampleInputModule, "--sourceType module"])).resolves;
});

it("should handle comments", async () => {
return expect(
Promise.all([
runCli([sampleInputModule, "--sourceType module", "--comments false"]),
runCli([sampleInputModule, "--sourceType module", "--comments true"])
])
).resolves.toMatchSnapshot();
});
});
16 changes: 16 additions & 0 deletions packages/babel-minify/__tests__/fixtures/module/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// comment 1
import Foo from "foo";

// comment 2
import pick from "lodash.pick";

export const items /* comment 3 */ = pick(Foo.all, [
// comment 4
"a",
// comment 5
"b",
// comment 6
"c"
]);

export default Foo(items);
1 change: 1 addition & 0 deletions packages/babel-minify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@babel/core": "^7.0.0-beta.46",
"babel-preset-minify": "^0.4.3",
"fs-readdir-recursive": "^1.1.0",
"lodash.pick": "^4.4.0",
"mkdirp": "^0.5.1",
"util.promisify": "^1.0.0",
"yargs-parser": "^10.0.0"
Expand Down
42 changes: 27 additions & 15 deletions packages/babel-minify/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const yargsParser = require("yargs-parser");
const optionsParser = require("./options-parser");
const { version } = require("../package.json");
const { handleStdin, handleFile, handleArgs, isFile } = require("./fs");
const pick = require("lodash.pick");

const plugins = [
"booleans",
Expand Down Expand Up @@ -53,8 +54,8 @@ const typeConsOpts = [
"typeConstructors.string"
];

const cliBooleanOpts = ["help", "version"];
const cliOpts = ["out-file", "out-dir"];
const cliBooleanOpts = ["help", "version", "comments"];
const cliOpts = ["out-file", "out-dir", "sourceType"];
const alias = {
outFile: "o",
outDir: "d",
Expand All @@ -76,6 +77,12 @@ function printHelpInfo({ exitCode = 0 } = {}) {
--out-file, -o Output to a specific file
--out-dir, -d Output to a specific directory

Parser/Generator options
--sourceType Indicate the mode the code should be parsed in. Valid options
are "script" | "module" | "unambiguous"
--comments Enable/Disable comments in the output. For more specific control,
use the Node API

Transform Options:
--mangle Context and scope aware variable renaming
--simplify Simplifies code for minification by reducing statements into
Expand Down Expand Up @@ -198,8 +205,13 @@ function getMinifyOpts(argv) {
delete options["out-file"];
delete options.outFile;
delete options.outDir;
delete options.sourceType;
delete options["source-type"];
delete options.comments;

const babelOptions = pick(inputOpts, ["sourceType", "comments"]);

return options;
return { options, babelOptions };
}

function validate(opts) {
Expand All @@ -220,30 +232,30 @@ function validate(opts) {
);
}

function runStdin(argv, options) {
function runStdin(argv, options, babelOptions) {
if (argv._.length > 0) {
throw new Error("Reading input from STDIN. Cannot take file params");
}

return handleStdin(argv.outFile, options);
return handleStdin(argv.outFile, options, babelOptions);
}

function runFile(argv, options) {
function runFile(argv, options, babelOptions) {
const file = argv._[0];

// prefer outFile
if (argv.outFile) {
return handleFile(file, argv.outFile, options);
return handleFile(file, argv.outFile, options, babelOptions);
} else if (argv.outDir) {
return handleArgs([file], argv.outDir, options);
return handleArgs([file], argv.outDir, options, babelOptions);
} else {
// prints to STDOUT
return handleFile(file, void 0, options);
return handleFile(file, void 0, options, babelOptions);
}
}

function runArgs(argv, options) {
return handleArgs(argv._, argv.outDir, options);
function runArgs(argv, options, babelOptions) {
return handleArgs(argv._, argv.outDir, options, babelOptions);
}

async function run(args) {
Expand All @@ -253,18 +265,18 @@ async function run(args) {
if (argv.help) printHelpInfo();
if (argv.V) log(version);

const options = getMinifyOpts(argv);
const { options, babelOptions } = getMinifyOpts(argv);

if (argv._.length <= 0) {
if (!process.stdin.isTTY) {
return runStdin(argv, options);
return runStdin(argv, options, babelOptions);
} else {
return printHelpInfo({ exitCode: 1 });
}
} else if (argv._.length === 1 && (await isFile(argv._[0]))) {
return runFile(argv, options);
return runFile(argv, options, babelOptions);
} else {
return runArgs(argv, options);
return runArgs(argv, options, babelOptions);
}
}

Expand Down
24 changes: 13 additions & 11 deletions packages/babel-minify/src/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@ async function readStdin() {
});
}

async function handleStdin(outputFilename, options) {
const { code } = minify(await readStdin(), options);
async function handleStdin(outputFilename, options, babelOptions) {
const { code } = minify(await readStdin(), options, babelOptions);
if (outputFilename) {
await writeFile(outputFilename, code);
} else {
process.stdout.write(code);
}
}

async function handleFile(filename, outputFilename, options) {
const { code } = minify(await readFile(filename), options);
async function handleFile(filename, outputFilename, options, babelOptions) {
const { code } = minify(await readFile(filename), options, babelOptions);
if (outputFilename) {
await writeFile(outputFilename, code);
} else {
process.stdout.write(code);
}
}

async function handleFiles(files, outputDir, options) {
async function handleFiles(files, outputDir, options, babelOptions) {
if (!outputDir) {
throw new TypeError(`outputDir is falsy. Got "${outputDir}"`);
}
Expand All @@ -87,13 +87,13 @@ async function handleFiles(files, outputDir, options) {
files.map(file => {
const outputFilename = path.join(outputDir, path.basename(file));
return mkdirp(path.dirname(outputFilename))
.then(() => handleFile(file, outputFilename, options))
.then(() => handleFile(file, outputFilename, options, babelOptions))
.catch(e => Promise.reject(new MinifyFileError(e.message, { file })));
})
);
}

async function handleDir(dir, outputDir, options) {
async function handleDir(dir, outputDir, options, babelOptions) {
if (!outputDir) {
throw new TypeError(`outputDir is falsy`);
}
Expand All @@ -107,7 +107,9 @@ async function handleDir(dir, outputDir, options) {
const inputFilename = path.join(dir, file);

return mkdirp(path.dirname(outputFilename))
.then(() => handleFile(inputFilename, outputFilename, options))
.then(() =>
handleFile(inputFilename, outputFilename, options, babelOptions)
)
.catch(e =>
Promise.reject(
new MinifyFileError(e.message, { file: inputFilename })
Expand All @@ -117,7 +119,7 @@ async function handleDir(dir, outputDir, options) {
);
}

async function handleArgs(args, outputDir, options) {
async function handleArgs(args, outputDir, options, babelOptions) {
const files = [];
const dirs = [];

Expand All @@ -136,8 +138,8 @@ async function handleArgs(args, outputDir, options) {
}

return Promise.all([
handleFiles(files, outputDir, options),
...dirs.map(dir => handleDir(dir, outputDir, options))
handleFiles(files, outputDir, options, babelOptions),
...dirs.map(dir => handleDir(dir, outputDir, options, babelOptions))
]);
}

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4465,6 +4465,10 @@ lodash.mapvalues@^4.4.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.mapvalues/-/lodash.mapvalues-4.6.0.tgz#1bafa5005de9dd6f4f26668c30ca37230cc9689c"

lodash.pick@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"

lodash.some@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d"
Expand Down