Skip to content

Commit a8ec7da

Browse files
authored
feat: add icss option (#1140)
1 parent bc7b18d commit a8ec7da

7 files changed

+61
-11
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,33 @@ module.exports = {
10001000
};
10011001
```
10021002

1003+
### `icss`
1004+
1005+
Type: Boolean Default: `true` if `modules` are enabled, false otherwise
1006+
1007+
Enables/disables handling of the low level "Interoperable CSS" format for declaring
1008+
import and export dependencies between CSS and other languages. ICSS enables
1009+
CSS Module support, and is enabled automatically when `modules` are enabled. It
1010+
can also be enabled independently to allow other loaders to handle processing CSS modules.
1011+
1012+
**webpack.config.js**
1013+
1014+
```js
1015+
module.exports = {
1016+
module: {
1017+
rules: [
1018+
{
1019+
test: /\.css$/i,
1020+
loader: 'css-loader',
1021+
options: {
1022+
icss: true,
1023+
},
1024+
},
1025+
],
1026+
},
1027+
};
1028+
```
1029+
10031030
### `sourceMap`
10041031

10051032
Type: `Boolean`

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default async function loader(content, map, meta) {
111111
const icssPluginImports = [];
112112
const icssPluginApi = [];
113113

114-
if (needUseModulesPlugins) {
114+
if (needUseModulesPlugins || options.icss) {
115115
const icssResolver = this.getResolve({
116116
mainFields: ['css', 'style', 'main', '...'],
117117
mainFiles: ['index', '...'],

src/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@
118118
}
119119
]
120120
},
121+
"icss": {
122+
"description": "Enables/Disables handling the CSS module interoperable import/export format ((https://github.com/webpack-contrib/css-loader#icss)",
123+
"type": "boolean"
124+
},
121125
"sourceMap": {
122126
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
123127
"type": "boolean"

src/utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@ function getModulesOptions(rawOptions, loaderContext) {
197197
}
198198

199199
function normalizeOptions(rawOptions, loaderContext) {
200+
const modulesOptions = getModulesOptions(rawOptions, loaderContext);
200201
return {
201202
url: typeof rawOptions.url === 'undefined' ? true : rawOptions.url,
202203
import: typeof rawOptions.import === 'undefined' ? true : rawOptions.import,
203-
modules: getModulesOptions(rawOptions, loaderContext),
204+
modules: modulesOptions,
205+
icss: modulesOptions ? true : rawOptions.icss,
204206
sourceMap:
205207
typeof rawOptions.sourceMap === 'boolean'
206208
? rawOptions.sourceMap

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ exports[`validate options should throw an error on the "esModule" option with "t
66
-> Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule)."
77
`;
88

9+
exports[`validate options should throw an error on the "icss" option with "1" value 1`] = `
10+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
11+
- options.icss should be a boolean.
12+
-> Enables/Disables handling the CSS module interoperable import/export format ((https://github.com/webpack-contrib/css-loader#icss)"
13+
`;
14+
15+
exports[`validate options should throw an error on the "icss" option with "true" value 1`] = `
16+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
17+
- options.icss should be a boolean.
18+
-> Enables/Disables handling the CSS module interoperable import/export format ((https://github.com/webpack-contrib/css-loader#icss)"
19+
`;
20+
921
exports[`validate options should throw an error on the "import" option with "true" value 1`] = `
1022
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
1123
- options.import should be one of these:
@@ -235,49 +247,49 @@ exports[`validate options should throw an error on the "sourceMap" option with "
235247
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
236248
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
237249
- options has an unknown property 'unknown'. These properties are valid:
238-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
250+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
239251
`;
240252
241253
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
242254
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
243255
- options has an unknown property 'unknown'. These properties are valid:
244-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
256+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
245257
`;
246258
247259
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
248260
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
249261
- options has an unknown property 'unknown'. These properties are valid:
250-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
262+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
251263
`;
252264
253265
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
254266
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
255267
- options has an unknown property 'unknown'. These properties are valid:
256-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
268+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
257269
`;
258270
259271
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
260272
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
261273
- options has an unknown property 'unknown'. These properties are valid:
262-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
274+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
263275
`;
264276
265277
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
266278
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
267279
- options has an unknown property 'unknown'. These properties are valid:
268-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
280+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
269281
`;
270282
271283
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
272284
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
273285
- options has an unknown property 'unknown'. These properties are valid:
274-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
286+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
275287
`;
276288
277289
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
278290
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
279291
- options has an unknown property 'unknown'. These properties are valid:
280-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
292+
object { url?, import?, modules?, icss?, sourceMap?, importLoaders?, esModule? }"
281293
`;
282294
283295
exports[`validate options should throw an error on the "url" option with "true" value 1`] = `

test/icss.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ describe('ICSS', () => {
1717
testCases.forEach((name) => {
1818
it(`show work with the case "${name}"`, async () => {
1919
const compiler = getCompiler(`./icss/tests-cases/${name}/source.js`, {
20-
modules: 'global',
20+
modules: false,
21+
icss: true,
2122
});
2223
const stats = await compile(compiler);
2324

test/validate-options.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ describe('validate options', () => {
1010
success: [true, false, () => {}],
1111
failure: ['true'],
1212
},
13+
icss: {
14+
success: [true, false],
15+
failure: ['true', 1],
16+
},
1317
modules: {
1418
success: [
1519
true,

0 commit comments

Comments
 (0)