Skip to content
This repository was archived by the owner on Mar 16, 2026. It is now read-only.

Commit d87de01

Browse files
phamtmpedronauck
authored andcommitted
chore: smartly decide when to use CSS modules (#6)
1 parent efe85b4 commit d87de01

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Example of Button component with custom class!
9898

9999
If you don't pass `.module` in front of the preprocessor extension, bundler will don't parse your css as cssmodule!
100100

101+
If in your project some places use both CSS modules and some place doesn't, you can leave out the `cssmodules` option so that `webpack` can determined by itself the correct way to load the CSS.
102+
101103
### Multiple pre-processor
102104

103105
You can still use multiple pre-processor together in the same configuration:

src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,22 @@ const loaders = {
8888

8989
const applyRule = (
9090
opts: CSSPluginOptions,
91-
cssmodules: boolean | undefined,
91+
cssmodules: boolean | undefined, // if cssmodules === undefined, then let webpack decide whether to use CSS modules by itself
9292
dev: boolean
9393
) => {
9494
const { preprocessor, cssOpts, loaderOpts, ruleOpts } = opts
9595

9696
const loaderfn = loaders[preprocessor as PreProcessor]
9797
const loader = loaderfn(loaderOpts)
98-
const cssoptions = merge(cssOpts, {
99-
importLoaders: 1,
100-
modules: cssmodules,
101-
sourceMap: !dev,
102-
...(cssmodules && { getLocalIdent }),
103-
})
98+
const cssoptions = merge(
99+
cssOpts,
100+
{
101+
importLoaders: 1,
102+
sourceMap: !dev,
103+
...(cssmodules && { getLocalIdent }),
104+
},
105+
typeof cssmodules === 'boolean' ? { modules: cssmodules } : {}
106+
)
104107

105108
return {
106109
test: tests[preprocessor as PreProcessor],
@@ -119,7 +122,7 @@ export interface CSSPluginOptions {
119122

120123
const defaultOpts: Record<string, any> = {
121124
preprocessor: 'postcss',
122-
cssmodules: false,
125+
cssmodules: undefined,
123126
loadersOpts: {},
124127
cssOpts: {},
125128
ruleOpts: {},

0 commit comments

Comments
 (0)