forked from systemjs/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-build-cache.js
260 lines (218 loc) · 8.23 KB
/
test-build-cache.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var Builder = require('../index');
var expect = require('chai').expect;
var toFileURL = require('../lib/utils.js').toFileURL;
var fs = require('fs');
suite('Test compiler cache', function() {
var builder = new Builder('test/fixtures/test-cache-tree');
builder.config({ transpiler: 'babel' });
test('Use compile cache entry when available', function() {
var loadName = 'simple.js';
var outputPath = 'test/output/cached.js';
var cacheObj;
var tree;
return builder.trace(loadName).then(function(_tree) {
tree = _tree;
return builder.bundle(tree);
})
.then(function() {
var cacheEntry = builder.getCache();
expect(cacheEntry).to.be.an('object');
cacheObj = cacheEntry.compile.loads['simple.js'];
expect(cacheObj).to.be.an('object');
expect(cacheObj.hash).to.be.a('string');
expect(cacheObj.output).to.be.an('object');
// poison cache
cacheObj.output.source = cacheObj.output.source.replace('hate', 'love');
return builder.bundle(tree);
})
.then(function(output) {
// verify buildTree use poisoned cache rather than recompiling
var outputSource = output.source;
expect(outputSource).not.to.contain('hate caches');
expect(outputSource).to.contain('love caches');
// invalidate poisoned cache entry and rebuild
cacheObj.hash = 'out of date';
return builder.bundle(tree);
})
.then(function(output) {
// verify original source is used once more
var outputSource = output.source;
expect(outputSource).to.contain('hate caches');
expect(outputSource).not.to.contain('love caches');
});
});
test('Use trace cache when available', function() {
// construct the load record for the cache
var cacheObj = {
trace: {
'simple.js': {
name: 'simple.js',
path: 'fixtures/test-cache-tree/simple.js',
metadata: {
deps: [],
format: 'amd',
isAnon: true
},
deps: [],
depMap: {},
source: 'define([], function(module) {\n console.log(\'fake cache\');\n});\n',
originalSource: 'define([], function(module) {\n console.log(\'fake cache\');\n});\n'
}
}
};
builder.reset();
builder.setCache(cacheObj);
return builder.bundle('simple.js').then(function(output) {
expect(output.source).to.contain('fake cache');
});
});
test('Cache invalidation', function() {
var cacheObj = {
trace: {
'simple.js': {},
'another/path.js': {}
}
};
builder.reset();
builder.setCache(cacheObj);
var invalidated = builder.invalidate('*');
assert.deepEqual(invalidated, [builder.loader.normalizeSync('simple.js'), builder.loader.normalizeSync('another/path.js')]);
cacheObj = {
trace: {
'simple.js': {},
'new/path.js': {},
'deep/wildcard/test.js': {}
}
};
builder.setCache(cacheObj);
invalidated = builder.invalidate('new/path.js');
assert.deepEqual(invalidated, [builder.loader.normalizeSync('new/path.js')]);
invalidated = builder.invalidate('deep/*.js');
assert.deepEqual(invalidated, [builder.loader.normalizeSync('deep/wildcard/test.js')]);
});
test('builder.loader.fetch sets load.metadata.timestamp', function() {
var source = 'export var p = 5;';
var builder = new Builder('test/output');
fs.writeFileSync('./test/output/timestamp-module.js', source);
var address = builder.loader.normalizeSync('./test/output/timestamp-module.js');
var load = { name: address, address: address, metadata: {} };
return builder.loader.fetch(load)
.then(function(text) {
//console.log(JSON.stringify(load));
assert(text == source); // true
assert(load.metadata.deps); // true
assert(load.metadata.timestamp); // false
})
});
test('Bundle example', function() {
var builder = new Builder('test/output');
fs.writeFileSync('./test/output/dynamic-module.js', 'export var p = 5;');
return builder.bundle('dynamic-module.js')
.then(function(output) {
assert(output.source.match(/p = 5/));
fs.writeFileSync('./test/output/dynamic-module.js', 'export var p = 6;');
builder.invalidate('dynamic-module.js');
return builder.bundle('dynamic-module.js');
})
.then(function(output) {
assert(output.source.match(/p = 6/));
});
});
test('Bundle example with imported file', function() {
var builder = new Builder('test/output');
fs.writeFileSync('./test/output/dynamic-import.js', [
'const d = 9;',
'export default d;'
].join('\n'));
fs.writeFileSync('./test/output/dynamic-main.js', [
'import d from "./dynamic-import.js";',
'console.log(d);'
].join('\n'));
return builder.bundle('dynamic-main.js')
.then(function(output) {
assert(output.source.match(/d = 9/));
assert(output.source.match(/console/));
fs.writeFileSync('./test/output/dynamic-import.js', [
'import "./dynamic-import2.js";', // Add another transitive dependency.
'const d = 7;',
'export default d;'
].join('\n'));
builder.invalidate('dynamic-import.js');
fs.writeFileSync('./test/output/dynamic-import2.js', [
'const u = "transitive";',
'export default u;'
].join('\n'));
return builder.bundle('dynamic-main.js');
})
.then(function(output) {
assert(output.source.match(/transitive/));
assert(output.source.match(/d = 7/));
assert(output.source.match(/console/));
// Remove the transitive dependency from the build.
fs.writeFileSync('./test/output/dynamic-import.js', [
'const d = 7;',
'export default d;'
].join('\n'));
builder.invalidate('dynamic-import.js');
return builder.bundle('dynamic-main.js');
})
.then(function(output) {
assert(!output.source.match(/transitive/));
});
});
test('Static build example statting check', function() {
var builder = new Builder('test/output');
fs.writeFileSync('./test/output/static-main.js', "import { testThing } from './static-test-module.js'; testThing();");
fs.writeFileSync('./test/output/static-test-module.js', "export function testThing() { console.log('test'); }");
return builder.buildStatic('static-main.js')
.then(function() {
builder.invalidate('static-main.js');
// despite removing the file, it remains cached
fs.unlinkSync('./test/output/static-test-module.js');
return builder.buildStatic('static-main.js');
});
});
test('Static build example dependency reload check', function() {
var builder = new Builder('test/output');
fs.writeFileSync('./test/output/static-main.js', "import { testThing } from './static-test-module.js'; testThing();");
fs.writeFileSync('./test/output/static-test-module.js', "export function testThing() { console.log('test'); }");
return builder.buildStatic('static-main.js')
.then(function() {
fs.writeFileSync('./test/output/static-test-module.js', "export function testThing() { console.log('new test'); }")
builder.invalidate('static-test-module.js');
return builder.buildStatic('static-main.js');
});
});
test('Static build, fetch override', function () {
var builder = new Builder('test/fixtures/test-tree');
return builder.buildStatic('foo.js', {
fetch: function (load, fetch) {
if (load.name.indexOf('foo.js') !== -1) {
return fs.readFileSync('test/fixtures/test-tree/cjs.js', 'utf8');
} else {
return fetch(load);
}
}
});
});
test('Static build, fetch override with callback', function () {
var builder = new Builder('test/fixtures/test-tree');
return builder.buildStatic('cjs.js', {
fetch: function (load, fetch) {
return fetch(load);
}
});
});
test('Static string build', function () {
var builder = new Builder('test/fixtures/test-tree');
return builder.bundle('foo.js', {
fetch: function (load, fetch) {
if (load.name.indexOf('foo.js') !== -1) {
return fs.readFileSync('test/fixtures/test-tree/cjs.js', 'utf8');
} else {
return fetch(load);
}
}
});
});
});