webpack/test/Compiler.test.js

492 lines
13 KiB
JavaScript
Raw Normal View History

2017-02-02 19:56:41 +08:00
/* globals describe, it */
2017-01-18 16:03:37 +08:00
"use strict";
2013-01-31 01:49:25 +08:00
2017-01-18 16:03:37 +08:00
const path = require("path");
2013-01-31 01:49:25 +08:00
2017-01-18 16:03:37 +08:00
const webpack = require("../");
const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
const MemoryFs = require("memory-fs");
2017-01-18 16:03:37 +08:00
describe("Compiler", () => {
2013-01-31 01:49:25 +08:00
function compile(entry, options, callback) {
2017-01-18 16:03:37 +08:00
const noOutputPath = !options.output || !options.output.path;
2018-02-25 18:46:17 +08:00
if (!options.mode) options.mode = "production";
options = new WebpackOptionsDefaulter().process(options);
2013-01-31 01:49:25 +08:00
options.entry = entry;
options.context = path.join(__dirname, "fixtures");
2018-02-25 18:46:17 +08:00
if (noOutputPath) options.output.path = "/";
2013-01-31 01:49:25 +08:00
options.output.pathinfo = true;
2017-12-13 23:05:21 +08:00
options.optimization = {
minimize: false
};
2017-01-18 16:03:37 +08:00
const logs = {
mkdirp: [],
2018-02-25 18:46:17 +08:00
writeFile: []
};
2013-01-31 01:49:25 +08:00
2017-01-18 16:03:37 +08:00
const c = webpack(options);
const files = {};
2013-01-31 01:49:25 +08:00
c.outputFileSystem = {
2018-01-24 23:00:43 +08:00
join() {
2017-02-09 19:57:09 +08:00
return [].join.call(arguments, "/").replace(/\/+/g, "/");
},
2018-01-24 23:00:43 +08:00
mkdirp(path, callback) {
logs.mkdirp.push(path);
2013-01-31 01:49:25 +08:00
callback();
},
2018-01-24 23:00:43 +08:00
writeFile(name, content, callback) {
logs.writeFile.push(name, content);
2013-01-31 01:49:25 +08:00
files[name] = content.toString("utf-8");
callback();
}
};
2018-02-25 18:46:17 +08:00
c.hooks.compilation.tap(
"CompilerTest",
compilation => (compilation.bail = true)
);
2017-01-18 16:03:37 +08:00
c.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) throw err;
2018-01-24 23:00:43 +08:00
expect(typeof stats).toBe("object");
2017-01-18 16:03:37 +08:00
const compilation = stats.compilation;
2013-01-31 01:49:25 +08:00
stats = stats.toJson({
modules: true,
reasons: true
});
2018-01-24 23:00:43 +08:00
expect(typeof stats).toBe("object");
expect(stats).toHaveProperty("errors");
expect(Array.isArray(stats.errors)).toBe(true);
2018-02-25 18:46:17 +08:00
if (stats.errors.length > 0) {
2018-01-24 23:00:43 +08:00
expect(stats.errors[0]).toBeInstanceOf(Error);
2013-01-31 01:49:25 +08:00
throw stats.errors[0];
}
stats.logs = logs;
callback(stats, files, compilation);
2013-01-31 01:49:25 +08:00
});
}
2018-02-25 18:46:17 +08:00
it("should compile a single file to deep output", done => {
compile(
"./c",
{
output: {
path: "/what",
filename: "the/hell.js"
}
},
(stats, files) => {
expect(stats.logs.mkdirp).toEqual(["/what", "/what/the"]);
done();
}
2018-02-25 18:46:17 +08:00
);
});
2018-02-25 18:46:17 +08:00
it("should compile a single file", done => {
2017-01-18 16:03:37 +08:00
compile("./c", {}, (stats, files) => {
2018-01-24 23:00:43 +08:00
expect(Object.keys(files)).toEqual(["/main.js"]);
const bundle = files["/main.js"];
2018-01-24 23:00:43 +08:00
expect(bundle).toMatch("function __webpack_require__(");
expect(bundle).toMatch(/__webpack_require__\(\/\*! \.\/a \*\/ \d\);/);
2018-01-24 23:00:43 +08:00
expect(bundle).toMatch("./c.js");
expect(bundle).toMatch("./a.js");
expect(bundle).toMatch("This is a");
expect(bundle).toMatch("This is c");
expect(bundle).not.toMatch("2: function(");
expect(bundle).not.toMatch("window");
expect(bundle).not.toMatch("jsonp");
expect(bundle).not.toMatch("fixtures");
2013-01-31 01:49:25 +08:00
done();
});
});
2018-02-25 18:46:17 +08:00
it("should compile a complex file", done => {
2017-01-18 16:03:37 +08:00
compile("./main1", {}, (stats, files) => {
2018-01-24 23:00:43 +08:00
expect(Object.keys(files)).toEqual(["/main.js"]);
const bundle = files["/main.js"];
2018-01-24 23:00:43 +08:00
expect(bundle).toMatch("function __webpack_require__(");
expect(bundle).toMatch("__webpack_require__(/*! ./a */");
expect(bundle).toMatch("./main1.js");
expect(bundle).toMatch("./a.js");
expect(bundle).toMatch("./b.js");
expect(bundle).toMatch("./node_modules/m1/a.js");
expect(bundle).toMatch("This is a");
expect(bundle).toMatch("This is b");
expect(bundle).toMatch("This is m1/a");
expect(bundle).not.toMatch("4: function(");
expect(bundle).not.toMatch("window");
expect(bundle).not.toMatch("jsonp");
expect(bundle).not.toMatch("fixtures");
2013-01-31 01:49:25 +08:00
done();
});
});
2018-02-25 18:46:17 +08:00
it("should compile a file with transitive dependencies", done => {
2017-01-18 16:03:37 +08:00
compile("./abc", {}, (stats, files) => {
2018-01-24 23:00:43 +08:00
expect(Object.keys(files)).toEqual(["/main.js"]);
const bundle = files["/main.js"];
2018-01-24 23:00:43 +08:00
expect(bundle).toMatch("function __webpack_require__(");
expect(bundle).toMatch("__webpack_require__(/*! ./a */");
expect(bundle).toMatch("__webpack_require__(/*! ./b */");
expect(bundle).toMatch("__webpack_require__(/*! ./c */");
expect(bundle).toMatch("./abc.js");
expect(bundle).toMatch("./a.js");
expect(bundle).toMatch("./b.js");
expect(bundle).toMatch("./c.js");
expect(bundle).toMatch("This is a");
expect(bundle).toMatch("This is b");
expect(bundle).toMatch("This is c");
expect(bundle).not.toMatch("4: function(");
expect(bundle).not.toMatch("window");
expect(bundle).not.toMatch("jsonp");
expect(bundle).not.toMatch("fixtures");
2013-01-31 01:49:25 +08:00
done();
});
});
2018-02-25 18:46:17 +08:00
it("should compile a file with multiple chunks", done => {
2017-01-18 16:03:37 +08:00
compile("./chunks", {}, (stats, files) => {
2018-01-24 23:00:43 +08:00
expect(stats.chunks).toHaveLength(2);
expect(Object.keys(files)).toEqual(["/main.js", "/1.js"]);
const bundle = files["/main.js"];
const chunk = files["/1.js"];
2018-01-24 23:00:43 +08:00
expect(bundle).toMatch("function __webpack_require__(");
expect(bundle).toMatch("__webpack_require__(/*! ./b */");
expect(chunk).not.toMatch("__webpack_require__(/* ./b */");
expect(bundle).toMatch("./chunks.js");
expect(chunk).toMatch("./a.js");
expect(chunk).toMatch("./b.js");
expect(chunk).toMatch("This is a");
expect(bundle).not.toMatch("This is a");
expect(chunk).toMatch("This is b");
expect(bundle).not.toMatch("This is b");
expect(bundle).not.toMatch("4: function(");
expect(bundle).not.toMatch("fixtures");
expect(chunk).not.toMatch("fixtures");
expect(bundle).toMatch("webpackJsonp");
2018-02-25 18:46:17 +08:00
expect(chunk).toMatch('window["webpackJsonp"] || []).push');
2013-01-31 01:49:25 +08:00
done();
});
});
2017-06-24 04:30:30 +08:00
describe("methods", () => {
let compiler;
beforeEach(() => {
compiler = webpack({
entry: "./c",
context: path.join(__dirname, "fixtures"),
output: {
path: "/",
2018-02-25 18:46:17 +08:00
pathinfo: true
2017-06-24 04:30:30 +08:00
}
});
});
describe("purgeInputFileSystem", () => {
2018-02-25 18:46:17 +08:00
it("invokes purge() if inputFileSystem.purge", done => {
2018-05-07 21:26:04 +08:00
const mockPurge = jest.fn();
2017-06-24 04:30:30 +08:00
compiler.inputFileSystem = {
2018-02-25 18:46:17 +08:00
purge: mockPurge
2017-06-24 04:30:30 +08:00
};
compiler.purgeInputFileSystem();
2018-05-07 21:26:04 +08:00
expect(mockPurge.mock.calls.length).toBe(1);
2017-06-24 04:30:30 +08:00
done();
});
2018-02-25 18:46:17 +08:00
it("does NOT invoke purge() if !inputFileSystem.purge", done => {
2018-05-07 21:26:04 +08:00
const mockPurge = jest.fn();
2017-06-24 04:30:30 +08:00
compiler.inputFileSystem = null;
compiler.purgeInputFileSystem();
2018-05-07 21:26:04 +08:00
expect(mockPurge.mock.calls.length).toBe(0);
2017-06-24 04:30:30 +08:00
done();
});
});
describe("isChild", () => {
2018-02-25 18:46:17 +08:00
it("returns booleanized this.parentCompilation", done => {
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = "stringyStringString";
const response1 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response1).toBe(true);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = 123456789;
const response2 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response2).toBe(true);
2017-06-24 04:30:30 +08:00
2017-06-24 04:45:59 +08:00
compiler.parentCompilation = {
what: "I belong to an object"
};
2017-06-24 04:30:30 +08:00
const response3 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response3).toBe(true);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = ["Array", 123, true, null, [], () => {}];
const response4 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response4).toBe(true);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = false;
const response5 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response5).toBe(false);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = 0;
const response6 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response6).toBe(false);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = null;
const response7 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response7).toBe(false);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = "";
const response8 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response8).toBe(false);
2017-06-24 04:30:30 +08:00
compiler.parentCompilation = NaN;
const response9 = compiler.isChild();
2018-01-24 23:00:43 +08:00
expect(response9).toBe(false);
2017-06-24 04:30:30 +08:00
done();
});
});
});
2018-02-25 18:46:17 +08:00
it("should not emit on errors", done => {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./missing",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
compiler.run((err, stats) => {
2018-02-25 18:46:17 +08:00
if (err) return done(err);
if (compiler.outputFileSystem.existsSync("/bundle.js"))
return done(new Error("Bundle should not be created on error"));
done();
});
});
2018-02-25 18:46:17 +08:00
it("should not emit on errors (watch)", done => {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./missing",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
const watching = compiler.watch({}, (err, stats) => {
watching.close();
2018-02-25 18:46:17 +08:00
if (err) return done(err);
if (compiler.outputFileSystem.existsSync("/bundle.js"))
return done(new Error("Bundle should not be created on error"));
done();
});
});
2018-03-07 06:31:14 +08:00
it("should not be run twice at a time (run)", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
compiler.run((err, stats) => {
if (err) return done(err);
});
compiler.run((err, stats) => {
if (err) return done();
});
});
2018-03-07 06:31:14 +08:00
it("should not be run twice at a time (watch)", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
compiler.watch({}, (err, stats) => {
if (err) return done();
});
2018-03-07 06:31:14 +08:00
});
it("should not be run twice at a time (run - watch)", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
compiler.run((err, stats) => {
if (err) return done(err);
});
2018-03-07 06:31:14 +08:00
compiler.watch({}, (err, stats) => {
if (err) return done();
});
});
2018-03-07 06:31:14 +08:00
it("should not be run twice at a time (watch - run)", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
2018-03-07 06:31:14 +08:00
compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
2018-03-07 06:31:14 +08:00
compiler.run((err, stats) => {
if (err) return done();
});
});
2018-03-07 06:31:14 +08:00
it("should not be run twice at a time (instance cb)", function(done) {
const compiler = webpack(
{
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
},
() => {}
);
compiler.outputFileSystem = new MemoryFs();
2018-03-07 06:31:14 +08:00
compiler.run((err, stats) => {
if (err) return done();
});
});
2018-03-07 06:31:14 +08:00
it("should run again correctly after first compilation", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
2018-03-07 06:31:14 +08:00
compiler.run((err, stats) => {
if (err) return done(err);
2018-03-07 06:31:14 +08:00
compiler.run((err, stats) => {
if (err) return done(err);
done();
});
});
});
2018-03-07 06:31:14 +08:00
it("should watch again correctly after first compilation", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
2018-03-07 06:31:14 +08:00
compiler.run((err, stats) => {
if (err) return done(err);
2018-03-07 06:31:14 +08:00
compiler.watch({}, (err, stats) => {
if (err) return done(err);
done();
});
});
});
2018-03-07 06:31:14 +08:00
it("should run again correctly after first closed watch", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
2018-03-07 06:31:14 +08:00
const watching = compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
watching.close(() => {
compiler.run((err, stats) => {
if (err) return done(err);
done();
});
});
});
2018-03-07 17:16:40 +08:00
it("should watch again correctly after first closed watch", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
const watching = compiler.watch({}, (err, stats) => {
if (err) return done(err);
});
watching.close(() => {
compiler.watch({}, (err, stats) => {
if (err) return done(err);
done();
});
});
});
2018-10-23 07:12:42 +08:00
it("should flag watchMode as true in watch", function(done) {
const compiler = webpack({
context: __dirname,
mode: "production",
entry: "./c",
output: {
path: "/",
filename: "bundle.js"
}
});
compiler.outputFileSystem = new MemoryFs();
2018-10-23 07:12:42 +08:00
compiler.watch({}, err => {
if (err) return done(err);
2018-10-23 07:12:42 +08:00
expect(compiler.watchMode).toBeTruthy();
done();
});
2018-10-23 07:12:42 +08:00
});
2018-06-20 03:41:23 +08:00
it("should use cache on second run call", function(done) {
const compiler = webpack({
context: __dirname,
mode: "development",
devtool: false,
entry: "./fixtures/count-loader!./fixtures/count-loader",
output: {
path: "/"
}
});
compiler.outputFileSystem = new MemoryFs();
compiler.run(() => {
compiler.run(() => {
const result = compiler.outputFileSystem.readFileSync(
"/main.js",
"utf-8"
);
expect(result).toContain("module.exports = 0;");
done();
});
});
2018-06-20 16:23:26 +08:00
});
});