2020-10-21 12:31:07 +02:00
|
|
|
import { AcceptedPlugin } from 'postcss'
|
2021-01-14 02:59:08 +01:00
|
|
|
import { webpack } from 'next/dist/compiled/webpack/webpack'
|
2020-01-22 16:50:27 +01:00
|
|
|
import { ConfigurationContext } from '../../../utils'
|
|
|
|
import { getClientStyleLoader } from './client'
|
2020-10-08 20:45:39 +02:00
|
|
|
import { cssFileResolve } from './file-resolve'
|
2020-01-22 16:50:27 +01:00
|
|
|
import { getCssModuleLocalIdent } from './getCssModuleLocalIdent'
|
|
|
|
|
|
|
|
export function getCssModuleLoader(
|
|
|
|
ctx: ConfigurationContext,
|
2020-10-21 12:31:07 +02:00
|
|
|
postCssPlugins: readonly AcceptedPlugin[],
|
2020-01-28 22:31:38 +01:00
|
|
|
preProcessors: readonly webpack.RuleSetUseItem[] = []
|
2020-01-22 16:50:27 +01:00
|
|
|
): webpack.RuleSetUseItem[] {
|
|
|
|
const loaders: webpack.RuleSetUseItem[] = []
|
|
|
|
|
|
|
|
if (ctx.isClient) {
|
|
|
|
// Add appropriate development more or production mode style
|
|
|
|
// loader
|
|
|
|
loaders.push(
|
|
|
|
getClientStyleLoader({
|
|
|
|
isDevelopment: ctx.isDevelopment,
|
|
|
|
assetPrefix: ctx.assetPrefix,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve CSS `@import`s and `url()`s
|
|
|
|
loaders.push({
|
2021-01-20 06:47:25 +01:00
|
|
|
loader: require.resolve('next/dist/compiled/css-loader'),
|
2020-01-22 16:50:27 +01:00
|
|
|
options: {
|
2020-01-22 21:26:51 +01:00
|
|
|
importLoaders: 1 + preProcessors.length,
|
2020-01-22 16:50:27 +01:00
|
|
|
sourceMap: true,
|
2020-09-10 01:29:37 +02:00
|
|
|
// Use CJS mode for backwards compatibility:
|
|
|
|
esModule: false,
|
2020-10-08 20:45:39 +02:00
|
|
|
url: cssFileResolve,
|
|
|
|
import: (url: string, _: any, resourcePath: string) =>
|
|
|
|
cssFileResolve(url, resourcePath),
|
2020-01-22 16:50:27 +01:00
|
|
|
modules: {
|
2020-09-10 01:29:37 +02:00
|
|
|
// Do not transform class names (CJS mode backwards compatibility):
|
|
|
|
exportLocalsConvention: 'asIs',
|
|
|
|
// Server-side (Node.js) rendering support:
|
|
|
|
exportOnlyLocals: ctx.isServer,
|
2020-01-22 16:50:27 +01:00
|
|
|
// Disallow global style exports so we can code-split CSS and
|
|
|
|
// not worry about loading order.
|
|
|
|
mode: 'pure',
|
|
|
|
// Generate a friendly production-ready name so it's
|
|
|
|
// reasonably understandable. The same name is used for
|
|
|
|
// development.
|
|
|
|
// TODO: Consider making production reduce this to a single
|
|
|
|
// character?
|
|
|
|
getLocalIdent: getCssModuleLocalIdent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Compile CSS
|
|
|
|
loaders.push({
|
2020-03-30 04:01:30 +02:00
|
|
|
loader: require.resolve('next/dist/compiled/postcss-loader'),
|
2020-01-22 16:50:27 +01:00
|
|
|
options: {
|
2020-10-19 17:07:59 +02:00
|
|
|
postcssOptions: { plugins: postCssPlugins, config: false },
|
2020-01-22 16:50:27 +01:00
|
|
|
sourceMap: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-01-22 21:26:51 +01:00
|
|
|
loaders.push(
|
|
|
|
// Webpack loaders run like a stack, so we need to reverse the natural
|
|
|
|
// order of preprocessors.
|
2020-01-28 22:31:38 +01:00
|
|
|
...preProcessors.slice().reverse()
|
2020-01-22 21:26:51 +01:00
|
|
|
)
|
|
|
|
|
2020-01-22 16:50:27 +01:00
|
|
|
return loaders
|
|
|
|
}
|