Skip to content

Commit

Permalink
chore: smartly decide when to use CSS modules (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
phamtm authored and pedronauck committed Dec 4, 2018
1 parent efe85b4 commit d87de01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, ruleOpts } = 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 } : {}

This comment has been minimized.

Copy link
@clock157

clock157 Jan 19, 2019

Contributor

cssmodules not work, because of deepmerge only accept two object, the third param is options, see: https://www.npmjs.com/package/deepmerge

@pedronauck

)

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

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

0 comments on commit d87de01

Please sign in to comment.