Skip to content

Commit ee2d253

Browse files
test: importLoaders option (#841)
1 parent 1dad1fb commit ee2d253

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`importLoaders option 0 (\`postcss-loader\` before): errors 1`] = `Array []`;
4+
5+
exports[`importLoaders option 0 (\`postcss-loader\` before): module (evaluated) 1`] = `
6+
Array [
7+
Array [
8+
2,
9+
".bar {
10+
color: blue;
11+
color: rgb(0 0 100% / 90%);
12+
}
13+
",
14+
"",
15+
],
16+
Array [
17+
1,
18+
".foo {
19+
color: red;
20+
color: rgba(0, 0, 255, 0.9);
21+
}
22+
",
23+
"",
24+
],
25+
]
26+
`;
27+
28+
exports[`importLoaders option 0 (\`postcss-loader\` before): module 1`] = `
29+
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
30+
// imports
31+
exports.i(require(\\"-!../../../index.js??ref--4-0!./imported.css\\"), \\"\\");
32+
33+
// module
34+
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
35+
36+
// exports
37+
"
38+
`;
39+
40+
exports[`importLoaders option 0 (\`postcss-loader\` before): warnings 1`] = `Array []`;
41+
42+
exports[`importLoaders option 1 (\`postcss-loader\` before): errors 1`] = `Array []`;
43+
44+
exports[`importLoaders option 1 (\`postcss-loader\` before): module (evaluated) 1`] = `
45+
Array [
46+
Array [
47+
2,
48+
".bar {
49+
color: blue;
50+
color: rgba(0, 0, 255, 0.9);
51+
}
52+
",
53+
"",
54+
],
55+
Array [
56+
1,
57+
".foo {
58+
color: red;
59+
color: rgba(0, 0, 255, 0.9);
60+
}
61+
",
62+
"",
63+
],
64+
]
65+
`;
66+
67+
exports[`importLoaders option 1 (\`postcss-loader\` before): module 1`] = `
68+
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
69+
// imports
70+
exports.i(require(\\"-!../../../index.js??ref--4-0!../../../node_modules/postcss-loader/src/index.js??ref--4-1!./imported.css\\"), \\"\\");
71+
72+
// module
73+
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
74+
75+
// exports
76+
"
77+
`;
78+
79+
exports[`importLoaders option 1 (\`postcss-loader\` before): warnings 1`] = `Array []`;
80+
81+
exports[`importLoaders option 1 (no loaders before): errors 1`] = `Array []`;
82+
83+
exports[`importLoaders option 1 (no loaders before): module (evaluated) 1`] = `
84+
Array [
85+
Array [
86+
2,
87+
".bar {
88+
color: blue;
89+
color: rgb(0 0 100% / 90%);
90+
}
91+
",
92+
"",
93+
],
94+
Array [
95+
1,
96+
".foo {
97+
color: red;
98+
color: rgb(0 0 100% / 90%);
99+
}
100+
",
101+
"",
102+
],
103+
]
104+
`;
105+
106+
exports[`importLoaders option 1 (no loaders before): module 1`] = `
107+
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
108+
// imports
109+
exports.i(require(\\"-!../../../index.js??ref--4-0!./imported.css\\"), \\"\\");
110+
111+
// module
112+
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgb(0 0 100% / 90%);\\\\n}\\\\n\\", \\"\\"]);
113+
114+
// exports
115+
"
116+
`;
117+
118+
exports[`importLoaders option 1 (no loaders before): warnings 1`] = `Array []`;
119+
120+
exports[`importLoaders option 2 (\`postcss-loader\` before): errors 1`] = `Array []`;
121+
122+
exports[`importLoaders option 2 (\`postcss-loader\` before): module (evaluated) 1`] = `
123+
Array [
124+
Array [
125+
2,
126+
".bar {
127+
color: blue;
128+
color: rgba(0, 0, 255, 0.9);
129+
}
130+
",
131+
"",
132+
],
133+
Array [
134+
1,
135+
".foo {
136+
color: red;
137+
color: rgba(0, 0, 255, 0.9);
138+
}
139+
",
140+
"",
141+
],
142+
]
143+
`;
144+
145+
exports[`importLoaders option 2 (\`postcss-loader\` before): module 1`] = `
146+
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
147+
// imports
148+
exports.i(require(\\"-!../../../index.js??ref--4-0!../../../node_modules/postcss-loader/src/index.js??ref--4-1!./imported.css\\"), \\"\\");
149+
150+
// module
151+
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
152+
153+
// exports
154+
"
155+
`;
156+
157+
exports[`importLoaders option 2 (\`postcss-loader\` before): warnings 1`] = `Array []`;
158+
159+
exports[`importLoaders option not specify (no loader before): errors 1`] = `Array []`;
160+
161+
exports[`importLoaders option not specify (no loader before): module (evaluated) 1`] = `
162+
Array [
163+
Array [
164+
2,
165+
".bar {
166+
color: blue;
167+
color: rgb(0 0 100% / 90%);
168+
}
169+
",
170+
"",
171+
],
172+
Array [
173+
1,
174+
".foo {
175+
color: red;
176+
color: rgba(0, 0, 255, 0.9);
177+
}
178+
",
179+
"",
180+
],
181+
]
182+
`;
183+
184+
exports[`importLoaders option not specify (no loader before): module 1`] = `
185+
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
186+
// imports
187+
exports.i(require(\\"-!../../../index.js??ref--4-0!./imported.css\\"), \\"\\");
188+
189+
// module
190+
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
191+
192+
// exports
193+
"
194+
`;
195+
196+
exports[`importLoaders option not specify (no loader before): warnings 1`] = `Array []`;
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.bar {
2+
color: blue;
3+
color: rgb(0 0 100% / 90%);
4+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import './imported.css';
2+
3+
.foo {
4+
color: red;
5+
color: rgb(0 0 100% / 90%);
6+
}

test/helpers.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function evaluated(output, modules, moduleId = 1) {
3131
const importedModule = modules.find((el) => {
3232
const modulePath = el.identifier.split('!').pop();
3333
const importedPaths = [
34+
'nested-import',
3435
'postcss-present-env',
3536
'icss/tests-cases/import',
3637
'icss/tests-cases/import-reserved-keywords',

test/importLoaders-option.test.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const postcssPresetEnv = require('postcss-preset-env');
2+
3+
const { webpack, evaluated } = require('./helpers');
4+
5+
describe('importLoaders option', () => {
6+
it('not specify (no loader before)', async () => {
7+
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
8+
const config = {
9+
postcssLoader: true,
10+
postcssLoaderOptions: {
11+
plugins: () => [postcssPresetEnv({ stage: 0 })],
12+
},
13+
};
14+
const testId = './nested-import/source.css';
15+
const stats = await webpack(testId, config);
16+
const { modules } = stats.toJson();
17+
const module = modules.find((m) => m.id === testId);
18+
19+
expect(module.source).toMatchSnapshot('module');
20+
expect(evaluated(module.source, modules)).toMatchSnapshot(
21+
'module (evaluated)'
22+
);
23+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
24+
expect(stats.compilation.errors).toMatchSnapshot('errors');
25+
});
26+
27+
it('1 (no loaders before)', async () => {
28+
const config = {
29+
loader: { options: { importLoaders: 1 } },
30+
};
31+
const testId = './nested-import/source.css';
32+
const stats = await webpack(testId, config);
33+
const { modules } = stats.toJson();
34+
const module = modules.find((m) => m.id === testId);
35+
36+
expect(module.source).toMatchSnapshot('module');
37+
expect(evaluated(module.source, modules)).toMatchSnapshot(
38+
'module (evaluated)'
39+
);
40+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
41+
expect(stats.compilation.errors).toMatchSnapshot('errors');
42+
});
43+
44+
it('0 (`postcss-loader` before)', async () => {
45+
const config = {
46+
loader: { options: { importLoaders: 0 } },
47+
postcssLoader: true,
48+
postcssLoaderOptions: {
49+
plugins: () => [postcssPresetEnv({ stage: 0 })],
50+
},
51+
};
52+
const testId = './nested-import/source.css';
53+
const stats = await webpack(testId, config);
54+
const { modules } = stats.toJson();
55+
const module = modules.find((m) => m.id === testId);
56+
57+
expect(module.source).toMatchSnapshot('module');
58+
expect(evaluated(module.source, modules)).toMatchSnapshot(
59+
'module (evaluated)'
60+
);
61+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
62+
expect(stats.compilation.errors).toMatchSnapshot('errors');
63+
});
64+
65+
it('1 (`postcss-loader` before)', async () => {
66+
const config = {
67+
loader: { options: { importLoaders: 1 } },
68+
postcssLoader: true,
69+
postcssLoaderOptions: {
70+
plugins: () => [postcssPresetEnv({ stage: 0 })],
71+
},
72+
};
73+
const testId = './nested-import/source.css';
74+
const stats = await webpack(testId, config);
75+
const { modules } = stats.toJson();
76+
const module = modules.find((m) => m.id === testId);
77+
78+
expect(module.source).toMatchSnapshot('module');
79+
expect(evaluated(module.source, modules)).toMatchSnapshot(
80+
'module (evaluated)'
81+
);
82+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
83+
expect(stats.compilation.errors).toMatchSnapshot('errors');
84+
});
85+
86+
it('2 (`postcss-loader` before)', async () => {
87+
const config = {
88+
loader: { options: { importLoaders: 2 } },
89+
postcssLoader: true,
90+
postcssLoaderOptions: {
91+
plugins: () => [postcssPresetEnv({ stage: 0 })],
92+
},
93+
};
94+
const testId = './nested-import/source.css';
95+
const stats = await webpack(testId, config);
96+
const { modules } = stats.toJson();
97+
const module = modules.find((m) => m.id === testId);
98+
99+
expect(module.source).toMatchSnapshot('module');
100+
expect(evaluated(module.source, modules)).toMatchSnapshot(
101+
'module (evaluated)'
102+
);
103+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
104+
expect(stats.compilation.errors).toMatchSnapshot('errors');
105+
});
106+
});

0 commit comments

Comments
 (0)