Skip to content

Commit 453ccc1

Browse files
test: more
1 parent 7c949f4 commit 453ccc1

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
@@ -113,6 +113,177 @@ You may need an appropriate loader to handle this file type, currently no loader
113113

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

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