Skip to content

Commit 5565848

Browse files
test: code coverange
1 parent 0af02bf commit 5565848

File tree

11 files changed

+106
-7
lines changed

11 files changed

+106
-7
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from './utils';
2727

2828
export default function loader(content, map, meta) {
29-
const options = getOptions(this) || {};
29+
const options = getOptions(this);
3030

3131
validateOptions(schema, options, {
3232
name: 'CSS Loader',

src/plugins/postcss-import-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ 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+
} else {
5454
// Invalid function - `@import nourl(test.css);`
5555
if (nodes[0].value.toLowerCase() !== 'url') {
5656
result.warn(`Unable to find uri in "${atRule.toString()}"`, {

src/plugins/postcss-url-parser.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,9 @@ export default postcss.plugin(pluginName, (options) => (css, result) => {
172172
const { node, url, needQuotes, isStringValue } = rule;
173173
const splittedUrl = url.split(/(\?)?#/);
174174
const [urlWithoutHash, singleQuery, hashValue] = splittedUrl;
175-
const hash =
176-
singleQuery || hashValue
177-
? `${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}`
178-
: '';
175+
176+
let hash = singleQuery ? '?' : '';
177+
hash += hashValue ? `#${hashValue}` : '';
179178

180179
let normalizedUrl = normalizeUrl(
181180
urlWithoutHash,

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

+62
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,68 @@ Array [
313313

314314
exports[`"import" option should respect style field in package.json: warnings 1`] = `Array []`;
315315

316+
exports[`"import" option should work resolve order: local -> node_modules -> alias: errors 1`] = `Array []`;
317+
318+
exports[`"import" option should work resolve order: local -> node_modules -> alias: module 1`] = `
319+
"// Imports
320+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
321+
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??[ident]!./test.css\\";
322+
import ___CSS_LOADER_AT_RULE_IMPORT_1___ from \\"-!../../../src/index.js??[ident]!./issue-683.css\\";
323+
import ___CSS_LOADER_AT_RULE_IMPORT_2___ from \\"-!../../../src/index.js??[ident]!./node_modules/package/tilde.css\\";
324+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
325+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
326+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_1___);
327+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_2___);
328+
// Module
329+
___CSS_LOADER_EXPORT___.push([module.id, \\"\\\\n\\", \\"\\"]);
330+
// Exports
331+
export default ___CSS_LOADER_EXPORT___;
332+
"
333+
`;
334+
335+
exports[`"import" option should work resolve order: local -> node_modules -> alias: result 1`] = `
336+
Array [
337+
Array [
338+
"../../src/index.js?[ident]!./import/test.css",
339+
".test {
340+
a: a;
341+
}
342+
",
343+
"",
344+
],
345+
Array [
346+
"../../src/index.js?[ident]!./import/node_modules/issue-683/test.css",
347+
".test {
348+
color: coral;
349+
}
350+
",
351+
"",
352+
],
353+
Array [
354+
"../../src/index.js?[ident]!./import/issue-683.css",
355+
"
356+
",
357+
"",
358+
],
359+
Array [
360+
"../../src/index.js?[ident]!./import/node_modules/package/tilde.css",
361+
".tilde {
362+
color: yellow;
363+
}
364+
",
365+
"",
366+
],
367+
Array [
368+
"./import/import-order.css",
369+
"
370+
",
371+
"",
372+
],
373+
]
374+
`;
375+
376+
exports[`"import" option should work resolve order: local -> node_modules -> alias: warnings 1`] = `Array []`;
377+
316378
exports[`"import" option should work when "Function": errors 1`] = `Array []`;
317379

318380
exports[`"import" option should work when "Function": module 1`] = `

test/fixtures/import/import-order.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@import "test";
2+
@import "issue-683";
3+
@import "aliasesPackage";

test/fixtures/import/import-order.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './import-order.css';
2+
3+
__export__ = css;
4+
5+
export default css;

test/fixtures/import/node_modules/issue-683/package.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/import/node_modules/test/package.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/import/node_modules/test/test.css

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/helpers/getCompiler.js

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export default (fixture, loaderOptions = {}, config = {}) => {
3535
},
3636
resolve: {
3737
alias: {
38+
aliasesPackage: path.resolve(
39+
__dirname,
40+
'../fixtures/import/node_modules/package/tilde.css'
41+
),
3842
aliasesImg: path.resolve(__dirname, '../fixtures/url'),
3943
aliasesImport: path.resolve(__dirname, '../fixtures/import'),
4044
aliasesComposes: path.resolve(

test/import-option.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ describe('"import" option', () => {
153153
expect(getErrors(stats)).toMatchSnapshot('errors');
154154
});
155155

156+
it('should work resolve order: local -> node_modules -> alias', async () => {
157+
const compiler = getCompiler('./import/import-order.js');
158+
const stats = await compile(compiler);
159+
160+
expect(getModuleSource('./import/import-order.css', stats)).toMatchSnapshot(
161+
'module'
162+
);
163+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
164+
'result'
165+
);
166+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
167+
expect(getErrors(stats)).toMatchSnapshot('errors');
168+
});
169+
156170
it('should emit warning when unresolved import', async () => {
157171
const compiler = getCompiler('./import/unresolved.js');
158172
const stats = await compile(compiler);

0 commit comments

Comments
 (0)