forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.web.js
More file actions
446 lines (405 loc) · 14.9 KB
/
index.web.js
File metadata and controls
446 lines (405 loc) · 14.9 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Should not break it... should not include complete directory...
require = require("enhanced-require")(module);
if(typeof define != "function") var define = require.define;
function test(cond, message) {
if(!cond) throw new Error(message);
}
// load tests from library1, with script loader
require("script!../js/library1.js");
// Buildin 'style' loader adds css to document
require("../css/stylesheet.css");
require("../less/stylesheet.less");
function testCase(number) {
//window.test(require("./folder/file" + (number === 1 ? 1 : "2")) === "file" + number);
require(number === 1 ? "../folder/file1" : number === 2 ? "../folder/file2" : number === 3 ? "../folder/file3" : "./missingModule").should.be.eql("file" + number);
require(
number === 1 ? "../folder/file1" :
number === 2 ? "../folder/file2" :
number === 3 ? "../folder/file3" :
"./missingModule"
).should.be.eql("file" + number);
}
describe("main", function() {
it("should load library1 with script-loader", function() {
should.exist(window.library1);
window.library1.should.be.eql(true);
});
it("should load library1 with script-loader", function() {
should.exist(window.library2);
should.exist(window.library2.ok);
window.library2.ok.should.be.eql(true);
});
describe("resolving", function() {
it("should load index.web.js instead of index.js", function() {
true.should.be.eql(true);
});
it("should load single file modules", function() {
require("subfilemodule").should.be.eql("subfilemodule");
});
it("should load correct replacements for files", function(done) {
require.ensure(["subcontent"], function(require) {
// Comments work!
exports.ok = true;
test(require("subcontent") === "replaced", "node_modules should be replaced with web_modules");
test(require("subcontent2/file.js") === "orginal", "node_modules should still work when web_modules exists");
done();
});
});
after(function() {
should.exist(exports.ok);
exports.ok.should.be.eql(true);
});
});
describe("runtime", function() {
it("should load circular dependencies correctly", function() {
require("./circular").should.be.eql(1);
});
it("should cache modules correctly", function(done) {
require("./singluar.js").value.should.be.eql(1);
(require("./singluar.js")).value.should.be.eql(1);
require("./sing" + "luar.js").value = 2;
require("./singluar.js").value.should.be.eql(2);
require.ensure(["./two.js"], function(require) {
require("./singluar.js").value.should.be.eql(2);
done();
});
});
it("should throw an error on missing module at runtime, but not at compile time if in try block", function() {
var error = null;
try {
testCase(4); // indirect
} catch(e) {
error = e;
}
error.should.be.instanceOf(Error);
error = null;
try {
require("./missingModule2"); // direct
} catch(e) {
error = e;
}
error.should.be.instanceOf(Error);
});
it("should fire multiple code load callbacks in the correct order", function(done) {
var calls = [];
require.ensure([], function(require) {
require("./duplicate");
require("./duplicate2");
calls.push(1);
});
require.ensure([], function(require) {
require("./duplicate");
require("./duplicate2");
calls.push(2);
calls.should.be.eql([1,2]);
done();
});
});
it("should be able the remove modules from cache with require.cache and require.resolve", function() {
var singlarObj = require("./singluar2");
var singlarId = require.resolve("./singluar2");
var singlarIdInConditional = require.resolve(true ? "./singluar2" : "./singluar");
singlarId.should.be.a("number");
singlarIdInConditional.should.be.eql(singlarId);
should.exist(require.cache);
should.exist(require.cache[singlarId]);
require.cache[singlarId].should.be.a("object");
delete require.cache[singlarId];
require("./singluar2").should.be.not.equal(singlarObj);
});
});
describe("context", function() {
it("should be able to load a file with the require.context method", function() {
require.context("../templates")("./tmpl").should.be.eql("test template");
(require.context("../././templates"))("./tmpl").should.be.eql("test template");
(require.context(".././templates/.")("./tmpl")).should.be.eql("test template");
require . context ( "." + "." + "/" + "templ" + "ates" ) ( "./subdir/tmpl.js" ).should.be.eql("subdir test template");
});
it("should automatically create contexts", function() {
var template = "tmpl", templateFull = "./tmpl.js";
require("../templates/" + template).should.be.eql("test template");
require("../templates/templateLoader")(templateFull).should.be.eql("test template");
require("../templates/templateLoaderIndirect")(templateFull).should.be.eql("test template");
});
it("should also work in a chunk", function(done) {
require.ensure([], function(require) {
var contextRequire = require.context(".");
contextRequire("./two").should.be.eql(2);
var tw = "tw";
require("." + "/" + tw + "o").should.be.eql(2);
done();
});
});
it("should be able to use a context with a loader", function() {
var abc = "abc", scr = "script.coffee";
require("../resources/" + scr).should.be.eql("coffee test");
require("raw!../resources/" + abc + ".txt").should.be.eql("abc");
});
});
describe("parsing", function() {
it("should parse complex require calls", function() {
test(new(require("./constructor"))(1234).value == 1234, "Parse require in new(...) should work");
test(new ( require ( "./constructor" ) ) ( 1234 ) .value == 1234, "Parse require in new(...) should work, with spaces");
});
it("should let the user hide the require function", function() {
(function(require) { return require; }(1234)).should.be.eql(1234);
function testFunc(abc, require) {
return require;
}
testFunc(333, 678).should.be.eql(678);
(function() {
var require = 123;
require.should.be.eql(123);
}());
(function() {
var module = 1233;
module.should.be.eql(1233);
}());
});
it("should not create a context for the ?: operator", function() {
testCase(1);
testCase(2);
testCase(3);
});
it("should not create a context for typeof require", function() {
require("../folder/typeof").should.be.eql("function");
});
});
describe("polyfilling", function() {
var sum2;
before(function() {
sum2 = 0;
});
it("should polyfill process and module", function(done) {
module.id.should.be.a("number");
module.id.should.be.eql(require.resolve("./index.web.js"));
require.ensure([], function(require) {
test(process.argv && process.argv.length > 1, "process.argv should be an array");
process.nextTick(function() {
sum2++;
sum2.should.be.eql(2);
done();
});
process.on("xyz", function() {
sum2++;
});
process.emit("xyz");
test(global === window, "global === window");
});
});
});
describe("chunks", function() {
it("should handle duplicate chunks", function(done) {
var firstOne = false, secondOne = false;
require.ensure([], function(require) {
require("./acircular");
require("./duplicate");
require("./duplicate2");
firstOne = true;
if(secondOne) done();
});
require.ensure([], function(require) {
require("./acircular2");
require("./duplicate");
require("./duplicate2");
secondOne = true;
if(firstOne) done();
});
});
});
describe("loaders", function() {
it("should run a loader from package.json", function() {
require("testloader!../resources/abc.txt").should.be.eql("abcwebpack");
require("testloader/lib/loader2!../resources/abc.txt").should.be.eql("abcweb");
require("testloader/lib/loader3!../resources/abc.txt").should.be.eql("abcloader");
require("testloader/lib/loader-indirect!../resources/abc.txt").should.be.eql("abcwebpack");
});
it("should run a loader from .webpack-loader.js extension", function() {
require("testloader/lib/loader!../resources/abc.txt").should.be.eql("abcwebpack");
});
it("should be able to pipe loaders", function() {
require("testloader!../loaders/reverseloader!../resources/abc.txt").should.be.eql("cbawebpack");
});
describe("buildin", function() {
it("should handle the raw loader correctly", function() {
require("raw!../resources/abc.txt").should.be.eql("abc");
});
it("should handle the json loader correctly", function() {
require("json!../../../package.json").name.should.be.eql("webpack");
require("../../../package.json").name.should.be.eql("webpack");
});
it("should handle the jade loader correctly", function() {
require("jade!../resources/template.jade")({abc: "abc"}).should.be.eql("<p>abc</p>");
require("../resources/template.jade")({abc: "abc"}).should.be.eql("<p>abc</p>");
});
it("should handle the coffee loader correctly", function() {
require("coffee!../resources/script.coffee").should.be.eql("coffee test");
require("../resources/script.coffee").should.be.eql("coffee test");
});
it("should handle the css loader correctly", function() {
require("css!../css/stylesheet.css").indexOf(".rule-direct").should.not.be.eql(-1);
require("css!../css/stylesheet.css").indexOf(".rule-import1").should.not.be.eql(-1);
require("css!../css/stylesheet.css").indexOf(".rule-import2").should.not.be.eql(-1);
});
it("should handle the val loader (piped with css loader) correctly", function() {
require("css!val!../css/generateCss").indexOf("generated").should.not.be.eql(-1);
require("css!val!../css/generateCss").indexOf(".rule-import2").should.not.be.eql(-1);
require("raw!val!../css/generateCss").indexOf("generated").should.not.be.eql(-1);
});
it("should handle the val loader (piped with css loader) correctly", function() {
require("less!../less/stylesheet.less").indexOf(".less-rule-direct").should.not.be.eql(-1);
require("less!../less/stylesheet.less").indexOf(".less-rule-import1").should.not.be.eql(-1);
require("less!../less/stylesheet.less").indexOf(".less-rule-import2").should.not.be.eql(-1);
});
it("should handle the file loader correctly", function() {
require("file/png!../img/image.png").should.match(/^js\/.+\.png$/);
document.getElementById("image").src = require("file/png!../img/image.png");
});
});
});
describe("query", function() {
it("should make different modules for query", function() {
var a = require("./empty");
var b = require("./empty?1");
var c = require("./empty?2");
should.exist(a);
should.exist(b);
should.exist(c);
a.should.be.not.equal(b);
a.should.be.not.equal(c);
b.should.be.not.equal(c);
});
it("should pass query to loader", function() {
var result = require("../loaders/queryloader?query!./a?resourcequery");
result.should.be.eql({
resourceQuery: "?resourcequery",
query: "?query",
prev: "module.exports = \"a\";"
});
});
it("should pass query to loader without resource with resource query", function() {
var result = require("../loaders/queryloader?query!?resourcequery");
result.should.be.eql({
resourceQuery: "?resourcequery",
query: "?query",
prev: null
});
});
it("should pass query to loader without resource", function() {
var result = require("../loaders/queryloader?query!");
result.should.be.eql({
query: "?query",
prev: null
});
});
it("should pass query to multiple loaders", function() {
var result = require("../loaders/queryloader?query1!../loaders/queryloader?query2!./a?resourcequery");
result.should.be.a("object");
result.should.have.property("resourceQuery").be.eql("?resourcequery");
result.should.have.property("query").be.eql("?query1");
result.should.have.property("prev").be.eql("module.exports = " + JSON.stringify({
resourceQuery: "?resourcequery",
query: "?query2",
prev: "module.exports = \"a\";"
}));
});
it("should pass query to loader over context", function() {
var test = "test";
var result = require("../loaders/queryloader?query!../context-query-test/" + test);
result.should.be.eql({
resourceQuery: null,
query: "?query",
prev: "test content"
});
});
});
describe("AMD", function() {
it("should be able to use AMD-style require", function(done) {
var template = "tmpl";
require(["./circular", "../templates/" + template, true ? "./circular" : "./circular"], function(circular, testTemplate, circular2) {
circular.should.be.eql(1);
circular2.should.be.eql(1);
testTemplate.should.be.eql("test template");
done();
});
});
it("should be able to use require.js-style define", function(done) {
define("name", ["./circular"], function(circular) {
circular.should.be.eql(1);
done();
});
});
it("should be able to use require.js-style define, without name", function(done) {
define(["./circular"], function(circular) {
circular.should.be.eql(1);
done();
});
});
it("should be able to use require.js-style define, with empty dependencies", function(done) {
define("name", [], function() {
done();
});
});
it("should be able to use require.js-style define, without dependencies", function(done) {
define("name", function() {
done();
});
});
var obj = {};
it("should be able to use require.js-style define, with an object", function() {
define("blaaa", obj);
});
after(function() {
module.exports.should.be.equal(obj);
});
it("should offer AMD-style define for CommonJs", function(done) {
var _test_require = require.valueOf();
var _test_exports = module.exports;
var _test_module = module;
define(function(require, exports, module) {
(typeof require).should.be.eql("function");
require.valueOf().should.be.equal(_test_require);
exports.should.be.equal(_test_exports);
module.should.be.equal(_test_module);
require("./circular").should.be.eql(1);
done();
});
});
it("should not crash on require.js require only with array", function() {
require(["./circular"]);
});
it("should create a chunk for require.js require", function(done) {
var sameTick = true;
require(["./c"], function(c) {
sameTick.should.be.eql(false);
c.should.be.eql("c");
require("./d").should.be.eql("d");
done();
});
sameTick = false;
});
});
describe("cross module system", function() {
it("should answer typeof require correctly", function() {
(typeof require).should.be.eql("function");
});
it("should answer typeof define correctly", function() {
(typeof define).should.be.eql("function");
});
it("should answer typeof require.amd correctly", function() {
(typeof require.amd).should.be.eql("object");
});
it("should answer typeof define.amd correctly", function() {
(typeof define.amd).should.be.eql("object");
});
it("should answer typeof module correctly", function() {
(typeof module).should.be.eql("object");
});
it("should answer typeof exports correctly", function() {
(typeof exports).should.be.eql("object");
});
});
describe("node.js tests", function() {
require("../nodetests");
});
});