Skip to content

Commit

Permalink
Merge pull request #13 from bem/yarastqt.fix-css-modules
Browse files Browse the repository at this point in the history
Allow css-modules
  • Loading branch information
yarastqt authored Feb 25, 2022
2 parents 8b32c6b + f94688b commit 7bb2404
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const regexCssGlobal = /(?<!\.module)\.css$/
const regexScssGlobal = /(?<!\.module)\.(scss|sass)$/
const globalCssRe = [/(?<!\.module)\.css$/, /(?<!\.module)\.(scss|sass)$/]
const globalCssModulesRe = [/\.module\.css$/, /\.module\.(scss|sass)$/]

module.exports = { regexCssGlobal, regexScssGlobal }
module.exports = { globalCssRe, globalCssModulesRe }
29 changes: 16 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { regexCssGlobal, regexScssGlobal } = require('./constants')
const { globalCssRe, globalCssModulesRe } = require('./constants')

function patchServerWebpackConfig(config) {
const originalEntry = config.entry
Expand All @@ -16,34 +16,37 @@ function patchServerWebpackConfig(config) {
}
}

function patchClientWebpackConfig(config, options) {
function patchCommonWebpackConfig(config) {
for (const rule of config.module.rules) {
if (rule.oneOf) {
for (const oneOfRule of rule.oneOf) {
patchWebpackStyleRules(oneOfRule, options)
patchWebpackStyleRules(oneOfRule)
}
} else {
patchWebpackStyleRules(rule, options)
patchWebpackStyleRules(rule)
}
}
}

function patchWebpackStyleRules(rule) {
if (
rule.test &&
(rule.test.source === regexCssGlobal.source || rule.test.source === regexScssGlobal.source)
) {
// Remove issuer for allow import css from 3d-party libs and locals.
delete rule.issuer
function patchWebpackStyleRules(rawRule) {
const rules = Array.isArray(rawRule) ? rawRule : [rawRule]
const cssRe = [...globalCssRe, ...globalCssModulesRe]

for (const rule of rules) {
if (rule.test && cssRe.some((reg) => reg.source === rule.test.source)) {
// Remove issuer for allow import css from 3d-party libs and locals.
delete rule.issuer
}
}
}

function patchWebpackConfig(config, { isServer }) {
if (isServer) {
patchServerWebpackConfig(config)
} else {
patchClientWebpackConfig(config)
}

patchCommonWebpackConfig(config)

return config
}

Expand Down
4 changes: 2 additions & 2 deletions lib/patch-global-require.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Module = require('module')

const { regexCssGlobal, regexScssGlobal } = require('./constants')
const { globalCssRe } = require('./constants')

Module.prototype.require = new Proxy(Module.prototype.require, {
apply(target, thisArg, args) {
if (regexCssGlobal.test(args[0]) || regexScssGlobal.test(args[0])) {
if (globalCssRe.some((reg) => reg.test(args[0]))) {
return ''
}
return Reflect.apply(target, thisArg, args)
Expand Down
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = withConfig({
```

**If your webpack configuration is already customized:**

```js
const { patchWebpackConfig } = require('next-global-css')

Expand All @@ -43,6 +44,29 @@ const nextConfig = {
}
```

**Allow css-modules from node-modules:**

```js
const { patchWebpackConfig } = require('next-global-css')
const webpackNodeExternals = require('webpack-node-externals')

module.exports = {
reactStrictMode: true,
webpack: (config, options) => {
patchWebpackConfig(config, options)

if (options.isServer) {
config.externals = webpackNodeExternals({
// Uses list to add this modules for server bundle and process.
allowlist: [/design-system/],
})
}

return config
},
}
```

## 📜 License

Project is [MIT licensed](https://github.com/yarastqt/next-global-css/blob/master/license.md).
Expand Down

0 comments on commit 7bb2404

Please sign in to comment.