Skip to content

Commit eafad1f

Browse files
authored
test: use asset modules (#3771)
1 parent b80610f commit eafad1f

File tree

6 files changed

+146
-33
lines changed

6 files changed

+146
-33
lines changed

examples/default/webpack.config.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
"use strict";
22

3+
const { version } = require("webpack");
34
// our setup function adds behind-the-scenes bits to the config that all of our
45
// examples need
56
const { setup } = require("../util");
67

8+
let moduleRuleForPNG = {};
9+
10+
if (version.startsWith("5")) {
11+
moduleRuleForPNG = {
12+
test: /\.png$/,
13+
type: "asset/resource",
14+
generator: {
15+
filename: "images/[hash][ext][query]",
16+
},
17+
};
18+
} else {
19+
moduleRuleForPNG = {
20+
test: /\.png$/,
21+
loader: "file-loader",
22+
options: { prefix: "img/" },
23+
};
24+
}
25+
726
module.exports = setup({
827
context: __dirname,
928
entry: "./app.js",
@@ -14,9 +33,7 @@ module.exports = setup({
1433
use: ["style-loader", "css-loader", "less-loader"],
1534
},
1635
{
17-
test: /\.png$/,
18-
loader: "file-loader",
19-
options: { prefix: "img/" },
36+
...moduleRuleForPNG,
2037
},
2138
],
2239
},

examples/general/config-array/webpack.config.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
"use strict";
22

3+
const { version } = require("webpack");
34
// our setup function adds behind-the-scenes bits to the config that all of our
45
// examples need
56
const { setup } = require("../../util");
67

8+
const isWebpack5 = version.startsWith("5");
9+
10+
let moduleRuleForPNG = {};
11+
12+
if (isWebpack5) {
13+
moduleRuleForPNG = {
14+
test: /\.png$/,
15+
type: "asset/resource",
16+
generator: {
17+
filename: "images/[hash][ext][query]",
18+
},
19+
};
20+
} else {
21+
moduleRuleForPNG = {
22+
test: /\.png$/,
23+
loader: "file-loader",
24+
options: { prefix: "img/" },
25+
};
26+
}
27+
728
module.exports = [
829
setup({
930
context: __dirname,
@@ -15,9 +36,7 @@ module.exports = [
1536
use: ["style-loader", "css-loader", "less-loader"],
1637
},
1738
{
18-
test: /\.png$/,
19-
loader: "file-loader",
20-
options: { prefix: "img/" },
39+
...moduleRuleForPNG,
2140
},
2241
],
2342
},
@@ -34,11 +53,19 @@ module.exports = [
3453
test: /\.less$/,
3554
use: ["style-loader", "css-loader", "less-loader"],
3655
},
37-
{
38-
test: /\.png$/,
39-
loader: "url-loader",
40-
options: { limit: 100000 },
41-
},
56+
isWebpack5
57+
? {
58+
test: /\.png$/,
59+
type: "asset/resource",
60+
generator: {
61+
filename: "images/[hash][ext][query]",
62+
},
63+
}
64+
: {
65+
test: /\.png$/,
66+
loader: "url-loader",
67+
options: { limit: 100000 },
68+
},
4269
],
4370
},
4471
optimization: {

test/fixtures/historyapifallback-3-config/webpack.config.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
"use strict";
22

3+
const { version } = require("webpack");
4+
5+
let moduleRuleForHTML = {};
6+
7+
if (version.startsWith("5")) {
8+
moduleRuleForHTML = {
9+
test: /\.html$/,
10+
type: "asset/resource",
11+
generator: {
12+
filename: "index.html",
13+
},
14+
};
15+
} else {
16+
moduleRuleForHTML = {
17+
test: /\.html$/,
18+
loader: "file-loader",
19+
options: { name: "index.html" },
20+
};
21+
}
22+
323
module.exports = {
424
mode: "development",
525
context: __dirname,
@@ -11,9 +31,7 @@ module.exports = {
1131
module: {
1232
rules: [
1333
{
14-
test: /\.html$/,
15-
loader: "file-loader",
16-
options: { name: "index.html" },
34+
...moduleRuleForHTML,
1735
},
1836
],
1937
},

test/fixtures/historyapifallback-config/webpack.config.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
"use strict";
22

3+
const { version } = require("webpack");
4+
5+
let moduleRuleForHTML = {};
6+
7+
if (version.startsWith("5")) {
8+
moduleRuleForHTML = {
9+
test: /\.html$/,
10+
type: "asset/resource",
11+
generator: {
12+
filename: "[name][ext]",
13+
},
14+
};
15+
} else {
16+
moduleRuleForHTML = {
17+
test: /\.html$/,
18+
loader: "file-loader",
19+
options: { name: "[name].[ext]" },
20+
};
21+
}
22+
323
module.exports = {
424
mode: "development",
525
context: __dirname,
@@ -11,9 +31,7 @@ module.exports = {
1131
module: {
1232
rules: [
1333
{
14-
test: /\.html$/,
15-
loader: "file-loader",
16-
options: { name: "[name].[ext]" },
34+
...moduleRuleForHTML,
1735
},
1836
],
1937
},

test/fixtures/mime-types-config/webpack.config.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
"use strict";
22

3+
const { version } = require("webpack");
4+
5+
let moduleRuleForCustom = {};
6+
7+
if (version.startsWith("5")) {
8+
moduleRuleForCustom = {
9+
test: /\.custom$/,
10+
type: "asset/resource",
11+
generator: {
12+
filename: "[name][ext]",
13+
},
14+
};
15+
} else {
16+
moduleRuleForCustom = {
17+
test: /\.custom$/,
18+
use: [
19+
{
20+
loader: "file-loader",
21+
options: {
22+
name() {
23+
return "[name].[ext]";
24+
},
25+
},
26+
},
27+
],
28+
};
29+
}
30+
331
module.exports = {
432
mode: "development",
533
context: __dirname,
@@ -15,17 +43,7 @@ module.exports = {
1543
module: {
1644
rules: [
1745
{
18-
test: /\.custom$/,
19-
use: [
20-
{
21-
loader: "file-loader",
22-
options: {
23-
name() {
24-
return "[name].[ext]";
25-
},
26-
},
27-
},
28-
],
46+
...moduleRuleForCustom,
2947
},
3048
],
3149
},

test/fixtures/multi-public-path-config/webpack.config.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
"use strict";
22

33
const path = require("path");
4+
const { version } = require("webpack");
5+
6+
let moduleRuleForHTML = {};
7+
8+
if (version.startsWith("5")) {
9+
moduleRuleForHTML = {
10+
test: /\.html$/,
11+
type: "asset/resource",
12+
generator: {
13+
filename: "path/to/file.html",
14+
},
15+
};
16+
} else {
17+
moduleRuleForHTML = {
18+
test: /\.html$/,
19+
loader: "file-loader",
20+
options: { name: "path/to/file.html" },
21+
};
22+
}
423

524
module.exports = [
625
{
@@ -19,11 +38,7 @@ module.exports = [
1938
module: {
2039
rules: [
2140
{
22-
test: /\.html$/,
23-
loader: "file-loader",
24-
options: {
25-
name: "path/to/file.html",
26-
},
41+
...moduleRuleForHTML,
2742
},
2843
],
2944
},

0 commit comments

Comments
 (0)