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