-
Notifications
You must be signed in to change notification settings - Fork 23
/
tree_shaking_test.js
275 lines (245 loc) · 7.37 KB
/
tree_shaking_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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"use strict";
var path = require("path");
var assert = require("assert");
var keys = require("lodash/keys");
var denodeify = require("pdenodeify");
var testHelpers = require("./helpers");
var build = require("../index").build;
var optimize = require("../index").optimize;
var intersection = require("lodash/intersection");
var escapeRegExp = require("lodash/escapeRegExp");
var fs = require("fs-extra");
var rimraf = require("rimraf");
var rmdir = denodeify(rimraf);
var readFile = denodeify(fs.readFile);
describe("Tree-shaking", function(){
var app, browser;
var open = testHelpers.popen;
var find = testHelpers.pfind;
function buildAndOpen(opts){
var options = Object.assign(opts || {}, {
quiet: true,
minify: false
});
return function(done){
this.timeout(20000);
var base = path.join(__dirname, "treeshake", "basics");
var config = { config: path.join(base, "package.json!npm") };
var page = `prod.html`;
rmdir(path.join(base, "dist"))
.then(function() {
return build(config, options);
})
.then(function() {
var close;
return open(path.join("test", "treeshake", "basics", page))
.then(function(args) {
close = args.close;
browser = args.browser;
return find(browser, "app");
})
.then(function(mod) {
app = mod;
close();
done();
});
})
.catch(err => {
console.error(err);
done(err);
});
}
}
describe("Defaults", function(){
before(buildAndOpen());
describe("Import/Export syntaxes", function(){
describe("import {}", function(){
it("An unused export is removed", function(){
let dep = app.dep;
assert.equal(typeof dep.one, "function", "The 'one' export is used");
assert.equal(typeof dep.two, "undefined", "The 'two' export was treeshaken");
});
it("Uses multiple shakes to remove all unused exports", function(){
let o = app.dep4Other;
assert.equal(typeof o.first, "function", "included");
assert.equal(typeof o.second, "undefined", "removed");
let a = app.dep4AndAnother;
assert.equal(typeof a.anotherOne, "function", "included");
assert.equal(typeof a.anotherTwo, "undefined", "removed");
});
});
describe("import default", function(){
it("Includes the binding", function(){
assert.equal(app.anon(), "default export", "Includes default exports");
});
it("Includes CommonJS modules", function(){
assert.equal(app.dep5.doStuff(), "worked", "CommonJS module left alone.");
});
});
describe("import 'mod'", function(){
it("Includes modules imported for side-effects", function(){
assert.equal(browser.window.DEP3_SIDE_EFFECT, true,
"Includes an ES module with needed side effects.");
assert.equal(browser.window.DEP3_SIDE_EFFECT2, true,
"Includes a global module with needed side effects.");
});
it("Works when the dependency is CSS", function(){
var mod = browser.window.steal.loader.get("treeshake@1.0.0#styles.css!treeshake@1.0.0#css");
assert.ok(mod, true, "Includes a CSS module");
});
})
describe("export *", function(){
it("Includes the exports that are used", function(){
assert.equal(app.fromExports.rexpOne, "one", "Included the used export");
});
it("Tree-shakes exports that are not used", function(){
assert.equal(app.fromExports.rexpTwo, undefined, "Doesn\'t include unused export.");
});
})
});
describe("Bundles", function(){
it("Are not tree-shaken", function(){
let b = app.bundleA;
assert.equal(b.one(), 1, "This export is still there");
assert.equal(b.two(), 2, "This one too");
});
});
describe("Unused packages", function(){
it("Get pruned from the build", function(){
let connect = app.canConnect;
assert.equal(connect.didFail, true, "marked as failed");
});
});
describe("ES modules imported by CommonJS modules", function() {
it("Tree-shakes their ES module dependencies", function() {
let mod = app.dep5dep5AnotherESModule;
assert.equal(typeof mod.mainThing, "function", "Kept the used export");
assert.equal(typeof mod.another, "undefined", "Removed the unused export");
});
});
});
describe("treeShaking: false", function(){
describe("as a BuildOption", function(){
before(buildAndOpen({
treeShaking: false
}));
it("Doesn\'t tree shake modules", function(){
let dep = app.dep;
assert.equal(typeof dep.one, "function", "Included");
assert.equal(typeof dep.two, "function", "Included");
});
});
describe("in the package.json", function(){
var app;
before(function(done){
this.timeout(20000);
var base = path.join(__dirname, "treeshake", "disabled");
var config = { config: path.join(base, "package.json!npm") };
var page = `prod.html`;
rmdir(path.join(base, "dist"))
.then(function() {
return build(config, {
quiet: true,
minify: false
});
})
.then(function() {
var close;
return open(path.join("test", "treeshake", "disabled", page))
.then(function(args) {
close = args.close;
browser = args.browser;
return find(browser, "app");
})
.then(function(mod) {
app = mod;
close();
done();
});
})
.catch(done);
});
it("Doesn\'t tree shake modules", function(){
let dep = app.dep;
assert.equal(typeof dep.one, "function", "Included");
assert.equal(typeof dep.two, "function", "Included");
});
});
describe("With a multi-main project", function(){
var app;
before(function(done){
this.timeout(20000);
var base = path.join(__dirname, "treeshake", "multimain");
var config = {
config: path.join(base, "package.json!npm"),
main: [
"~/main1",
"~/main2"
]
};
var page = `prod1.html`;
rmdir(path.join(base, "dist"))
.then(function() {
return optimize(config, {
quiet: true,
minify: false,
treeShaking: false
});
})
.then(function() {
var close;
return open(path.join("test", "treeshake", "multimain", page))
.then(function(args) {
close = args.close;
browser = args.browser;
return find(browser, "globals");
})
.then(function(mod) {
app = mod;
close();
done();
});
})
.catch(done);
});
it("Doesn\'t tree shake modules", function(){
let Component = app.Component;
assert.equal(typeof Component, "function", "was not tree-shaken");
});
});
});
describe("Bundles", function() {
before(function(done){
this.timeout(20000);
var base = path.join(__dirname, "treeshake", "bundle");
var config = { config: path.join(base, "package.json!npm") };
var page = `prod.html`;
rmdir(path.join(base, "dist"))
.then(function() {
return build(config, {
quiet: true,
minify: false
});
})
.then(function() {
var close;
return open(path.join("test", "treeshake", "bundle", page))
.then(function(args) {
close = args.close;
browser = args.browser;
return find(browser, "APP");
})
.then(function(mod) {
app = mod;
close();
done();
});
})
.catch(done);
});
it("Items from both are included in the build", function(){
assert.equal(app.a, "a", "main bundle loaded");
assert.equal(app.b, "b", "this bundle loaded");
});
})
});