Skip to content

Commit ec58a7c

Browse files
feat: the importLoaders can be string (#1178)
1 parent df490c7 commit ec58a7c

File tree

6 files changed

+98
-18
lines changed

6 files changed

+98
-18
lines changed

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
{
137137
"type": "boolean"
138138
},
139+
{
140+
"type": "string"
141+
},
139142
{
140143
"type": "integer"
141144
}

src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ function normalizeOptions(rawOptions, loaderContext) {
225225
typeof rawOptions.sourceMap === 'boolean'
226226
? rawOptions.sourceMap
227227
: loaderContext.sourceMap,
228-
importLoaders: rawOptions.importLoaders,
228+
importLoaders:
229+
typeof rawOptions.importLoaders === 'string'
230+
? parseInt(rawOptions.importLoaders, 10)
231+
: rawOptions.importLoaders,
229232
esModule:
230233
typeof rawOptions.esModule === 'undefined' ? true : rawOptions.esModule,
231234
};

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,57 @@ Array [
5151

5252
exports[`"importLoaders" option should work when not specified: warnings 1`] = `Array []`;
5353

54+
exports[`"importLoaders" option should work with a value equal to ""1"" ("postcss-loader" before): errors 1`] = `Array []`;
55+
56+
exports[`"importLoaders" option should work with a value equal to ""1"" ("postcss-loader" before): module 1`] = `
57+
"// Imports
58+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
59+
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??[ident]!./imported.css\\";
60+
import ___CSS_LOADER_AT_RULE_IMPORT_1___ from \\"-!../../../src/index.js??[ident]!./other-imported.css\\";
61+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
62+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
63+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_1___);
64+
// Module
65+
___CSS_LOADER_EXPORT___.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
66+
// Exports
67+
export default ___CSS_LOADER_EXPORT___;
68+
"
69+
`;
70+
71+
exports[`"importLoaders" option should work with a value equal to ""1"" ("postcss-loader" before): result 1`] = `
72+
Array [
73+
Array [
74+
"../../src/index.js?[ident]!./nested-import/imported.css",
75+
".bar {
76+
color: blue;
77+
color: rgba(0, 0, 255, 0.9);
78+
}
79+
",
80+
"",
81+
],
82+
Array [
83+
"../../src/index.js?[ident]!./nested-import/other-imported.css",
84+
".baz {
85+
color: green;
86+
color: rgba(0, 0, 255, 0.9);
87+
}
88+
",
89+
"",
90+
],
91+
Array [
92+
"./nested-import/source.css",
93+
".foo {
94+
color: red;
95+
color: rgba(0, 0, 255, 0.9);
96+
}
97+
",
98+
"",
99+
],
100+
]
101+
`;
102+
103+
exports[`"importLoaders" option should work with a value equal to ""1"" ("postcss-loader" before): warnings 1`] = `Array []`;
104+
54105
exports[`"importLoaders" option should work with a value equal to "0" (\`postcss-loader\` before): errors 1`] = `Array []`;
55106

56107
exports[`"importLoaders" option should work with a value equal to "0" (\`postcss-loader\` before): module 1`] = `

test/__snapshots__/validate-options.test.js.snap

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,14 @@ exports[`validate options should throw an error on the "import" option with "tru
2828
* options.import should be an instance of function."
2929
`;
3030

31-
exports[`validate options should throw an error on the "importLoaders" option with "1" value 1`] = `
32-
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
33-
- options.importLoaders should be one of these:
34-
boolean | integer
35-
-> Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).
36-
Details:
37-
* options.importLoaders should be a boolean.
38-
* options.importLoaders should be a integer."
39-
`;
40-
4131
exports[`validate options should throw an error on the "importLoaders" option with "2.5" value 1`] = `
4232
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
4333
- options.importLoaders should be one of these:
44-
boolean | integer
34+
boolean | string | integer
4535
-> Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).
4636
Details:
4737
* options.importLoaders should be a boolean.
38+
* options.importLoaders should be a string.
4839
* options.importLoaders should be a integer."
4940
`;
5041

test/importLoaders-option.test.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313

1414
describe('"importLoaders" option', () => {
1515
it('should work when not specified', async () => {
16-
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
1716
const compiler = getCompiler(
1817
'./nested-import/source.js',
1918
{},
@@ -47,7 +46,6 @@ describe('"importLoaders" option', () => {
4746
});
4847

4948
it('should work with a value equal to "0" (`postcss-loader` before)', async () => {
50-
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
5149
const compiler = getCompiler(
5250
'./nested-import/source.js',
5351
{},
@@ -98,7 +96,6 @@ describe('"importLoaders" option', () => {
9896
});
9997

10098
it('should work with a value equal to "1" ("postcss-loader" before)', async () => {
101-
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
10299
const compiler = getCompiler(
103100
'./nested-import/source.js',
104101
{},
@@ -135,7 +132,6 @@ describe('"importLoaders" option', () => {
135132
});
136133

137134
it('should work with a value equal to "2" ("postcss-loader" before)', async () => {
138-
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
139135
const compiler = getCompiler(
140136
'./nested-import/source.js',
141137
{},
@@ -170,4 +166,40 @@ describe('"importLoaders" option', () => {
170166
expect(getWarnings(stats)).toMatchSnapshot('warnings');
171167
expect(getErrors(stats)).toMatchSnapshot('errors');
172168
});
169+
170+
it('should work with a value equal to ""1"" ("postcss-loader" before)', async () => {
171+
const compiler = getCompiler(
172+
'./nested-import/source.js',
173+
{},
174+
{
175+
module: {
176+
rules: [
177+
{
178+
test: /\.css$/i,
179+
use: [
180+
{
181+
loader: path.resolve(__dirname, '../src'),
182+
options: { importLoaders: '1' },
183+
},
184+
{
185+
loader: 'postcss-loader',
186+
options: { plugins: () => [postcssPresetEnv({ stage: 0 })] },
187+
},
188+
],
189+
},
190+
],
191+
},
192+
}
193+
);
194+
const stats = await compile(compiler);
195+
196+
expect(
197+
getModuleSource('./nested-import/source.css', stats)
198+
).toMatchSnapshot('module');
199+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
200+
'result'
201+
);
202+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
203+
expect(getErrors(stats)).toMatchSnapshot('errors');
204+
});
173205
});

test/validate-options.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ describe('validate options', () => {
7878
failure: ['true'],
7979
},
8080
importLoaders: {
81-
success: [false, 0, 1, 2],
82-
failure: ['1', 2.5],
81+
success: [false, 0, 1, 2, '1'],
82+
failure: [2.5],
8383
},
8484
esModule: {
8585
success: [true, false],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy