From a28b6b000a05934864db9aea70cef671ea79b167 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Tue, 22 May 2018 16:36:19 +0300 Subject: [PATCH] feat(cli): add sourceType and comments (#861) --- .../__tests__/__snapshots__/cli-tests.js.snap | 18 ++++++++ packages/babel-minify/__tests__/cli-tests.js | 14 +++++++ .../__tests__/fixtures/module/mod.js | 16 +++++++ packages/babel-minify/package.json | 1 + packages/babel-minify/src/cli.js | 42 ++++++++++++------- packages/babel-minify/src/fs.js | 24 ++++++----- yarn.lock | 4 ++ 7 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 packages/babel-minify/__tests__/fixtures/module/mod.js diff --git a/packages/babel-minify/__tests__/__snapshots__/cli-tests.js.snap b/packages/babel-minify/__tests__/__snapshots__/cli-tests.js.snap index 5eb1e2ce4..b49da4593 100644 --- a/packages/babel-minify/__tests__/__snapshots__/cli-tests.js.snap +++ b/packages/babel-minify/__tests__/__snapshots__/cli-tests.js.snap @@ -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, diff --git a/packages/babel-minify/__tests__/cli-tests.js b/packages/babel-minify/__tests__/cli-tests.js index 0c29e20b5..0c3e83f96 100644 --- a/packages/babel-minify/__tests__/cli-tests.js +++ b/packages/babel-minify/__tests__/cli-tests.js @@ -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"); @@ -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(); + }); }); diff --git a/packages/babel-minify/__tests__/fixtures/module/mod.js b/packages/babel-minify/__tests__/fixtures/module/mod.js new file mode 100644 index 000000000..316a344d1 --- /dev/null +++ b/packages/babel-minify/__tests__/fixtures/module/mod.js @@ -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); diff --git a/packages/babel-minify/package.json b/packages/babel-minify/package.json index fe16f81b2..3c7c3aae1 100644 --- a/packages/babel-minify/package.json +++ b/packages/babel-minify/package.json @@ -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" diff --git a/packages/babel-minify/src/cli.js b/packages/babel-minify/src/cli.js index 9d18dddcf..0b3e50887 100644 --- a/packages/babel-minify/src/cli.js +++ b/packages/babel-minify/src/cli.js @@ -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", @@ -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", @@ -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 @@ -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) { @@ -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) { @@ -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); } } diff --git a/packages/babel-minify/src/fs.js b/packages/babel-minify/src/fs.js index 97a70bbf0..dc3ba4d91 100644 --- a/packages/babel-minify/src/fs.js +++ b/packages/babel-minify/src/fs.js @@ -60,8 +60,8 @@ 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 { @@ -69,8 +69,8 @@ async function handleStdin(outputFilename, options) { } } -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 { @@ -78,7 +78,7 @@ async function handleFile(filename, outputFilename, options) { } } -async function handleFiles(files, outputDir, options) { +async function handleFiles(files, outputDir, options, babelOptions) { if (!outputDir) { throw new TypeError(`outputDir is falsy. Got "${outputDir}"`); } @@ -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`); } @@ -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 }) @@ -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 = []; @@ -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)) ]); } diff --git a/yarn.lock b/yarn.lock index a2fcbaa14..b6bb6686e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"