This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
forked from jsdf/only-if-changed-webpack-plugin
/
plugin-test.js
183 lines (138 loc) · 5.7 KB
/
plugin-test.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var fs = require('fs');
var test = require('tap').test;
var path = require('path');
var webpack = require('webpack');
var OnlyIfChangedPlugin = require('../');
var webpackConfig = require('../support/webpack.config.js');
var testUtils = require('../support/testUtils');
var readFile = testUtils.readFile;
var writeFile = testUtils.writeFile;
var cleanupTestOutputDir = testUtils.cleanupTestOutputDir;
var testOutputDir = testUtils.testOutputDir;
var waitForIOSettle = testUtils.waitForIOSettle;
var entry = path.join(testOutputDir, 'entry.js');
test('it rebuilds when input files change', (t) => {
var bundleFile1 = 'bundle-1.js';
var dependency1 = path.join(testOutputDir, 'stuff.js');
var dependency2 = path.join(testOutputDir, 'stuff2.js');
var inputContent1 = 'console.log("123")';
var inputContent2 = 'console.log("567")';
cleanupTestOutputDir((cleanupErr) => {
t.notOk(cleanupErr, 'clean up test output dir');
writeFile(entry, `require('${dependency1}');`);
writeFile(dependency1, inputContent1);
writeFile(dependency2, inputContent2);
var bundleOutputFile = path.join(testOutputDir, bundleFile1);
doBuild(bundleFile1, (build1Err) => {
t.notOk(build1Err, 'built first time');
var output1 = readFile(bundleOutputFile);
t.match(output1, inputContent1, 'built bundle containing initial content');
var mtime1 = fs.statSync(bundleOutputFile).mtime.getTime();
writeFile(entry, `require('${dependency2}');`);
doBuild(bundleFile1, build2err => {
t.notOk(build2err, 'built second time');
var mtime2 = fs.statSync(bundleOutputFile).mtime.getTime();
t.ok(mtime1 !== mtime2, 'does rebuild files');
var output2 = readFile(bundleOutputFile);
t.match(output2, inputContent2, 'built bundle containing changed content');
t.end();
});
});
});
});
test('it rebuilds when output files change', (t) => {
var bundleFile1 = 'bundle-1.js';
var dependency1 = path.join(testOutputDir, 'stuff.js');
var inputContent1 = 'console.log("123")';
cleanupTestOutputDir((cleanupErr) => {
t.notOk(cleanupErr, 'clean up test output dir');
writeFile(entry, `require('${dependency1}');`);
writeFile(dependency1, inputContent1);
var bundleOutputFile = path.join(testOutputDir, bundleFile1);
doBuild(bundleFile1, (build1Err) => {
t.notOk(build1Err, 'built first time');
var output1 = readFile(bundleOutputFile);
t.match(output1, inputContent1, 'built bundle containing initial content');
fs.unlinkSync(bundleOutputFile);
doBuild(bundleFile1, build2Err => {
t.notOk(build2Err, 'built second time');
t.ok(fs.existsSync(bundleOutputFile), 'does rebuild files');
t.end();
});
});
});
});
test('when input and output files do not change', (t) => {
var test = t.test;
test('it does not rebuild', (t) => {
var bundleFile1 = 'bundle-1.js';
var dependency1 = path.join(testOutputDir, 'stuff.js');
var inputContent1 = 'console.log("123")';
cleanupTestOutputDir((cleanupErr) => {
t.notOk(cleanupErr, 'clean up test output dir');
writeFile(entry, `require('${dependency1}');`);
writeFile(dependency1, inputContent1);
var bundleOutputFile = path.join(testOutputDir, bundleFile1);
doBuild(bundleFile1, (build1Err) => {
t.notOk(build1Err, 'built first time');
var output1 = readFile(bundleOutputFile);
t.match(output1, inputContent1, 'built bundle containing initial content');
var mtime1 = fs.statSync(bundleOutputFile).mtime.getTime();
doBuild(bundleFile1, build2Err => {
t.notOk(build2Err, 'built second time');
var mtime2 = fs.statSync(bundleOutputFile).mtime.getTime();
t.ok(mtime1 === mtime2, 'does not rebuild files');
t.end();
});
});
});
});
test('it does not rebuild when a file has Epoch timestamp', (t) => {
// see https://github.com/npm/npm/issues/19968#issuecomment-372799983 for why we need to support this
var bundleFile1 = 'bundle-1.js';
var dependency1 = path.join(testOutputDir, 'stuff.js');
var inputContent1 = 'console.log("123")';
cleanupTestOutputDir((cleanupErr) => {
t.notOk(cleanupErr, 'clean up test output dir');
writeFile(entry, `require('${dependency1}');`);
writeFile(dependency1, inputContent1, 0); // zero timestamp
var bundleOutputFile = path.join(testOutputDir, bundleFile1);
doBuild(bundleFile1, (build1Err) => {
t.notOk(build1Err, 'built first time');
var output1 = readFile(bundleOutputFile);
t.match(output1, inputContent1, 'built bundle containing initial content');
var mtime1 = fs.statSync(bundleOutputFile).mtime.getTime();
doBuild(bundleFile1, build2Err => {
t.notOk(build2Err, 'built second time');
var mtime2 = fs.statSync(bundleOutputFile).mtime.getTime();
t.ok(mtime1 === mtime2, 'does not rebuild files');
t.end();
});
});
});
});
t.end();
});
function doBuild(filename, done) {
webpackConfig.entry = entry;
webpackConfig.output.filename = filename;
webpackConfig.plugins = [
new OnlyIfChangedPlugin({
cacheDirectory: testOutputDir,
cacheIdentifier: 'cache',
}),
];
webpack(webpackConfig, function(err, stats) {
if (err) return done(err);
var jsonStats = stats.toJson();
if (jsonStats.errors.length > 0) {
console.error.apply(console, jsonStats.errors);
console.error.apply(console, jsonStats.errorDetails);
return done(new Error(jsonStats.errors.join(', ')));
}
if (jsonStats.warnings.length > 0) {
console.error.apply(console, jsonStats.warnings);
}
waitForIOSettle(done);
});
}