forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.test.js
More file actions
146 lines (143 loc) · 5.27 KB
/
Compiler.test.js
File metadata and controls
146 lines (143 loc) · 5.27 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
var should = require("should");
var path = require("path");
var NodeEnvironmentPlugin = require("../lib/node/NodeEnvironmentPlugin");
var Compiler = require("../lib/Compiler");
var WebpackOptionsApply = require("../lib/WebpackOptionsApply");
var WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
var FunctionModuleTemplate = require("../lib/FunctionModuleTemplate");
describe("Compiler", function() {
function compile(entry, options, callback) {
new WebpackOptionsDefaulter().process(options);
options.entry = entry;
options.context = path.join(__dirname, "fixtures");
options.output.pathinfo = true;
var c = new Compiler();
c.options = new WebpackOptionsApply().process(options, c);
new NodeEnvironmentPlugin().apply(c);
var files = {};
c.outputFileSystem = {
join: path.join.bind(path),
mkdirp: function(path, callback) {
callback();
},
writeFile: function(name, content, callback) {
files[name] = content.toString("utf-8");
callback();
}
};
c.moduleTemplate = new FunctionModuleTemplate({ pathinfo: true }, {
shorten: function(p) {
var fixDir = path.join(__dirname, "fixtures");
if(p.indexOf(fixDir) == 0) p = "FIXDIR" + p.substr(fixDir.length);
return p.replace(/\\/g, "/");
}
});
c.plugin("compilation", function(compilation) {
compilation.bail = true;
});
c.run(function(err, stats) {
if(err) throw err;
should.strictEqual(typeof stats, "object");
stats = stats.toJson({
modules: true,
reasons: true
});
should.strictEqual(typeof stats, "object");
stats.should.have.property("errors");
Array.isArray(stats.errors).should.be.ok;
if(stats.errors.length > 0) {
stats.errors[0].should.be.instanceOf(Error);
throw stats.errors[0];
}
callback(stats, files);
});
}
it("should compile a single file", function(done) {
compile("./c", {}, function(stats, files) {
files.should.have.property("bundle.js").have.type("string");
Object.keys(files).should.be.eql(["bundle.js"]);
var bundle = files["bundle.js"];
bundle.should.include("function require(");
bundle.should.include("require(/*! ./a */ 1);");
bundle.should.include("FIXDIR/c.js");
bundle.should.include("FIXDIR/a.js");
bundle.should.include("This is a");
bundle.should.include("This is c");
bundle.should.not.include("2: function(");
bundle.should.not.include("window");
bundle.should.not.include("jsonp");
bundle.should.not.include("fixtures");
done();
});
});
it("should compile a complex file", function(done) {
compile("./main1", {}, function(stats, files) {
files.should.have.property("bundle.js").have.type("string");
Object.keys(files).should.be.eql(["bundle.js"]);
var bundle = files["bundle.js"];
bundle.should.include("function require(");
bundle.should.include("require(/*! ./a */");
bundle.should.include("FIXDIR/main1.js");
bundle.should.include("FIXDIR/a.js");
bundle.should.include("FIXDIR/b.js");
bundle.should.include("FIXDIR/node_modules/m1/a.js");
bundle.should.include("This is a");
bundle.should.include("This is b");
bundle.should.include("This is m1/a");
bundle.should.not.include("4: function(");
bundle.should.not.include("window");
bundle.should.not.include("jsonp");
bundle.should.not.include("fixtures");
done();
});
});
it("should compile a file with transitive dependencies", function(done) {
compile("./abc", {}, function(stats, files) {
files.should.have.property("bundle.js").have.type("string");
Object.keys(files).should.be.eql(["bundle.js"]);
var bundle = files["bundle.js"];
bundle.should.include("function require(");
bundle.should.include("require(/*! ./a */");
bundle.should.include("require(/*! ./b */");
bundle.should.include("require(/*! ./c */");
bundle.should.include("FIXDIR/abc.js");
bundle.should.include("FIXDIR/a.js");
bundle.should.include("FIXDIR/b.js");
bundle.should.include("FIXDIR/c.js");
bundle.should.include("This is a");
bundle.should.include("This is b");
bundle.should.include("This is c");
bundle.should.not.include("4: function(");
bundle.should.not.include("window");
bundle.should.not.include("jsonp");
bundle.should.not.include("fixtures");
done();
});
});
it("should compile a file with multiple chunks", function(done) {
compile("./chunks", {}, function(stats, files) {
stats.chunks.length.should.be.eql(2);
files.should.have.property("bundle.js").have.type("string");
files.should.have.property("1.bundle.js").have.type("string");
Object.keys(files).should.be.eql(["bundle.js", "1.bundle.js"]);
var bundle = files["bundle.js"];
var chunk = files["1.bundle.js"];
bundle.should.include("function require(");
bundle.should.include("require(/*! ./b */");
chunk.should.not.include("require(/* ./b */");
bundle.should.include("FIXDIR/chunks.js");
chunk.should.include("FIXDIR/a.js");
chunk.should.include("FIXDIR/b.js");
chunk.should.include("This is a");
bundle.should.not.include("This is a");
chunk.should.include("This is b");
bundle.should.not.include("This is b");
bundle.should.not.include("4: function(");
bundle.should.not.include("fixtures");
chunk.should.not.include("fixtures");
bundle.should.include("webpackJsonp");
chunk.should.include("webpackJsonp(");
done();
});
});
});