1 |
|
2 | import fs from 'fs';
|
3 | import glob from 'glob';
|
4 | import sinon from 'sinon';
|
5 | import test from 'ava';
|
6 | import WebpackParallelUglifyPlugin from '../index';
|
7 |
|
8 | let sandbox;
|
9 |
|
10 | test.beforeEach(() => {
|
11 | sandbox = sinon.sandbox.create();
|
12 | });
|
13 |
|
14 | test.afterEach(() => {
|
15 | sandbox.restore();
|
16 | });
|
17 |
|
18 | test('creating a WebpackParallelUglifyPlugin instance w/ both uglify options throws', (t) => {
|
19 | t.throws(() => {
|
20 | new WebpackParallelUglifyPlugin({
|
21 | uglifyJS: {},
|
22 | uglifyES: {},
|
23 | });
|
24 | });
|
25 | });
|
26 |
|
27 | test('creating a WebpackParallelUglifyPlugin instance with uglify.sourceMap throws', (t) => {
|
28 | t.throws(() => {
|
29 | new WebpackParallelUglifyPlugin({
|
30 | uglifyJS: { sourceMap: true },
|
31 | });
|
32 | });
|
33 |
|
34 | t.throws(() => {
|
35 | new WebpackParallelUglifyPlugin({
|
36 | uglifyES: { sourceMap: true },
|
37 | });
|
38 | });
|
39 | });
|
40 |
|
41 | test('providing no uglify options defaults to uglifyJS: {}', (t) => {
|
42 | const plugin = new WebpackParallelUglifyPlugin({});
|
43 | t.deepEqual(plugin.options, { uglifyJS: {} });
|
44 | });
|
45 |
|
46 | function FakeCompiler() {
|
47 | const callbacks = {};
|
48 | this.assets = [];
|
49 |
|
50 | this.plugin = (event, callback) => {
|
51 | callbacks[event] = callback;
|
52 | };
|
53 |
|
54 | this.fireEvent = (event, ...args) => {
|
55 | callbacks[event].apply(this, args);
|
56 | };
|
57 | }
|
58 |
|
59 | test('deleting unused cache files after all asset optimizations', (t) => {
|
60 | const originalRead = fs.readFileSync;
|
61 | sandbox.stub(fs, 'unlinkSync');
|
62 | sandbox.stub(fs, 'writeFileSync');
|
63 | sandbox.stub(fs, 'mkdirSync');
|
64 | sandbox.stub(fs, 'readFileSync', (filePath, encoding) => (
|
65 | filePath.match(/fake_cache_dir/)
|
66 | ? 'filecontents'
|
67 | : originalRead(filePath, encoding)
|
68 | ));
|
69 |
|
70 | sandbox.stub(glob, 'sync').returns(
|
71 | [
|
72 | '/fake_cache_dir/file1.js',
|
73 | '/fake_cache_dir/file2.js',
|
74 | ]
|
75 | );
|
76 |
|
77 | const uglifyPlugin = new WebpackParallelUglifyPlugin({
|
78 | uglifyJS: {},
|
79 | cacheDir: '/fake_cache_dir/',
|
80 | });
|
81 |
|
82 | const compiler = new FakeCompiler();
|
83 | uglifyPlugin.apply(compiler);
|
84 | compiler.fireEvent('compilation', compiler);
|
85 | compiler.fireEvent('optimize-chunk-assets', null, () => {});
|
86 | compiler.fireEvent('optimize-chunk-assets', null, () => {});
|
87 | t.is(fs.unlinkSync.callCount, 0, 'Cache should not be cleared by optimize-chunk-assets');
|
88 |
|
89 | compiler.fireEvent('done');
|
90 | t.deepEqual(
|
91 | fs.unlinkSync.args,
|
92 | [['/fake_cache_dir/file1.js'], ['/fake_cache_dir/file2.js']],
|
93 | 'Unused cache files should be removed after compilation'
|
94 | );
|
95 | });
|