Skip to content

Commit cc725f8

Browse files
authoredFeb 3, 2021
Merge ac7eb60 into fdb5e85
2 parents fdb5e85 + ac7eb60 commit cc725f8

File tree

5 files changed

+1884
-52
lines changed

5 files changed

+1884
-52
lines changed
 

‎README.md

+37-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ module.exports = {
5050
};
5151
```
5252

53+
**Only for webpack v4:**
54+
5355
Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
5456

5557
And run `webpack` via your preferred method.
@@ -1134,6 +1136,30 @@ module.exports = {
11341136

11351137
The following `webpack.config.js` can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as [Data URLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.
11361138

1139+
**For webpack v5:**
1140+
1141+
**webpack.config.js**
1142+
1143+
```js
1144+
module.exports = {
1145+
module: {
1146+
rules: [
1147+
{
1148+
test: /\.css$/i,
1149+
use: ["style-loader", "css-loader"],
1150+
},
1151+
{
1152+
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1153+
// More information here https://webpack.js.org/guides/asset-modules/
1154+
type: "asset",
1155+
},
1156+
],
1157+
},
1158+
};
1159+
```
1160+
1161+
**For webpack v4:**
1162+
11371163
**webpack.config.js**
11381164

11391165
```js
@@ -1187,8 +1213,6 @@ module.exports = {
11871213
// Run `postcss-loader` on each CSS `@import`, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
11881214
// If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
11891215
importLoaders: 1,
1190-
// Automatically enable css modules for files satisfying `/\.module\.\w+$/i` RegExp.
1191-
modules: { auto: true },
11921216
},
11931217
},
11941218
{
@@ -1201,13 +1225,20 @@ module.exports = {
12011225
},
12021226
],
12031227
},
1228+
// For webpack v5
12041229
{
12051230
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1206-
loader: "url-loader",
1207-
options: {
1208-
limit: 8192,
1209-
},
1231+
// More information here https://webpack.js.org/guides/asset-modules/
1232+
type: "asset",
12101233
},
1234+
// For webpack v4
1235+
// {
1236+
// test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1237+
// loader: "url-loader",
1238+
// options: {
1239+
// limit: 8192,
1240+
// },
1241+
// },
12111242
],
12121243
},
12131244
};

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"stylus": "^0.54.8",
9393
"stylus-loader": "^4.3.0",
9494
"url-loader": "^4.1.1",
95-
"webpack": "^5.4.0"
95+
"webpack": "^5.20.1"
9696
},
9797
"keywords": [
9898
"webpack",

‎test/__snapshots__/url-option.test.js.snap

+1,703-40
Large diffs are not rendered by default.

‎test/fixtures/url/url.css

+8-5
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ a {
306306
.class.class.class {
307307
background: url('./img\
308308
(img.png');
309-
background: url('./img\(img.png');
310-
background: url('./img\
309+
background: url('./img\
310+
(img.png');
311+
background: url('./img\
311312
(img.png');
312313
background: url('./img\
313314
\
@@ -349,9 +350,11 @@ a {
349350
background: url(./img\'\28%29\ img.png);
350351
}
351352

352-
.qqq {
353-
background: url('!!../../helpers/url-loader.js?esModule=false!~package/img.png')
354-
}
353+
/* TODO need refactor tests after drop webpack@4 due inline syntax `!../../helpers/url-loader.js?esModule=false!~package/img.png` */
354+
/* We need to use `resourceQuery: /inline/` */
355+
/*.qqq {*/
356+
/* background: url('!!../../helpers/url-loader.js?esModule=false!~package/img.png')*/
357+
/*}*/
355358

356359
.class {
357360
/* Should be one import */

‎test/url-option.test.js

+135
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs from "fs";
22
import path from "path";
33

4+
import webpack from "webpack";
5+
46
import {
57
compile,
68
getCompiler,
@@ -10,6 +12,8 @@ import {
1012
getWarnings,
1113
} from "./helpers/index";
1214

15+
const isWebpack5 = webpack.version.startsWith(5);
16+
1317
describe('"url" option', () => {
1418
it("should work when not specified", async () => {
1519
const compiler = getCompiler("./url/url.js");
@@ -140,6 +144,137 @@ describe('"url" option', () => {
140144
expect(getErrors(stats)).toMatchSnapshot("errors");
141145
});
142146

147+
it("should work with the 'asset' type of asset modules", async () => {
148+
const compiler = getCompiler(
149+
"./url/url.js",
150+
{},
151+
{
152+
module: {
153+
rules: [
154+
{
155+
test: /\.css$/i,
156+
use: [
157+
{
158+
loader: path.resolve(__dirname, "../src"),
159+
options: {},
160+
},
161+
],
162+
},
163+
isWebpack5
164+
? {
165+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
166+
type: "asset",
167+
generator: {
168+
filename: "[name][ext]",
169+
},
170+
}
171+
: {
172+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
173+
loader: "url-loader",
174+
options: {
175+
limit: 8096,
176+
name: "[name].[ext]",
177+
},
178+
},
179+
],
180+
},
181+
}
182+
);
183+
const stats = await compile(compiler);
184+
185+
expect(getModuleSource("./url/url.css", stats)).toMatchSnapshot("module");
186+
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
187+
"result"
188+
);
189+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
190+
expect(getErrors(stats)).toMatchSnapshot("errors");
191+
});
192+
193+
it("should work with the 'asset/resource' type of asset modules", async () => {
194+
const compiler = getCompiler(
195+
"./url/url.js",
196+
{},
197+
{
198+
module: {
199+
rules: [
200+
{
201+
test: /\.css$/i,
202+
use: [
203+
{
204+
loader: path.resolve(__dirname, "../src"),
205+
options: {},
206+
},
207+
],
208+
},
209+
isWebpack5
210+
? {
211+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
212+
type: "asset",
213+
generator: {
214+
filename: "[name][ext]",
215+
},
216+
}
217+
: {
218+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
219+
loader: "url-loader",
220+
options: {
221+
limit: 8096,
222+
name: "[name].[ext]",
223+
},
224+
},
225+
],
226+
},
227+
}
228+
);
229+
const stats = await compile(compiler);
230+
231+
expect(getModuleSource("./url/url.css", stats)).toMatchSnapshot("module");
232+
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
233+
"result"
234+
);
235+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
236+
expect(getErrors(stats)).toMatchSnapshot("errors");
237+
});
238+
239+
it("should work with the 'asset/inline' type of asset modules", async () => {
240+
const compiler = getCompiler(
241+
"./url/url.js",
242+
{},
243+
{
244+
module: {
245+
rules: [
246+
{
247+
test: /\.css$/i,
248+
use: [
249+
{
250+
loader: path.resolve(__dirname, "../src"),
251+
options: {},
252+
},
253+
],
254+
},
255+
isWebpack5
256+
? {
257+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
258+
type: "asset/inline",
259+
}
260+
: {
261+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
262+
loader: "url-loader",
263+
},
264+
],
265+
},
266+
}
267+
);
268+
const stats = await compile(compiler);
269+
270+
expect(getModuleSource("./url/url.css", stats)).toMatchSnapshot("module");
271+
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
272+
"result"
273+
);
274+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
275+
expect(getErrors(stats)).toMatchSnapshot("errors");
276+
});
277+
143278
it("should throw an error on unresolved import", async () => {
144279
const compiler = getCompiler("./url/url-unresolved.js");
145280
const stats = await compile(compiler);

0 commit comments

Comments
 (0)