Skip to content

Commit 6dedebc

Browse files
committed
feat: add modules.type and deprecate icss option
1 parent 9070ba9 commit 6dedebc

File tree

9 files changed

+155
-20
lines changed

9 files changed

+155
-20
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export default async function loader(content, map, meta) {
5252
const exports = [];
5353

5454
const needUseModulesPlugins = shouldUseModulesPlugins(options);
55+
const needICSSPlugin =
56+
needUseModulesPlugins || options.icss || options.modules.type === 'icss';
5557

5658
if (needUseModulesPlugins) {
5759
plugins.push(...getModulesPlugins(options, this));
@@ -112,7 +114,7 @@ export default async function loader(content, map, meta) {
112114
const icssPluginImports = [];
113115
const icssPluginApi = [];
114116

115-
if (needUseModulesPlugins || options.icss) {
117+
if (needICSSPlugin) {
116118
const icssResolver = this.getResolve({
117119
conditionNames: ['style'],
118120
extensions: [],

src/options.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
"type": "object",
3737
"additionalProperties": false,
3838
"properties": {
39+
"type": {
40+
"description": "Controls the extent to which css-lodaer will process module code ((https://github.com/webpack-contrib/css-loader#icss)",
41+
"enum": ["icss", "full"]
42+
},
3943
"auto": {
4044
"description": "Allows auto enable CSS modules based on filename (https://github.com/webpack-contrib/css-loader#auto).",
4145
"anyOf": [

src/utils.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ function getModulesOptions(rawOptions, loaderContext) {
127127
let modulesOptions = {
128128
auto: true,
129129
mode: 'local',
130+
type: rawOptions.icss ? 'icss' : 'full',
130131
exportGlobals: false,
131132
localIdentName: '[hash:base64]',
132133
localIdentContext: loaderContext.rootContext,
@@ -147,6 +148,12 @@ function getModulesOptions(rawOptions, loaderContext) {
147148
typeof rawOptions.modules === 'string' ? rawOptions.modules : 'local';
148149
} else {
149150
if (rawOptions.modules) {
151+
if ('icss' in rawOptions && rawOptions.modules.type) {
152+
throw new Error(
153+
'The "modules.type" option cannot be set with "options.icss", remove the `icss` option and just use `type`'
154+
);
155+
}
156+
150157
if (typeof rawOptions.modules.auto === 'boolean') {
151158
const isModules =
152159
rawOptions.modules.auto && moduleRegExp.test(resourcePath);
@@ -202,11 +209,19 @@ function getModulesOptions(rawOptions, loaderContext) {
202209

203210
function normalizeOptions(rawOptions, loaderContext) {
204211
const modulesOptions = getModulesOptions(rawOptions, loaderContext);
212+
213+
if ('icss' in rawOptions) {
214+
loaderContext.emitWarning(
215+
new Error(
216+
'The `icss` option is deprecated, use modules.type: "icss" instead'
217+
)
218+
);
219+
}
220+
205221
return {
206222
url: typeof rawOptions.url === 'undefined' ? true : rawOptions.url,
207223
import: typeof rawOptions.import === 'undefined' ? true : rawOptions.import,
208224
modules: modulesOptions,
209-
icss: modulesOptions ? true : rawOptions.icss,
210225
sourceMap:
211226
typeof rawOptions.sourceMap === 'boolean'
212227
? rawOptions.sourceMap
@@ -242,6 +257,9 @@ function shouldUseURLPlugin(options) {
242257
}
243258

244259
function shouldUseModulesPlugins(options) {
260+
if (options.modules && options.modules.type === 'icss') {
261+
return false;
262+
}
245263
return Boolean(options.modules);
246264
}
247265

test/__snapshots__/icss.test.js.snap

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ exports[`ICSS show work with the case "export": module 1`] = `
117117
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../../src/runtime/api.js\\";
118118
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
119119
// Module
120-
___CSS_LOADER_EXPORT___.push([module.id, \\"\\\\n\\", \\"\\"]);
120+
___CSS_LOADER_EXPORT___.push([module.id, \\".other {\\\\n a: a;\\\\n}\\\\n\\", \\"\\"]);
121121
// Exports
122122
___CSS_LOADER_EXPORT___.locals = {
123123
\\"_test\\": \\"_test\\"
@@ -130,7 +130,9 @@ exports[`ICSS show work with the case "export": result 1`] = `
130130
Array [
131131
Array [
132132
"./icss/tests-cases/export/source.css",
133-
"
133+
".other {
134+
a: a;
135+
}
134136
",
135137
"",
136138
],
@@ -169,6 +171,35 @@ Array [
169171

170172
exports[`ICSS show work with the case "export-reserved-keywords": warnings 1`] = `Array []`;
171173

174+
exports[`ICSS show work with the case "exports-only": errors 1`] = `Array []`;
175+
176+
exports[`ICSS show work with the case "exports-only": module 1`] = `
177+
"// Imports
178+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../../src/runtime/api.js\\";
179+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(false);
180+
// Module
181+
___CSS_LOADER_EXPORT___.push([module.id, \\"\\\\n\\", \\"\\"]);
182+
// Exports
183+
___CSS_LOADER_EXPORT___.locals = {
184+
\\"_test\\": \\"_test\\"
185+
};
186+
export default ___CSS_LOADER_EXPORT___;
187+
"
188+
`;
189+
190+
exports[`ICSS show work with the case "exports-only": result 1`] = `
191+
Array [
192+
Array [
193+
"./icss/tests-cases/exports-only/source.css",
194+
"
195+
",
196+
"",
197+
],
198+
]
199+
`;
200+
201+
exports[`ICSS show work with the case "exports-only": warnings 1`] = `Array []`;
202+
172203
exports[`ICSS show work with the case "import": errors 1`] = `Array []`;
173204

174205
exports[`ICSS show work with the case "import": module 1`] = `
@@ -311,3 +342,25 @@ Array [
311342
`;
312343

313344
exports[`ICSS show work with the case "multiple-keys-values-in-export": warnings 1`] = `Array []`;
345+
346+
exports[`ICSS work with exports only: errors 1`] = `Array []`;
347+
348+
exports[`ICSS work with exports only: module 1`] = `
349+
"var ___CSS_LOADER_EXPORT___ = {};
350+
// Exports
351+
___CSS_LOADER_EXPORT___.locals = {
352+
\\"_test\\": \\"_test\\"
353+
};
354+
export default ___CSS_LOADER_EXPORT___;
355+
"
356+
`;
357+
358+
exports[`ICSS work with exports only: result 1`] = `
359+
Object {
360+
"locals": Object {
361+
"_test": "_test",
362+
},
363+
}
364+
`;
365+
366+
exports[`ICSS work with exports only: warnings 1`] = `Array []`;

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ exports[`validate options should throw an error on the "importLoaders" option wi
5151
exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = `
5252
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
5353
- options.modules should be one of these:
54-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
54+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
5555
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
5656
Details:
5757
* options.modules.auto should be one of these:
@@ -109,7 +109,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
109109
exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = `
110110
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
111111
- options.modules should be one of these:
112-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
112+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
113113
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
114114
Details:
115115
* options.modules.localIdentRegExp should be one of these:
@@ -123,7 +123,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
123123
exports[`validate options should throw an error on the "modules" option with "{"mode":"globals"}" value 1`] = `
124124
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
125125
- options.modules should be one of these:
126-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
126+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
127127
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
128128
Details:
129129
* options.modules.mode should be one of these:
@@ -138,7 +138,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
138138
exports[`validate options should throw an error on the "modules" option with "{"mode":"locals"}" value 1`] = `
139139
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
140140
- options.modules should be one of these:
141-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
141+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
142142
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
143143
Details:
144144
* options.modules.mode should be one of these:
@@ -153,7 +153,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
153153
exports[`validate options should throw an error on the "modules" option with "{"mode":"pures"}" value 1`] = `
154154
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
155155
- options.modules should be one of these:
156-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
156+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
157157
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
158158
Details:
159159
* options.modules.mode should be one of these:
@@ -168,7 +168,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
168168
exports[`validate options should throw an error on the "modules" option with "{"mode":true}" value 1`] = `
169169
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
170170
- options.modules should be one of these:
171-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
171+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
172172
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
173173
Details:
174174
* options.modules.mode should be one of these:
@@ -189,53 +189,53 @@ exports[`validate options should throw an error on the "modules" option with "{"
189189
exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = `
190190
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
191191
- options.modules should be one of these:
192-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
192+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
193193
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
194194
Details:
195195
* options.modules should be a boolean.
196196
* options.modules should be one of these:
197197
\\"local\\" | \\"global\\" | \\"pure\\"
198198
* options.modules should be an object:
199-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
199+
object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
200200
`;
201201
202202
exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = `
203203
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
204204
- options.modules should be one of these:
205-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
205+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
206206
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
207207
Details:
208208
* options.modules should be a boolean.
209209
* options.modules should be one of these:
210210
\\"local\\" | \\"global\\" | \\"pure\\"
211211
* options.modules should be an object:
212-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
212+
object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
213213
`;
214214
215215
exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = `
216216
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
217217
- options.modules should be one of these:
218-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
218+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
219219
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
220220
Details:
221221
* options.modules should be a boolean.
222222
* options.modules should be one of these:
223223
\\"local\\" | \\"global\\" | \\"pure\\"
224224
* options.modules should be an object:
225-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
225+
object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
226226
`;
227227
228228
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
229229
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
230230
- options.modules should be one of these:
231-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
231+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
232232
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
233233
Details:
234234
* options.modules should be a boolean.
235235
* options.modules should be one of these:
236236
\\"local\\" | \\"global\\" | \\"pure\\"
237237
* options.modules should be an object:
238-
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
238+
object { type?, auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
239239
`;
240240
241241
exports[`validate options should throw an error on the "sourceMap" option with "true" value 1`] = `
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.other {
2+
a: a;
3+
}
4+
15
:export {
26
_test: _test
37
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:export {
2+
_test: _test
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './source.css';
2+
3+
__export__ = css;
4+
5+
export default css;

test/icss.test.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ 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: false,
21-
icss: true,
20+
modules: { type: 'icss' },
2221
});
2322
const stats = await compile(compiler);
2423

@@ -32,4 +31,51 @@ describe('ICSS', () => {
3231
expect(getErrors(stats)).toMatchSnapshot('errors');
3332
});
3433
});
34+
35+
it(`show warn about icss deprecation`, async () => {
36+
const stats = await compile(
37+
getCompiler(`./icss/tests-cases/import/source.js`, {
38+
modules: false,
39+
icss: true,
40+
})
41+
);
42+
const warnings = getWarnings(stats);
43+
expect(warnings).toHaveLength(1);
44+
expect(warnings[0]).toContain(
45+
'The `icss` option is deprecated, use modules.type: "icss" instead'
46+
);
47+
});
48+
49+
it(`show throw when both options are specified`, async () => {
50+
const stats = await compile(
51+
getCompiler(`./icss/tests-cases/import/source.js`, {
52+
modules: { type: 'full' },
53+
icss: true,
54+
})
55+
);
56+
const errors = getErrors(stats);
57+
expect(errors).toHaveLength(1);
58+
expect(errors[0]).toContain(
59+
'The "modules.type" option cannot be set with "options.icss", remove the `icss` option and just use `type`'
60+
);
61+
});
62+
63+
it(`work with exports only`, async () => {
64+
const compiler = getCompiler(`./icss/tests-cases/exports-only/source.js`, {
65+
modules: {
66+
type: 'icss',
67+
exportOnlyLocals: true,
68+
},
69+
});
70+
const stats = await compile(compiler);
71+
72+
expect(
73+
getModuleSource(`./icss/tests-cases/exports-only/source.css`, stats)
74+
).toMatchSnapshot('module');
75+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
76+
'result'
77+
);
78+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
79+
expect(getErrors(stats)).toMatchSnapshot('errors');
80+
});
3581
});

0 commit comments

Comments
 (0)