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 --minify option to build command #842

Merged
merged 1 commit into from
Sep 4, 2021
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
5 changes: 5 additions & 0 deletions .changeset/gentle-phones-brush.md
Original file line number Diff line number Diff line change
@@ -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`
3 changes: 2 additions & 1 deletion packages/wmr/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand Down
1 change: 0 additions & 1 deletion packages/wmr/src/lib/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
77 changes: 77 additions & 0 deletions packages/wmr/test/cli.test.js
Original file line number Diff line number Diff line change
@@ -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/);
});
});
});
});
2 changes: 2 additions & 0 deletions packages/wmr/test/fixtures/cli-minify/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>check js</h1>
<script type="module" src="index.js"></script>
7 changes: 7 additions & 0 deletions packages/wmr/test/fixtures/cli-minify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const foo = {};

Object.defineProperty(foo, 'bar', {
value: 42
});

console.log(foo.bar);