-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed css modules in prod build (#5)
* Fixed css modules in prod build * News item
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed css modules in prod build @pnicolli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const path = require('path'); | ||
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder'); | ||
const interpolateName = require('loader-utils').interpolateName; | ||
|
||
function normalizePath(file) { | ||
return path.sep === '\\' ? file.replace(/\\/g, '/') : file; | ||
} | ||
|
||
// Custom function to not use 'loaderContext._module.matchResource' in hashing CSS class name. | ||
function getLocalIdent(loaderContext, localIdentName, localName, options) { | ||
const relativeResourcePath = normalizePath( | ||
path.relative(options.context, loaderContext.resourcePath), | ||
); | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
options.content = `${options.hashPrefix}${relativeResourcePath}\x00${localName}`; | ||
|
||
return interpolateName(loaderContext, localIdentName, options); | ||
} | ||
|
||
module.exports = { | ||
plugins: (plugs) => plugs, | ||
modify: function modifyWebpackConfig( | ||
defaultConfig, | ||
{ target, dev }, | ||
webpackObject, | ||
) { | ||
const config = Object.assign({}, defaultConfig); | ||
|
||
const cssLoaderFinder = makeLoaderFinder('css-loader'); | ||
const cssLoader = config.module.rules.find(cssLoaderFinder); | ||
|
||
if (!dev && cssLoader) { | ||
const loader = cssLoader.use.find( | ||
(u) => typeof u !== 'string' && u.ident === 'razzle-css-loader', | ||
); | ||
if (loader) { | ||
loader.options.modules.getLocalIdent = getLocalIdent; | ||
} | ||
} | ||
|
||
const scssLoaderFinder = makeLoaderFinder('sass-loader'); | ||
const scssLoader = config.module.rules.find(scssLoaderFinder); | ||
|
||
if (!dev && scssLoader) { | ||
const loader = scssLoader.use.find( | ||
(u) => typeof u !== 'string' && u.loader.match(/\/css-loader\//), | ||
); | ||
if (loader) { | ||
loader.options.modules.getLocalIdent = getLocalIdent; | ||
} | ||
} | ||
|
||
return config; | ||
}, | ||
}; |