Skip to content

Commit e69e670

Browse files
test: more
1 parent 74cac3e commit e69e670

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

test/__snapshots__/loader.test.js.snap

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,177 @@ You may need an appropriate loader to handle this file type, currently no loader
115115

116116
exports[`loader should throws error when no loader(s) for assets: warnings 1`] = `Array []`;
117117

118+
exports[`loader should work with "asset" module type: errors 1`] = `Array []`;
119+
120+
exports[`loader should work with "asset" module type: module 1`] = `
121+
"// Imports
122+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../src/runtime/api.js\\");
123+
var ___CSS_LOADER_AT_RULE_IMPORT_0___ = require(\\"-!../../src/index.js!./imported.css\\");
124+
var ___CSS_LOADER_GET_URL_IMPORT___ = require(\\"../../src/runtime/getUrl.js\\");
125+
var ___CSS_LOADER_URL_IMPORT_0___ = require(\\"./url/img.png\\");
126+
exports = module.exports = ___CSS_LOADER_API_IMPORT___(false);
127+
exports.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
128+
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___);
129+
// Module
130+
exports.push([module.id, \\"@charset \\\\\\"UTF-8\\\\\\";\\\\n\\\\n/* Comment */\\\\n\\\\n.class {\\\\n color: red;\\\\n background: url(\\" + ___CSS_LOADER_URL_REPLACEMENT_0___ + \\");\\\\n}\\\\n\\\\n.class-duplicate-url {\\\\n background: url(\\" + ___CSS_LOADER_URL_REPLACEMENT_0___ + \\");\\\\n}\\\\n\\\\n:root {\\\\n --foo: 1px;\\\\n --bar: 2px;\\\\n}\\\\n\\\\n.class { a: b c d; }\\\\n\\\\n.two {}\\\\n\\\\n.u-m\\\\\\\\+ { a: b c d; }\\\\n\\\\n.class { content: \\\\\\"\\\\\\\\F10C\\\\\\" }\\\\n\\\\n@media only screen and (max-width: 600px) {\\\\n body {\\\\n background-color: lightblue;\\\\n }\\\\n}\\\\n\\\\n.class {\\\\n content: \\\\\\"\\\\\\\\2193\\\\\\";\\\\n content: \\\\\\"\\\\\\\\2193\\\\\\\\2193\\\\\\";\\\\n content: \\\\\\"\\\\\\\\2193 \\\\\\\\2193\\\\\\";\\\\n content: \\\\\\"\\\\\\\\2193\\\\\\\\2193\\\\\\\\2193\\\\\\";\\\\n content: \\\\\\"\\\\\\\\2193 \\\\\\\\2193 \\\\\\\\2193\\\\\\";\\\\n}\\\\n\\\\n.-top {}\\\\n.\\\\\\\\-top {}\\\\n\\\\n#\\\\\\\\#test {}\\\\n\\\\n.grid {\\\\n display: flex;\\\\n flex-wrap: wrap;\\\\n}\\\\n.grid.\\\\\\\\-top {\\\\n align-items: flex-start;\\\\n}\\\\n.grid.-top {\\\\n align-items: flex-start;\\\\n}\\\\n.grid.\\\\\\\\-middle {\\\\n align-items: center;\\\\n}\\\\n.grid.\\\\\\\\-bottom {\\\\n align-items: flex-end;\\\\n}\\\\n\\\\n.u-m\\\\\\\\00002b {}\\\\n\\\\n.u-m00002b {}\\\\n\\\\n#u-m\\\\\\\\+ {}\\\\n\\\\nbody {\\\\n font-family: '微软雅黑'; /* some chinese font name */\\\\n}\\\\n\\\\n.myStyle {\\\\n content: '\\\\\\\\e901';\\\\n}\\\\n\\\\n.myStyle {\\\\n content: '\\\\\\\\E901';\\\\n}\\\\n\\\\n.♫ {}\\\\n\\\\n.\\\\\\\\3A \\\\\\\\\`\\\\\\\\( {} /* matches elements with class=\\\\\\":\`(\\\\\\" */\\\\n.\\\\\\\\31 a2b3c {} /* matches elements with class=\\\\\\"1a2b3c\\\\\\" */\\\\n#\\\\\\\\#fake-id {} /* matches the element with id=\\\\\\"#fake-id\\\\\\" */\\\\n#-a-b-c- {} /* matches the element with id=\\\\\\"-a-b-c-\\\\\\" */\\\\n#© {} /* matches the element with id=\\\\\\"©\\\\\\" */\\\\n\\\\n:root {\\\\n --title-align: center;\\\\n --sr-only: {\\\\n position: absolute;\\\\n width: 1px;\\\\n height: 1px;\\\\n padding: 0;\\\\n overflow: hidden;\\\\n clip: rect(0,0,0,0);\\\\n white-space: nowrap;\\\\n clip-path: inset(50%);\\\\n border: 0;\\\\n };\\\\n}\\\\n\\\\n.test {\\\\n content: \\\\\\"\\\\\\\\2014\\\\\\\\A0\\\\\\";\\\\n content: \\\\\\"\\\\\\\\2014 \\\\\\\\A0\\\\\\";\\\\n content: \\\\\\"\\\\\\\\A0 \\\\\\\\2014\\\\\\";\\\\n content: \\\\\\"\\\\\\\\A0\\\\\\\\2014\\\\\\";\\\\n margin-top: 1px\\\\\\\\9;\\\\n background-color: #000\\\\\\\\9;\\\\n}\\\\n\\\\n.light.on .bulb:before{\\\\n content: '💡';\\\\n}\\\\n\\\\n.base64 {\\\\n background: url(data:img/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAhxJREFUSA3tk71rU1EYxnMTEoJUkowWwdJ2akEHBfGjCiIF6ZylVUKSm2TqZLGI+A/oIu2UXm8C4lAyF4SWji0tdFLo1Eo7VN0SaBEhH7e/Nz0nPTfGOjiaCyfPc5734zlfCQT6X/8E/vUErL81KBaL9y3LSnued5PcITjUOwR3gsFg2bbtjYt6/NGgXC4P1et1l2aPLmpAbD0SidjpdPqgV15PA9d17zQajU8UxHQRK/4G35Q5pveAK8LlI1ZjPMnlcltnyvnvbwaO41xvtVqy7YHztMACq5xnlb9EY3dRdvcGo1kj5wR+t1AofDG0gM+A875E8DNjRCexsrV8Pj9ZqVQitVrtqejxePxjMpmss5hVTB4buXvMb2DyU2tBTRS+BjvNlVYUpPl7iuVO3Gq1uoQx1FtSOW1gPgp5ZWrdBtNmUDgv5asgxQ8F1af5vhY0YjyjuWC3wTszKJz7GBOkcFlQfW2ONq4FjWi+Hj6DRCKxQOK2TlY4x92EuYd5dvMAbYIzfikau3pu5tJ8KxaLLfo0cyKci7tK4TZjUMcoXAmHwzle0Q/RaC5P1GFMyVx9R9Fo9HYqlTrSgqDvFelAqVQa5hmuMR/WGtjAaBdjwBoDQ0ZsnwVMZjKZ9n0Zem8DSeDPdrnZbL6F2l3NOvUYNZk4oVDoRTabPe4EDNJzB0ZcjAYxeoZ2i3FNxQ7BHYw/cB/fldaH//UETgHHO8S44KbfXgAAAABJRU5ErkJggg==);\\\\n}\\\\n\\\\na[href=''] {\\\\n color: red;\\\\n}\\\\n\\\\na[href='' i] {\\\\n color: red;\\\\n}\\\\n\\\\na[href=\\\\\\"\\\\\\"] {\\\\n color: blue;\\\\n}\\\\n\\\\na[href=\\\\\\"\\\\\\" i] {\\\\n color: blue;\\\\n}\\\\n\\", \\"\\"]);
131+
"
132+
`;
133+
134+
exports[`loader should work with "asset" module type: result 1`] = `
135+
Array [
136+
Array [
137+
"../../src/index.js!./imported.css",
138+
".foo {
139+
color: red;
140+
}
141+
",
142+
"",
143+
],
144+
Array [
145+
"./basic.css",
146+
"@charset \\"UTF-8\\";
147+
148+
/* Comment */
149+
150+
.class {
151+
color: red;
152+
background: url(/webpack/public/path/de52feff0a80fb98abe3.png);
153+
}
154+
155+
.class-duplicate-url {
156+
background: url(/webpack/public/path/de52feff0a80fb98abe3.png);
157+
}
158+
159+
:root {
160+
--foo: 1px;
161+
--bar: 2px;
162+
}
163+
164+
.class { a: b c d; }
165+
166+
.two {}
167+
168+
.u-m\\\\+ { a: b c d; }
169+
170+
.class { content: \\"\\\\F10C\\" }
171+
172+
@media only screen and (max-width: 600px) {
173+
body {
174+
background-color: lightblue;
175+
}
176+
}
177+
178+
.class {
179+
content: \\"\\\\2193\\";
180+
content: \\"\\\\2193\\\\2193\\";
181+
content: \\"\\\\2193 \\\\2193\\";
182+
content: \\"\\\\2193\\\\2193\\\\2193\\";
183+
content: \\"\\\\2193 \\\\2193 \\\\2193\\";
184+
}
185+
186+
.-top {}
187+
.\\\\-top {}
188+
189+
#\\\\#test {}
190+
191+
.grid {
192+
display: flex;
193+
flex-wrap: wrap;
194+
}
195+
.grid.\\\\-top {
196+
align-items: flex-start;
197+
}
198+
.grid.-top {
199+
align-items: flex-start;
200+
}
201+
.grid.\\\\-middle {
202+
align-items: center;
203+
}
204+
.grid.\\\\-bottom {
205+
align-items: flex-end;
206+
}
207+
208+
.u-m\\\\00002b {}
209+
210+
.u-m00002b {}
211+
212+
#u-m\\\\+ {}
213+
214+
body {
215+
font-family: '微软雅黑'; /* some chinese font name */
216+
}
217+
218+
.myStyle {
219+
content: '\\\\e901';
220+
}
221+
222+
.myStyle {
223+
content: '\\\\E901';
224+
}
225+
226+
.♫ {}
227+
228+
.\\\\3A \\\\\`\\\\( {} /* matches elements with class=\\":\`(\\" */
229+
.\\\\31 a2b3c {} /* matches elements with class=\\"1a2b3c\\" */
230+
#\\\\#fake-id {} /* matches the element with id=\\"#fake-id\\" */
231+
#-a-b-c- {} /* matches the element with id=\\"-a-b-c-\\" */
232+
{} /* matches the element with id=\\\\" */
233+
234+
:root {
235+
--title-align: center;
236+
--sr-only: {
237+
position: absolute;
238+
width: 1px;
239+
height: 1px;
240+
padding: 0;
241+
overflow: hidden;
242+
clip: rect(0,0,0,0);
243+
white-space: nowrap;
244+
clip-path: inset(50%);
245+
border: 0;
246+
};
247+
}
248+
249+
.test {
250+
content: \\"\\\\2014\\\\A0\\";
251+
content: \\"\\\\2014 \\\\A0\\";
252+
content: \\"\\\\A0 \\\\2014\\";
253+
content: \\"\\\\A0\\\\2014\\";
254+
margin-top: 1px\\\\9;
255+
background-color: #000\\\\9;
256+
}
257+
258+
.light.on .bulb:before{
259+
content: '💡';
260+
}
261+
262+
.base64 {
263+
background: url(data:img/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAhxJREFUSA3tk71rU1EYxnMTEoJUkowWwdJ2akEHBfGjCiIF6ZylVUKSm2TqZLGI+A/oIu2UXm8C4lAyF4SWji0tdFLo1Eo7VN0SaBEhH7e/Nz0nPTfGOjiaCyfPc5734zlfCQT6X/8E/vUErL81KBaL9y3LSnued5PcITjUOwR3gsFg2bbtjYt6/NGgXC4P1et1l2aPLmpAbD0SidjpdPqgV15PA9d17zQajU8UxHQRK/4G35Q5pveAK8LlI1ZjPMnlcltnyvnvbwaO41xvtVqy7YHztMACq5xnlb9EY3dRdvcGo1kj5wR+t1AofDG0gM+A875E8DNjRCexsrV8Pj9ZqVQitVrtqejxePxjMpmss5hVTB4buXvMb2DyU2tBTRS+BjvNlVYUpPl7iuVO3Gq1uoQx1FtSOW1gPgp5ZWrdBtNmUDgv5asgxQ8F1af5vhY0YjyjuWC3wTszKJz7GBOkcFlQfW2ONq4FjWi+Hj6DRCKxQOK2TlY4x92EuYd5dvMAbYIzfikau3pu5tJ8KxaLLfo0cyKci7tK4TZjUMcoXAmHwzle0Q/RaC5P1GFMyVx9R9Fo9HYqlTrSgqDvFelAqVQa5hmuMR/WGtjAaBdjwBoDQ0ZsnwVMZjKZ9n0Zem8DSeDPdrnZbL6F2l3NOvUYNZk4oVDoRTabPe4EDNJzB0ZcjAYxeoZ2i3FNxQ7BHYw/cB/fldaH//UETgHHO8S44KbfXgAAAABJRU5ErkJggg==);
264+
}
265+
266+
a[href=''] {
267+
color: red;
268+
}
269+
270+
a[href='' i] {
271+
color: red;
272+
}
273+
274+
a[href=\\"\\"] {
275+
color: blue;
276+
}
277+
278+
a[href=\\"\\" i] {
279+
color: blue;
280+
}
281+
",
282+
"",
283+
],
284+
]
285+
`;
286+
287+
exports[`loader should work with "asset" module type: warnings 1`] = `Array []`;
288+
118289
exports[`loader should work with "sass-loader": errors 1`] = `Array []`;
119290
120291
exports[`loader should work with "sass-loader": module 1`] = `

test/helpers/getCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
2727
],
2828
},
2929
{
30-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
30+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
3131
loader: 'file-loader',
3232
options: { name: '[name].[ext]' },
3333
},

test/loader.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import path from 'path';
22

33
import postcssPresetEnv from 'postcss-preset-env';
44

5+
import { version } from 'webpack';
6+
57
import {
68
compile,
79
getCompiler,
@@ -48,6 +50,50 @@ describe('loader', () => {
4850
expect(getErrors(stats)).toMatchSnapshot('errors');
4951
});
5052

53+
it('should work with "asset" module type', async () => {
54+
const isWebpack5 = version[0] === '5';
55+
56+
const compiler = getCompiler(
57+
'./basic.js',
58+
{},
59+
{
60+
module: {
61+
rules: [
62+
{
63+
test: /\.css$/i,
64+
use: [
65+
{
66+
loader: path.resolve(__dirname, '../src'),
67+
},
68+
],
69+
},
70+
isWebpack5
71+
? {
72+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
73+
type: 'asset',
74+
}
75+
: {
76+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
77+
loader: 'file-loader',
78+
options: { name: '[hash].[ext]' },
79+
},
80+
],
81+
},
82+
experiments: {
83+
asset: true,
84+
},
85+
}
86+
);
87+
const stats = await compile(compiler);
88+
89+
expect(getModuleSource('./basic.css', stats)).toMatchSnapshot('module');
90+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
91+
'result'
92+
);
93+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
94+
expect(getErrors(stats)).toMatchSnapshot('errors');
95+
});
96+
5197
it('should throws error when no loader(s) for assets', async () => {
5298
const compiler = getCompiler(
5399
'./basic.js',

0 commit comments

Comments
 (0)