Skip to content

Commit fed0327

Browse files
test: coverange
1 parent bc19ddd commit fed0327

File tree

7 files changed

+184
-49
lines changed

7 files changed

+184
-49
lines changed

src/plugins/postcss-import-parser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
5050
if (nodes[0].type === 'string') {
5151
isStringValue = true;
5252
url = nodes[0].value;
53-
} else if (nodes[0].type === 'function') {
53+
}
54+
55+
if (nodes[0].type === 'function') {
5456
// Invalid function - `@import nourl(test.css);`
5557
if (nodes[0].value.toLowerCase() !== 'url') {
5658
result.warn(`Unable to find uri in "${atRule.toString()}"`, {

src/plugins/postcss-url-parser.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ function getNodeFromUrlFunc(node) {
2020
function ruleValidate(rule, decl, result, options) {
2121
// https://www.w3.org/TR/css-syntax-3/#typedef-url-token
2222
if (rule.url.replace(/^[\s]+|[\s]+$/g, '').length === 0) {
23-
result.warn(
24-
`Unable to find uri in '${decl ? decl.toString() : decl.value}'`,
25-
{ node: decl }
26-
);
23+
result.warn(`Unable to find uri in '${decl.toString()}'`, { node: decl });
2724

2825
return false;
2926
}
@@ -139,13 +136,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
139136
const replacementsMap = new Map();
140137
const urlToHelper = require.resolve('../runtime/getUrl.js');
141138

142-
let parsedResults;
143-
144-
try {
145-
parsedResults = await walkCssAsync(css, result, options);
146-
} catch (error) {
147-
reject(error);
148-
}
139+
const parsedResults = await walkCssAsync(css, result, options);
149140

150141
if (parsedResults.length === 0) {
151142
resolve();
@@ -169,9 +160,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
169160
// 'CSS_LOADER_GET_URL_IMPORT'
170161
order: 2,
171162
importName: '___CSS_LOADER_GET_URL_IMPORT___',
172-
url: options.urlHandler
173-
? options.urlHandler(urlToHelper)
174-
: urlToHelper,
163+
url: options.urlHandler(urlToHelper),
175164
index,
176165
},
177166
});
@@ -236,9 +225,7 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
236225
// 'CSS_LOADER_URL_IMPORT'
237226
order: 3,
238227
importName,
239-
url: options.urlHandler
240-
? options.urlHandler(resolvedUrl)
241-
: resolvedUrl,
228+
url: options.urlHandler(resolvedUrl),
242229
index: currentIndex,
243230
},
244231
});

test/__snapshots__/import-option.test.js.snap

Lines changed: 41 additions & 24 deletions
Large diffs are not rendered by default.

test/__snapshots__/sourceMap-option.test.js.snap

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,81 @@ Array [
477477

478478
exports[`"sourceMap" option true should generate source maps when source maps equal to "undefined" from an other loader: warnings 1`] = `Array []`;
479479

480+
exports[`"sourceMap" option true should generate source maps when source maps is valid and is set sourceRoot: errors 1`] = `Array []`;
481+
482+
exports[`"sourceMap" option true should generate source maps when source maps is valid and is set sourceRoot: module 1`] = `
483+
"// Imports
484+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../src/runtime/api.js\\");
485+
var ___CSS_LOADER_AT_RULE_IMPORT_0___ = require(\\"-!../../../src/index.js??[ident]!./nested/nested.css\\");
486+
___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(true);
487+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
488+
// Module
489+
___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"/fixtures/source-map/basic.css\\",\\"basic.css\\"],\\"names\\":[],\\"mappings\\":\\"AAEA;ECCE,UAAU;AACZ\\",\\"file\\":\\"basic.css\\",\\"sourcesContent\\":[\\".class {\\\\n color: red;\\\\n}\\\\n\\",\\"@import \\\\\\"./nested/nested.css\\\\\\";\\\\n\\\\n.class {\\\\n color: red;\\\\n}\\\\n\\"]}]);
490+
// Exports
491+
module.exports = ___CSS_LOADER_EXPORT___;
492+
"
493+
`;
494+
495+
exports[`"sourceMap" option true should generate source maps when source maps is valid and is set sourceRoot: result 1`] = `
496+
Array [
497+
Array [
498+
"../../src/index.js?[ident]!./source-map/nested/nested.css",
499+
".nested {
500+
color: blue;
501+
}
502+
",
503+
"",
504+
Object {
505+
"file": "nested.css",
506+
"mappings": "AAAA;EACE,WAAW;AACb",
507+
"names": Array [],
508+
"sources": Array [
509+
"nested.css",
510+
],
511+
"sourcesContent": Array [
512+
".nested {
513+
color: blue;
514+
}
515+
",
516+
],
517+
"version": 3,
518+
},
519+
],
520+
Array [
521+
"./source-map/basic.css",
522+
".class {
523+
color: red;
524+
}
525+
",
526+
"",
527+
Object {
528+
"file": "basic.css",
529+
"mappings": "AAEA;ECCE,UAAU;AACZ",
530+
"names": Array [],
531+
"sources": Array [
532+
"/fixtures/source-map/basic.css",
533+
"basic.css",
534+
],
535+
"sourcesContent": Array [
536+
".class {
537+
color: red;
538+
}
539+
",
540+
"@import \\"./nested/nested.css\\";
541+
542+
.class {
543+
color: red;
544+
}
545+
",
546+
],
547+
"version": 3,
548+
},
549+
],
550+
]
551+
`;
552+
553+
exports[`"sourceMap" option true should generate source maps when source maps is valid and is set sourceRoot: warnings 1`] = `Array []`;
554+
480555
exports[`"sourceMap" option true should generate source maps when source maps is valid and string from an other loader: errors 1`] = `Array []`;
481556

482557
exports[`"sourceMap" option true should generate source maps when source maps is valid and string from an other loader: module 1`] = `

test/fixtures/import/import.css

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ st.css');
103103
@import './t\65st%20test.css';
104104
@import url( test.css );
105105
@import nourl(test.css);
106-
/* TODO need fix */
107-
/*@import '\*/
108-
/*\*/
109-
/*\*/
110-
/*';*/
106+
@import '\
107+
\
108+
\
109+
';

test/helpers/normalizeErrors.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ function removeCWD(str) {
55
let cwd = process.cwd();
66

77
if (isWin) {
8-
// eslint-disable-next-line no-param-reassign
9-
str = str.replace(/\\/g, '/');
8+
const stringArray = str.split('\n');
9+
10+
if (stringArray.length === 1) {
11+
// eslint-disable-next-line no-param-reassign
12+
str = str.replace(/\\/g, '/');
13+
}
14+
1015
// eslint-disable-next-line no-param-reassign
1116
cwd = cwd.replace(/\\/g, '/');
1217
}

test/sourceMap-option.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,56 @@ describe('"sourceMap" option', () => {
193193
expect(getErrors(stats)).toMatchSnapshot('errors');
194194
});
195195

196+
it('should generate source maps when source maps is valid and is set sourceRoot', async () => {
197+
const compiler = getCompiler(
198+
'./source-map/basic.js',
199+
{},
200+
{
201+
module: {
202+
rules: [
203+
{
204+
test: /\.css$/i,
205+
use: [
206+
{
207+
loader: path.resolve(__dirname, '../src'),
208+
options: { sourceMap: true },
209+
},
210+
{
211+
loader: path.resolve(
212+
__dirname,
213+
'./fixtures/source-map-loader.js'
214+
),
215+
options: {
216+
sourceMap: JSON.stringify({
217+
foo: 'bar',
218+
version: 3,
219+
sources: ['basic.css'],
220+
sourceRoot: '/fixtures/source-map',
221+
names: [],
222+
mappings: 'AAAA;EACE,UAAU;AACZ',
223+
file: 'basic.css',
224+
sourcesContent: ['.class {\n color: red;\n}\n'],
225+
}),
226+
},
227+
},
228+
],
229+
},
230+
],
231+
},
232+
}
233+
);
234+
const stats = await compile(compiler);
235+
236+
expect(getModuleSource('./source-map/basic.css', stats)).toMatchSnapshot(
237+
'module'
238+
);
239+
expect(
240+
getExecutedCode('main.bundle.js', compiler, stats)
241+
).toMatchSnapshot('result');
242+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
243+
expect(getErrors(stats)).toMatchSnapshot('errors');
244+
});
245+
196246
it('should generate source maps when source maps is valid from an other loader (`postcss-loader`)', async () => {
197247
const compiler = getCompiler(
198248
'./source-map/basic-postcss.js',

0 commit comments

Comments
 (0)