webpack/test/RuleSet.unittest.js

484 lines
10 KiB
JavaScript
Raw Normal View History

"use strict";
2016-09-14 18:04:42 +08:00
2017-01-18 22:59:46 +08:00
const RuleSet = require("../lib/RuleSet");
2016-09-14 18:04:42 +08:00
function match(ruleSet, resource) {
2017-01-18 22:59:46 +08:00
const result = ruleSet.exec({
2016-09-14 18:04:42 +08:00
resource: resource
});
2018-02-25 18:46:17 +08:00
return result
.filter(r => {
return r.type === "use";
})
.map(r => r.value)
.map(r => {
if (!r.options) return r.loader;
if (typeof r.options === "string") return r.loader + "?" + r.options;
return r.loader + "?" + JSON.stringify(r.options);
});
}
2016-09-14 18:04:42 +08:00
2017-01-18 22:59:46 +08:00
describe("RuleSet", () => {
it("should create RuleSet with a blank array", () => {
const loader = new RuleSet([]);
2018-01-24 20:17:21 +08:00
expect(loader.rules).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should create RuleSet and match with empty array", () => {
const loader = new RuleSet([]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "something")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should not match with loaders array", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "something")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match with regex", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match with string", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: "style.css",
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match with function", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: function(str) {
return str === "style.css";
},
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if invalid test", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: {
invalid: "test"
},
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/Unexcepted property invalid in condition/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should accept multiple test array that all match", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: [/style.css/, /yle.css/],
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should accept multiple test array that not all match", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: [/style.css/, /something.css/],
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should not match if include does not match", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
include: /output.css/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match if include matches", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
include: /style.css/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should not match if exclude matches", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
exclude: /style.css/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual([]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should match if exclude does not match", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
exclude: /output.css/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work if a loader is applied to all files", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
expect(match(loader, "scripts.js")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader as string", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader as array", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: ["css"]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loaders as string", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: "css"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loaders as array", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: ["css"]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if using loaders with non-string or array", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: {
someObj: true
}
2016-09-14 18:04:42 +08:00
}
2018-02-25 18:46:17 +08:00
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/No loader specified/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader with inline query", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css?modules=1"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader with string query", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css",
query: "modules=1"
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using loader with object query", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css",
query: {
modules: 1
}
2016-09-14 18:04:42 +08:00
}
2018-02-25 18:46:17 +08:00
]);
expect(match(loader, "style.css")).toEqual(['css?{"modules":1}']);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using array loaders with basic object notation", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: [
{
loader: "css"
}
]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if using array loaders with object notation without specifying a loader", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: [
{
stuff: 1
}
]
}
]);
match(loader, "style.css");
2018-01-24 20:17:21 +08:00
}).toThrow(/No loader specified/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using array loaders with object notation", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: [
{
loader: "css",
query: "modules=1"
}
]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["css?modules=1"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using multiple array loaders with object notation", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: [
{
loader: "style",
query: "filesize=1000"
},
{
loader: "css",
query: "modules=1"
}
]
}
]);
expect(match(loader, "style.css")).toEqual([
"style?filesize=1000",
"css?modules=1"
]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work with using string multiple loaders", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: "style?filesize=1000!css?modules=1"
}
]);
expect(match(loader, "style.css")).toEqual([
"style?filesize=1000",
"css?modules=1"
]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should throw if using array loaders with a single legacy", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: ["style-loader", "css-loader"],
query: "modules=1"
}
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/options\/query cannot be used with loaders/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work when using array loaders", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: ["style-loader", "css-loader"]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
it("should work when using an array of functions returning a loader", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
use: [
function(data) {
return {
loader: "style-loader"
};
},
function(data) {
return {
loader: "css-loader"
};
}
]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
});
2017-01-18 22:59:46 +08:00
it("should work when using an array of either functions or strings returning a loader", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
use: [
"style-loader",
function(data) {
return {
loader: "css-loader"
};
}
]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
});
2017-01-18 22:59:46 +08:00
2018-02-26 10:35:40 +08:00
it("should work when using an array of functions returning either a loader object or loader name string", () => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
use: [
function(data) {
return "style-loader";
},
function(data) {
return {
loader: "css-loader"
};
}
]
}
]);
2018-01-24 20:17:21 +08:00
expect(match(loader, "style.css")).toEqual(["style-loader", "css-loader"]);
});
2017-01-18 22:59:46 +08:00
it("should throw if using array loaders with invalid type", () => {
2018-01-24 20:17:21 +08:00
expect(() => {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loaders: ["style-loader", "css-loader", 5]
}
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
}).toThrow(/No loader specified/);
2016-09-14 18:04:42 +08:00
});
2017-01-18 22:59:46 +08:00
describe("when exclude array holds an undefined item", () => {
function errorHasContext(err) {
2018-02-25 18:46:17 +08:00
return (
/Expected condition but got falsy value/.test(err) &&
/test/.test(err) &&
/include/.test(err) &&
/exclude/.test(err) &&
/node_modules/.test(err) &&
2018-02-25 18:46:17 +08:00
/undefined/.test(err)
);
}
2017-01-18 22:59:46 +08:00
it("should throw with context", () => {
2018-01-24 20:17:21 +08:00
try {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
test: /\.css$/,
loader: "css",
include: ["src"],
exclude: ["node_modules", undefined]
}
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
throw new Error("unreachable");
2018-02-25 18:46:17 +08:00
} catch (e) {
2018-01-24 20:17:21 +08:00
expect(errorHasContext(e.message)).toBe(true);
}
});
2017-01-18 22:59:46 +08:00
it("in resource should throw with context", () => {
2018-01-24 20:17:21 +08:00
try {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
resource: {
test: /\.css$/,
include: ["src"],
exclude: ["node_modules", undefined]
}
}
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
throw new Error("unreachable");
2018-02-25 18:46:17 +08:00
} catch (e) {
2018-01-24 20:17:21 +08:00
expect(errorHasContext(e.message)).toBe(true);
}
});
2017-01-18 22:59:46 +08:00
it("in issuer should throw with context", () => {
2018-01-24 20:17:21 +08:00
try {
2018-02-25 18:46:17 +08:00
const loader = new RuleSet([
{
issuer: {
test: /\.css$/,
include: ["src"],
exclude: ["node_modules", undefined]
}
}
]);
2018-01-24 20:17:21 +08:00
match(loader, "style.css");
throw new Error("unreachable");
2018-02-25 18:46:17 +08:00
} catch (e) {
2018-01-24 20:17:21 +08:00
expect(errorHasContext(e.message)).toBe(true);
}
});
});
2016-09-14 18:04:42 +08:00
});