Skip to content

Commit a5c1b5f

Browse files
test: code coverange (#1114)
1 parent 908ecee commit a5c1b5f

16 files changed

+189
-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/__snapshots__/loader.test.js.snap

+17
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,23 @@ a[href=\\"\\" i] {
579579
580580
exports[`loader should work with empty options: warnings 1`] = `Array []`;
581581
582+
exports[`loader should work with none AST metadata: errors 1`] = `Array []`;
583+
584+
exports[`loader should work with none AST metadata: result 1`] = `
585+
Array [
586+
Array [
587+
"./simple.css",
588+
".some-class {
589+
color: red;
590+
}
591+
",
592+
"",
593+
],
594+
]
595+
`;
596+
597+
exports[`loader should work with none AST metadata: warnings 1`] = `Array []`;
598+
582599
exports[`loader should work with the "modules.auto" option and the "importLoaders" option: errors 1`] = `Array []`;
583600
584601
exports[`loader should work with the "modules.auto" option and the "importLoaders" option: result 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/helpers/preLoader.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function loader(content, map) {
2+
const callback = this.async();
3+
4+
return callback(null, content, map, 'non-ast-meta');
5+
}

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);

test/loader.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,35 @@ describe('loader', () => {
411411
expect(getWarnings(stats)).toMatchSnapshot('warnings');
412412
expect(getErrors(stats)).toMatchSnapshot('errors');
413413
});
414+
415+
it('should work with none AST metadata', async () => {
416+
const compiler = getCompiler(
417+
'./simple.js',
418+
{},
419+
{
420+
module: {
421+
rules: [
422+
{
423+
test: /\.css$/i,
424+
rules: [
425+
{
426+
loader: path.resolve(__dirname, '../src'),
427+
},
428+
{
429+
loader: path.resolve(__dirname, '../test/helpers/preLoader'),
430+
},
431+
],
432+
},
433+
],
434+
},
435+
}
436+
);
437+
const stats = await compile(compiler);
438+
439+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
440+
'result'
441+
);
442+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
443+
expect(getErrors(stats)).toMatchSnapshot('errors');
444+
});
414445
});

test/runtime/__snapshots__/api.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports[`api should import modules 1`] = `"body { b: 2; }body { c: 3; }body { b: 2; }@media print {body { b: 2; }}@media print {body { d: 4; }}@media screen and (orientation:landscape) {body { a: 1; }}@media (orientation:landscape) {body { a: 1; }}"`;
44

5+
exports[`api should import modules when module string 1`] = `".button { b: 2; }"`;
6+
7+
exports[`api should import modules with dedupe 1`] = `"body { b: 1; }body { b: 2; }.button { b: 3; }"`;
8+
59
exports[`api should import named modules 1`] = `"body { b: 2; }body { c: 3; }body { b: 2; }@media print {body { b: 2; }}@media print {body { d: 4; }}@media screen {body { a: 1; }}"`;
610

711
exports[`api should toString a single module 1`] = `"body { a: 1; }"`;

test/runtime/api.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,30 @@ describe('api', () => {
137137

138138
expect(m.toString()).toMatchSnapshot();
139139
});
140+
141+
it('should import modules with dedupe', () => {
142+
const m = api();
143+
144+
const m1 = [null, 'body { b: 1; }', ''];
145+
const m2 = ['./module2', 'body { b: 2; }', ''];
146+
const m3 = ['./module3', '.button { b: 3; }', ''];
147+
148+
m.i([m1], '', true);
149+
m.i([m2], '', true);
150+
m.i([m3], '', true);
151+
m.i([m3], '', true);
152+
m.i([m3], '', true);
153+
154+
expect(m.toString()).toMatchSnapshot();
155+
expect(m.length).toBe(3);
156+
});
157+
158+
it('should import modules when module string', () => {
159+
const m = api();
160+
161+
m.i('.button { b: 2; }');
162+
m.i('');
163+
164+
expect(m.toString()).toMatchSnapshot();
165+
});
140166
});

0 commit comments

Comments
 (0)