From 585bcb2dceb32da43a03c0345a142dbff400513f Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sat, 4 Sep 2021 22:16:18 +0200 Subject: [PATCH] Add --minify option to "build" command --- .changeset/gentle-phones-brush.md | 5 ++ packages/wmr/src/cli.js | 3 +- packages/wmr/src/lib/normalize-options.js | 1 - packages/wmr/test/cli.test.js | 77 +++++++++++++++++++ .../wmr/test/fixtures/cli-minify/index.html | 2 + .../wmr/test/fixtures/cli-minify/index.js | 7 ++ 6 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 .changeset/gentle-phones-brush.md create mode 100644 packages/wmr/test/cli.test.js create mode 100644 packages/wmr/test/fixtures/cli-minify/index.html create mode 100644 packages/wmr/test/fixtures/cli-minify/index.js diff --git a/.changeset/gentle-phones-brush.md b/.changeset/gentle-phones-brush.md new file mode 100644 index 000000000..7e62f719d --- /dev/null +++ b/.changeset/gentle-phones-brush.md @@ -0,0 +1,5 @@ +--- +'wmr': minor +--- + +Add `--minify` option to the `build` command. That way one can disable minifcation for production builds by passing `--no-minify` or `--minify false` diff --git a/packages/wmr/src/cli.js b/packages/wmr/src/cli.js index 0f36f0930..99f6504f9 100644 --- a/packages/wmr/src/cli.js +++ b/packages/wmr/src/cli.js @@ -30,8 +30,9 @@ prog .option('--prerender', 'Pre-render the application to HTML') .option('--sourcemap', 'Enable Source Maps') .option('--visualize', 'Launch interactive bundle visualizer') + .option('--minify', 'Enable minification of generated code (default: true)', true) .action(opts => { - opts.minify = opts.minify !== false && !/false|0/.test(opts.minify); + opts.minify = bool(opts.minify); run(build(opts)); }); diff --git a/packages/wmr/src/lib/normalize-options.js b/packages/wmr/src/lib/normalize-options.js index 48dfd27bd..a27e767c0 100644 --- a/packages/wmr/src/lib/normalize-options.js +++ b/packages/wmr/src/lib/normalize-options.js @@ -21,7 +21,6 @@ export async function normalizeOptions(options, mode, configWatchFiles = []) { options.root = options.cwd; - options.minify = mode === 'build'; options.plugins = []; options.output = []; options.middleware = []; diff --git a/packages/wmr/test/cli.test.js b/packages/wmr/test/cli.test.js new file mode 100644 index 000000000..2ab4c9e85 --- /dev/null +++ b/packages/wmr/test/cli.test.js @@ -0,0 +1,77 @@ +import { loadFixture, runWmr, setupTest, teardown, withLog } from './test-helpers.js'; +import { promises as fs } from 'fs'; +import path from 'path'; + +jest.setTimeout(30000); + +describe('CLI', () => { + /** @type {TestEnv} */ + let env; + /** @type {WmrInstance} */ + let instance; + + beforeEach(async () => { + env = await setupTest(); + }); + + afterEach(async () => { + await teardown(env); + instance.close(); + }); + + describe('--minify', () => { + async function runTestCase() { + expect(await instance.done).toEqual(0); + + const dist = path.join(env.tmp.path, 'dist'); + const files = await fs.readdir(dist); + + const js = path.join( + dist, + files.find(x => x.endsWith('.js')) + ); + const script = await fs.readFile(js, 'utf-8'); + return script; + } + + it('should be enabled by default', async () => { + await loadFixture('cli-minify', env); + instance = await runWmr(env.tmp.path, 'build'); + + await withLog(instance.output, async () => { + const script = await runTestCase(); + expect(script).not.toMatch(/foo\.bar/); + }); + }); + + it('should be enabled via --minify', async () => { + await loadFixture('cli-minify', env); + instance = await runWmr(env.tmp.path, 'build', '--minify'); + + await withLog(instance.output, async () => { + const script = await runTestCase(); + expect(script).not.toMatch(/foo\.bar/); + }); + }); + + it('should be disabled via --minify false', async () => { + await loadFixture('cli-minify', env); + instance = await runWmr(env.tmp.path, 'build', '--minify', 'false'); + + await withLog(instance.output, async () => { + const script = await runTestCase(); + expect(script).toMatch(/foo\.bar/); + }); + }); + + it('should be disabled via --no-minify', async () => { + await loadFixture('cli-minify', env); + instance = await runWmr(env.tmp.path, 'build', '--no-minify'); + + await withLog(instance.output, async () => { + const script = await runTestCase(); + expect(script).toMatch(/foo\.bar/); + }); + }); + }); +}); diff --git a/packages/wmr/test/fixtures/cli-minify/index.html b/packages/wmr/test/fixtures/cli-minify/index.html new file mode 100644 index 000000000..fdfc78447 --- /dev/null +++ b/packages/wmr/test/fixtures/cli-minify/index.html @@ -0,0 +1,2 @@ +

check js

+ diff --git a/packages/wmr/test/fixtures/cli-minify/index.js b/packages/wmr/test/fixtures/cli-minify/index.js new file mode 100644 index 000000000..022248337 --- /dev/null +++ b/packages/wmr/test/fixtures/cli-minify/index.js @@ -0,0 +1,7 @@ +const foo = {}; + +Object.defineProperty(foo, 'bar', { + value: 42 +}); + +console.log(foo.bar);