2020-06-29 23:29:53 +02:00
|
|
|
import { process as minify } from 'cssnano-simple'
|
2019-09-18 19:59:46 +02:00
|
|
|
import webpack from 'webpack'
|
2020-08-22 14:16:13 +02:00
|
|
|
import sources from 'webpack-sources'
|
|
|
|
|
|
|
|
// @ts-ignore: TODO: remove ignore when webpack 5 is stable
|
|
|
|
const { RawSource, SourceMapSource } = webpack.sources || sources
|
2019-09-18 19:59:46 +02:00
|
|
|
|
|
|
|
// https://github.com/NMFR/optimize-css-assets-webpack-plugin/blob/0a410a9bf28c7b0e81a3470a13748e68ca2f50aa/src/index.js#L20
|
|
|
|
const CSS_REGEX = /\.css(\?.*)?$/i
|
|
|
|
|
|
|
|
type CssMinimizerPluginOptions = {
|
|
|
|
postcssOptions: {
|
|
|
|
map: false | { prev?: string | false; inline: boolean; annotation: boolean }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:59:44 +02:00
|
|
|
const isWebpack5 = parseInt(webpack.version!) === 5
|
|
|
|
|
2019-09-18 19:59:46 +02:00
|
|
|
export class CssMinimizerPlugin {
|
2019-12-13 21:23:28 +01:00
|
|
|
__next_css_remove = true
|
|
|
|
|
2019-09-18 19:59:46 +02:00
|
|
|
private options: CssMinimizerPluginOptions
|
|
|
|
|
|
|
|
constructor(options: CssMinimizerPluginOptions) {
|
|
|
|
this.options = options
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:59:44 +02:00
|
|
|
optimizeAsset(file: string, asset: any) {
|
|
|
|
const postcssOptions = {
|
|
|
|
...this.options.postcssOptions,
|
|
|
|
to: file,
|
|
|
|
from: file,
|
|
|
|
}
|
|
|
|
|
|
|
|
let input: string
|
|
|
|
if (postcssOptions.map && asset.sourceAndMap) {
|
|
|
|
const { source, map } = asset.sourceAndMap()
|
|
|
|
input = source
|
|
|
|
postcssOptions.map.prev = map ? map : false
|
|
|
|
} else {
|
|
|
|
input = asset.source()
|
|
|
|
}
|
|
|
|
|
|
|
|
return minify(input, postcssOptions).then((res) => {
|
|
|
|
if (res.map) {
|
|
|
|
return new SourceMapSource(res.css, file, res.map.toJSON())
|
|
|
|
} else {
|
|
|
|
return new RawSource(res.css)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-18 19:59:46 +02:00
|
|
|
apply(compiler: webpack.Compiler) {
|
2020-07-15 19:56:27 +02:00
|
|
|
compiler.hooks.compilation.tap('CssMinimizerPlugin', (compilation: any) => {
|
2020-08-17 12:59:44 +02:00
|
|
|
if (isWebpack5) {
|
2020-08-22 14:16:13 +02:00
|
|
|
const cache = compilation.getCache('CssMinimizerPlugin')
|
2020-08-17 12:59:44 +02:00
|
|
|
compilation.hooks.processAssets.tapPromise(
|
|
|
|
{
|
|
|
|
name: 'CssMinimizerPlugin',
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
|
|
},
|
|
|
|
async (assets: any) => {
|
|
|
|
const files = Object.keys(assets)
|
|
|
|
await Promise.all(
|
|
|
|
files
|
|
|
|
.filter((file) => CSS_REGEX.test(file))
|
|
|
|
.map(async (file) => {
|
2020-08-22 14:16:13 +02:00
|
|
|
const asset = assets[file]
|
|
|
|
|
|
|
|
const etag = cache.getLazyHashedEtag(asset)
|
|
|
|
|
|
|
|
const cachedResult = await cache.getPromise(file, etag)
|
|
|
|
if (cachedResult) {
|
|
|
|
assets[file] = cachedResult
|
|
|
|
return
|
|
|
|
}
|
2020-08-17 12:59:44 +02:00
|
|
|
|
2020-08-22 14:16:13 +02:00
|
|
|
const result = await this.optimizeAsset(file, asset)
|
|
|
|
await cache.storePromise(file, etag, result)
|
|
|
|
assets[file] = result
|
2020-08-17 12:59:44 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2019-09-18 19:59:46 +02:00
|
|
|
compilation.hooks.optimizeChunkAssets.tapPromise(
|
|
|
|
'CssMinimizerPlugin',
|
2020-07-15 19:56:27 +02:00
|
|
|
(chunks: webpack.compilation.Chunk[]) =>
|
2019-09-18 19:59:46 +02:00
|
|
|
Promise.all(
|
|
|
|
chunks
|
|
|
|
.reduce(
|
|
|
|
(acc, chunk) => acc.concat(chunk.files || []),
|
|
|
|
[] as string[]
|
|
|
|
)
|
2020-05-18 21:24:37 +02:00
|
|
|
.filter((entry) => CSS_REGEX.test(entry))
|
2020-08-17 12:59:44 +02:00
|
|
|
.map(async (file) => {
|
2019-09-18 19:59:46 +02:00
|
|
|
const asset = compilation.assets[file]
|
|
|
|
|
2020-08-17 12:59:44 +02:00
|
|
|
compilation.assets[file] = await this.optimizeAsset(file, asset)
|
2019-09-18 19:59:46 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|