Skip to content

Smartly decide when to use CSS modules #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -98,6 +98,8 @@ Example of Button component with custom class!

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

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.

### Multiple pre-processor

You can still use multiple pre-processor together in the same configuration:
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -88,19 +88,22 @@ const loaders = {

const applyRule = (
opts: CSSPluginOptions,
cssmodules: boolean | undefined,
cssmodules: boolean | undefined, // if cssmodules === undefined, then let webpack decide whether to use CSS modules by itself
dev: boolean
) => {
const { preprocessor, cssOpts, loaderOpts } = opts

const loaderfn = loaders[preprocessor as PreProcessor]
const loader = loaderfn(loaderOpts)
const cssoptions = merge(cssOpts, {
importLoaders: 1,
modules: cssmodules,
sourceMap: !dev,
...(cssmodules && { getLocalIdent }),
})
const cssoptions = merge(
cssOpts,
{
importLoaders: 1,
sourceMap: !dev,
...(cssmodules && { getLocalIdent }),
},
typeof cssmodules === 'boolean' ? { modules: cssmodules } : {}
)

return {
test: tests[preprocessor as PreProcessor],
@@ -117,7 +120,7 @@ export interface CSSPluginOptions {

const defaultOpts: Record<string, any> = {
preprocessor: 'postcss',
cssmodules: false,
cssmodules: undefined,
loadersOpts: {},
cssOpts: {},
}