2020-01-22 16:50:27 +01:00
|
|
|
import postcss from 'postcss'
|
|
|
|
import webpack from 'webpack'
|
|
|
|
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
|
|
|
|
|
|
|
export function getGlobalCssLoader(
|
|
|
|
ctx: ConfigurationContext,
|
2020-01-28 22:31:38 +01:00
|
|
|
postCssPlugins: readonly postcss.AcceptedPlugin[],
|
|
|
|
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({
|
|
|
|
loader: require.resolve('css-loader'),
|
2020-09-10 01:29:37 +02:00
|
|
|
options: {
|
|
|
|
importLoaders: 1 + preProcessors.length,
|
|
|
|
sourceMap: true,
|
|
|
|
// Next.js controls CSS Modules eligibility:
|
|
|
|
modules: false,
|
2020-10-08 20:45:39 +02:00
|
|
|
url: cssFileResolve,
|
|
|
|
import: (url: string, _: any, resourcePath: string) =>
|
|
|
|
cssFileResolve(url, resourcePath),
|
2020-09-10 01:29:37 +02:00
|
|
|
},
|
2020-01-22 16:50:27 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|