更新
This commit is contained in:
+241
@@ -0,0 +1,241 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
### [5.0.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v5.0.0...v5.0.1) (2023-06-13)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* improve perf ([#235](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/235)) ([959eb89](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/959eb89aa1e37df88d2decbe6f40c096f10a3745))
|
||||
|
||||
## [5.0.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v4.2.2...v5.0.0) (2023-03-27)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* update `cssnano` to v6 (migration guide - https://github.com/cssnano/cssnano/releases/tag/cssnano%406.0.0) (#224)
|
||||
|
||||
### Features
|
||||
|
||||
* update `cssnano` to v6 ([#224](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/224)) ([cfcae0c](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/cfcae0c9e2b7afd740774104a8e5eb675fe984db))
|
||||
|
||||
### [4.2.2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v4.2.1...v4.2.2) (2022-10-13)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* handle `swc` errors ([#202](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/202)) ([b1ce195](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/b1ce195a0de508217ea1a91d7f8e2683e77b495a))
|
||||
|
||||
### [4.2.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v4.2.0...v4.2.1) (2022-10-06)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* crash ([#200](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/200)) ([c50b2b2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/c50b2b25348a78a6bfaff248e0fb356a70a4241c))
|
||||
|
||||
## [4.2.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v4.1.0...v4.2.0) (2022-09-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* added `swc` minimizer ([#197](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/197)) ([5461421](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/546142104cb30e4711ba800170961822de7d0097))
|
||||
|
||||
## [4.1.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v4.0.0...v4.1.0) (2022-09-09)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* added `lightningcss` support ([#192](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/192)) ([04a3347](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/04a3347610a0044116bfebfd3f3be50d3ada4e04))
|
||||
|
||||
## [4.0.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.4.1...v4.0.0) (2022-05-18)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* minimum supported `Node.js` version is `14.15.0`
|
||||
|
||||
### [3.4.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.4.0...v3.4.1) (2022-01-18)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* types ([9c8b0f3](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/9c8b0f35cc6fc8816c387c48c535d71891d8aa65))
|
||||
|
||||
## [3.4.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.3.1...v3.4.0) (2022-01-18)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* added `@parcel/css` support ([#154](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/154)) ([5e5fe51](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/5e5fe512c7209f0ad4c7f9c55f47cf9763adfc74))
|
||||
|
||||
### [3.3.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.3.0...v3.3.1) (2021-12-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* cssnano types ([#147](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/147)) ([a51026e](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/a51026e3b948fa14d2334d823b3bbb6ea482c57b))
|
||||
|
||||
## [3.3.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.2.0...v3.3.0) (2021-12-16)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* removed cjs wrapper and generated types in commonjs format (`export =` and `namespaces` used in types), now you can directly use exported types ([3262a9a](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/3262a9aee2c89e6aed00c4f76443e0d1e07489f0))
|
||||
|
||||
## [3.2.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.1.4...v3.2.0) (2021-11-23)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* added types ([#142](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/142)) ([fb91610](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/fb91610730166b8fb4fb09cee5da781f0ec98e70))
|
||||
|
||||
### [3.1.4](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.1.3...v3.1.4) (2021-11-17)
|
||||
|
||||
|
||||
### Chore
|
||||
|
||||
* update `schema-utils` package to `4.0.0` version
|
||||
|
||||
### [3.1.3](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.1.2...v3.1.3) (2021-11-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* source map generation for `cssnano` and `clean-css` ([#135](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/135)) ([a9dd43e](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/a9dd43e1a7284bb713e19eac6fa3f3724fb9d48b))
|
||||
|
||||
### [3.1.2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.1.1...v3.1.2) (2021-11-08)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* handle `esbuild` warnings ([f427f41](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/f427f41d319b340262c9b5061cf5427bdf5a5f78))
|
||||
|
||||
### [3.1.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.1.0...v3.1.1) (2021-10-05)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* minified result can be empty ([#127](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/127)) ([0a8ad71](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/0a8ad71fa0da86c5f76381f13ddf013db02a60ea))
|
||||
|
||||
## [3.1.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.0.2...v3.1.0) (2021-10-04)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* added `esbuild` minimizer ([#122](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/122)) ([987d454](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/987d45402af81d88fbd1489021f8d14e5ff26a74))
|
||||
* allow returning errors from custom minimize function ([#121](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/121)) ([c9a11b2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/c9a11b2349d24206bc03cf3670f50a822ef7a52b))
|
||||
* output documentation links on errors ([4e8afba](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/4e8afbae5c89b0fe52b83b69fb2c5ab8a6382750))
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* source map generation for multiple `minify` functions ([b736099](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/b7360993a50a4d998bb92f0a3d491030452fcb0b))
|
||||
|
||||
### [3.0.2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.0.1...v3.0.2) (2021-06-25)
|
||||
|
||||
### Chore
|
||||
|
||||
* update `serialize-javascript`
|
||||
|
||||
### [3.0.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v3.0.0...v3.0.1) (2021-05-31)
|
||||
|
||||
### Chore
|
||||
|
||||
* update `jest-worker`
|
||||
|
||||
## [3.0.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v2.0.0...v3.0.0) (2021-05-12)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* minimum supported `Node.js` version is `12.13.0`
|
||||
|
||||
## [2.0.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.3.0...v2.0.0) (2021-04-10)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* update `cssnano` to `5.0.0` version
|
||||
* drop `webpack` v4 support,
|
||||
* removed the `cache` option (respect the [`cache`](https://webpack.js.org/configuration/other-options/#cache) option from `webpack`)
|
||||
* removed the `cacheKeys` option respect the [`cache`](https://webpack.js.org/configuration/other-options/#cache) option from `webpack`)
|
||||
* removed the sourceMap option (respect the [`devtool`](https://webpack.js.org/configuration/devtool/) option from `webpack`)
|
||||
|
||||
### Features
|
||||
|
||||
* added defaults functions for `clean-css` and `csso`, please look at [here](https://github.com/webpack-contrib/css-minimizer-webpack-plugin#minify) ([5211eed](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/5211eed3c816212a30715ebf0798393c886343d0))
|
||||
* added the ability to pass an array of functions to the minify ([91f9977](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/91f9977e9fdd1447cb45bc6f5d4b56fe7c26fb04))
|
||||
* update `cssnano` to `5.0.0` version ([4d2a8fd](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/4d2a8fd275ab17f6d98e4581b6c2adf09c73c91c))
|
||||
|
||||
## [1.3.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.2.0...v1.3.0) (2021-03-15)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* added support `processorOptions` for `cssnano` ([8865423](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/886542375dc37411e271d151b7060be574fc5898))
|
||||
|
||||
## [1.2.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.1.5...v1.2.0) (2021-01-08)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* optimize CSS assets added later by plugins (webpack@5 only) ([#47](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/47)) ([bdb3f52](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/bdb3f524289a2c131e2ec3728cf10b4c9a0010c7))
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* crash with source maps when the `parallel` option is `false` ([#53](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/53)) ([4fe95f9](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/4fe95f9b63764d06ffc4e6d6d77a45ba2dd1b76b))
|
||||
|
||||
### [1.1.5](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.1.4...v1.1.5) (2020-10-07)
|
||||
|
||||
### Chore
|
||||
|
||||
* update `schema-utils`
|
||||
|
||||
### [1.1.4](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.1.3...v1.1.4) (2020-09-18)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* weak cache
|
||||
* source map generation
|
||||
* cache warnings between builds
|
||||
|
||||
### [1.1.3](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.1.2...v1.1.3) (2020-09-03)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* do not crash on the `minify` option ([fd9abac](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/fd9abac59dc82561ef1db603165526caba6e9abe))
|
||||
|
||||
### [1.1.2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.1.1...v1.1.2) (2020-08-24)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* compatibility with webpack 5 ([6232829](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/62328298d6a81f31e773d11ced8811ee41cd0470))
|
||||
|
||||
### [1.1.1](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.1.0...v1.1.1) (2020-08-10)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* compatibility with `10.13` version of `Node.js` ([d38ea79](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/d38ea793bc2cefc6289d2fe8f2e5df8c31e487e0))
|
||||
|
||||
## [1.1.0](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/compare/v1.0.0...v1.1.0) (2020-08-04)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* show minimized assets in stats for webpack@5 ([#19](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/19)) ([cb038b9](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/cb038b91b15e934a56c260635506df4f02efd747))
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* compatibility cache feature with webpack@5 ([#16](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/16)) ([997e00f](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/997e00f66298219dccfdff8c01c71bebc973df49))
|
||||
* skip double compression for child compilation ([#18](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues/18)) ([ffc71c2](https://github.com/webpack-contrib/css-minimizer-webpack-plugin/commit/ffc71c2c5269ba12c794be87c3257390fdd9c926))
|
||||
|
||||
## 1.0.0 - 2020-08-01
|
||||
|
||||
Initial release
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
Copyright JS Foundation and other contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+624
@@ -0,0 +1,624 @@
|
||||
<div align="center">
|
||||
<a href="https://github.com/webpack/webpack">
|
||||
<img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
[![npm][npm]][npm-url]
|
||||
[![node][node]][node-url]
|
||||
[![tests][tests]][tests-url]
|
||||
[![cover][cover]][cover-url]
|
||||
[![discussion][discussion]][discussion-url]
|
||||
[![size][size]][size-url]
|
||||
|
||||
# css-minimizer-webpack-plugin
|
||||
|
||||
This plugin uses [cssnano](https://cssnano.co) to optimize and minify your CSS.
|
||||
|
||||
Just like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin) but more accurate with source maps and assets using query string, allows caching and works in parallel mode.
|
||||
|
||||
## Getting Started
|
||||
|
||||
To begin, you'll need to install `css-minimizer-webpack-plugin`:
|
||||
|
||||
```console
|
||||
npm install css-minimizer-webpack-plugin --save-dev
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```console
|
||||
yarn add -D css-minimizer-webpack-plugin
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```console
|
||||
pnpm add -D css-minimizer-webpack-plugin
|
||||
```
|
||||
|
||||
Then add the plugin to your `webpack` configuration. For example:
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
||||
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
|
||||
|
||||
module.exports = {
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /.s?css$/,
|
||||
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
|
||||
},
|
||||
],
|
||||
},
|
||||
optimization: {
|
||||
minimizer: [
|
||||
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
|
||||
// `...`,
|
||||
new CssMinimizerPlugin(),
|
||||
],
|
||||
},
|
||||
plugins: [new MiniCssExtractPlugin()],
|
||||
};
|
||||
```
|
||||
|
||||
This will enable CSS optimization only in production mode.
|
||||
|
||||
If you want to run it also in development set the `optimization.minimize` option to `true`:
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
// [...]
|
||||
module.exports = {
|
||||
optimization: {
|
||||
// [...]
|
||||
minimize: true,
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
And run `webpack` via your preferred method.
|
||||
|
||||
## Note about source maps
|
||||
|
||||
**Works only with `source-map`, `inline-source-map`, `hidden-source-map` and `nosources-source-map` values for the [`devtool`](https://webpack.js.org/configuration/devtool/) option.**
|
||||
|
||||
Why? Because CSS support only these source map types.
|
||||
|
||||
The plugin respect the [`devtool`](https://webpack.js.org/configuration/devtool/) and using the `SourceMapDevToolPlugin` plugin.
|
||||
Using supported `devtool` values enable source map generation.
|
||||
Using `SourceMapDevToolPlugin` with enabled the `columns` option enables source map generation.
|
||||
|
||||
Use source maps to map error message locations to modules (this slows down the compilation).
|
||||
If you use your own `minify` function please read the `minify` section for handling source maps correctly.
|
||||
|
||||
## Options
|
||||
|
||||
| Name | Type | Default | Description |
|
||||
| :-----------------------------------------: | :--------------------------------------------: | :--------------------------------: | :-------------------------------------------------------------------------------- |
|
||||
| **[`test`](#test)** | `String\|RegExp\|Array<String\|RegExp>` | `/\.css(\?.*)?$/i` | Test to match files against. |
|
||||
| **[`include`](#include)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to include. |
|
||||
| **[`exclude`](#exclude)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to exclude. |
|
||||
| **[`parallel`](#parallel)** | `Boolean\|Number` | `true` | Enable/disable multi-process parallel running. |
|
||||
| **[`minify`](#minify)** | `Function\|Array<Function>` | `CssMinimizerPlugin.cssnanoMinify` | Allows to override default minify function. |
|
||||
| **[`minimizerOptions`](#minimizeroptions)** | `Object\|Array<Object>` | `{ preset: 'default' }` | Cssnano optimisations [options](https://cssnano.co/docs/what-are-optimisations/). |
|
||||
| **[`warningsFilter`](#warningsfilter)** | `Function<(warning, file, source) -> Boolean>` | `() => true` | Allow to filter css-minimizer warnings. |
|
||||
|
||||
### `test`
|
||||
|
||||
Type: `String|RegExp|Array<String|RegExp>` - default: `/\.css(\?.*)?$/i`
|
||||
|
||||
Test to match files against.
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
test: /\.foo\.css$/i,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### `include`
|
||||
|
||||
Type: `String|RegExp|Array<String|RegExp>`
|
||||
Default: `undefined`
|
||||
|
||||
Files to include.
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
include: /\/includes/,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### `exclude`
|
||||
|
||||
Type: `String|RegExp|Array<String|RegExp>`
|
||||
Default: `undefined`
|
||||
|
||||
Files to exclude.
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
exclude: /\/excludes/,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### `parallel`
|
||||
|
||||
Type: `Boolean|Number`
|
||||
Default: `true`
|
||||
|
||||
Use multi-process parallel running to improve the build speed.
|
||||
Default number of concurrent runs: `os.cpus().length - 1`.
|
||||
|
||||
> ℹ️ Parallelization can speed up your build significantly and is therefore **highly recommended**.
|
||||
> If a parallelization is enabled, the packages in `minimizerOptions` must be required via strings (`packageName` or `require.resolve(packageName)`). Read more in [`minimizerOptions`](#minimizeroptions)
|
||||
|
||||
#### `Boolean`
|
||||
|
||||
Enable/disable multi-process parallel running.
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
parallel: true,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
#### `Number`
|
||||
|
||||
Enable multi-process parallel running and set number of concurrent runs.
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
parallel: 4,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### `minify`
|
||||
|
||||
Type: `Function|Array<Function>`
|
||||
Default: `CssMinimizerPlugin.cssnanoMinify`
|
||||
|
||||
Allows overriding default minify function.
|
||||
By default, plugin uses [cssnano](https://github.com/cssnano/cssnano) package.
|
||||
Useful for using and testing unpublished versions or forks.
|
||||
|
||||
Possible options:
|
||||
|
||||
- `CssMinimizerPlugin.cssnanoMinify`
|
||||
- `CssMinimizerPlugin.cssoMinify`
|
||||
- `CssMinimizerPlugin.cleanCssMinify`
|
||||
- `CssMinimizerPlugin.esbuildMinify`
|
||||
- `CssMinimizerPlugin.lightningCssMinify` (previously`CssMinimizerPlugin.parcelCssMinify`, the package was renamed, but we keep it for backward compatibility)
|
||||
- `async (data, inputMap, minimizerOptions) => {return {code: "a{color: red}", map: "...", warnings: [], errors: []}}`
|
||||
|
||||
> **Warning**
|
||||
>
|
||||
> **Always use `require` inside `minify` function when `parallel` option enabled**.
|
||||
|
||||
#### `Function`
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minimizerOptions: {
|
||||
level: {
|
||||
1: {
|
||||
roundingPrecision: "all=3,px=5",
|
||||
},
|
||||
},
|
||||
},
|
||||
minify: CssMinimizerPlugin.cleanCssMinify,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
#### `Array`
|
||||
|
||||
If an array of functions is passed to the `minify` option, the `minimizerOptions` must also be an array.
|
||||
The function index in the `minify` array corresponds to the options object with the same index in the `minimizerOptions` array.
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minimizerOptions: [
|
||||
{}, // Options for the first function (CssMinimizerPlugin.cssnanoMinify)
|
||||
{}, // Options for the second function (CssMinimizerPlugin.cleanCssMinify)
|
||||
{}, // Options for the third function
|
||||
],
|
||||
minify: [
|
||||
CssMinimizerPlugin.cssnanoMinify,
|
||||
CssMinimizerPlugin.cleanCssMinify,
|
||||
async (data, inputMap, minimizerOptions) => {
|
||||
// To do something
|
||||
return {
|
||||
code: `a{color: red}`,
|
||||
map: `{"version": "3", ...}`,
|
||||
warnings: [],
|
||||
errors: [],
|
||||
};
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### `minimizerOptions`
|
||||
|
||||
Type: `Object|Array<Object>`
|
||||
Default: `{ preset: 'default' }`
|
||||
|
||||
Cssnano optimisations [options](https://cssnano.co/docs/what-are-optimisations/).
|
||||
|
||||
#### `Object`
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minimizerOptions: {
|
||||
preset: [
|
||||
"default",
|
||||
{
|
||||
discardComments: { removeAll: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
#### `Array`
|
||||
|
||||
The function index in the `minify` array corresponds to the options object with the same index in the `minimizerOptions` array.
|
||||
If you use `minimizerOptions` like object, all `minify` function accept it.
|
||||
|
||||
> If a parallelization is enabled, the packages in `minimizerOptions` must be required via strings (`packageName` or `require.resolve(packageName)`). In this case, we shouldn't use `require`/`import`.
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minimizerOptions: {
|
||||
preset: require.resolve("cssnano-preset-simple"),
|
||||
},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
##### `processorOptions` (⚠ only cssnano)
|
||||
|
||||
Type: `Object`
|
||||
Default: `{ from: assetName }`
|
||||
|
||||
Allows filtering options [`processoptions`](https://postcss.org/api/#processoptions) for the cssnano.
|
||||
The `parser`,` stringifier` and `syntax` can be either a function or a string indicating the module that will be imported.
|
||||
|
||||
> **Warning**
|
||||
>
|
||||
> **If a function is passed, the `parallel` option must be disabled.**.
|
||||
|
||||
```js
|
||||
import sugarss from "sugarss";
|
||||
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
parallel: false,
|
||||
minimizerOptions: {
|
||||
processorOptions: {
|
||||
parser: sugarss,
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minimizerOptions: {
|
||||
processorOptions: {
|
||||
parser: "sugarss",
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### `warningsFilter`
|
||||
|
||||
Type: `Function<(warning, file, source) -> Boolean>`
|
||||
Default: `() => true`
|
||||
|
||||
Allow filtering css-minimizer warnings (By default [cssnano](https://github.com/cssnano/cssnano)).
|
||||
Return `true` to keep the warning, a falsy value (`false`/`null`/`undefined`) otherwise.
|
||||
|
||||
> **Warning**
|
||||
>
|
||||
> The `source` argument will contain `undefined` if you don't use source maps.
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
warningsFilter: (warning, file, source) => {
|
||||
if (/Dropping unreachable code/i.test(warning)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (/file\.css/i.test(file)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (/source\.css/i.test(source)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Use sourcemaps
|
||||
|
||||
Don't forget to enable `sourceMap` options for all loaders.
|
||||
|
||||
```js
|
||||
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
|
||||
|
||||
module.exports = {
|
||||
devtool: "source-map",
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /.s?css$/,
|
||||
use: [
|
||||
MiniCssExtractPlugin.loader,
|
||||
{ loader: "css-loader", options: { sourceMap: true } },
|
||||
{ loader: "sass-loader", options: { sourceMap: true } },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
optimization: {
|
||||
minimizer: [new CssMinimizerPlugin()],
|
||||
},
|
||||
plugins: [new MiniCssExtractPlugin()],
|
||||
};
|
||||
```
|
||||
|
||||
### Remove all comments
|
||||
|
||||
Remove all comments (including comments starting with `/*!`).
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
optimization: {
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minimizerOptions: {
|
||||
preset: [
|
||||
"default",
|
||||
{
|
||||
discardComments: { removeAll: true },
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Using custom minifier [csso](https://github.com/css/csso)
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
// Uncomment if you need source maps
|
||||
// devtool: "source-map",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minify: CssMinimizerPlugin.cssoMinify,
|
||||
// Uncomment this line for options
|
||||
// minimizerOptions: { restructure: false },
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Using custom minifier [clean-css](https://github.com/jakubpawlowicz/clean-css)
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
// Uncomment if you need source maps
|
||||
// devtool: "source-map",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minify: CssMinimizerPlugin.cleanCssMinify,
|
||||
// Uncomment this line for options
|
||||
// minimizerOptions: { compatibility: 'ie11,-properties.merging' },
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Using custom minifier [esbuild](https://github.com/evanw/esbuild)
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
// Uncomment if you need source maps
|
||||
// devtool: "source-map",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minify: CssMinimizerPlugin.esbuildMinify,
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Using custom minifier [lightningcss](https://github.com/parcel-bundler/lightningcss), previously `@parcel/css`
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
// Uncomment if you need source maps
|
||||
// devtool: "source-map",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minify: CssMinimizerPlugin.lightningCssMinify,
|
||||
// Uncomment this line for options
|
||||
// minimizerOptions: { targets: { ie: 11 }, drafts: { nesting: true } },
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Using custom minifier [swc](https://github.com/swc-project/swc)
|
||||
|
||||
**webpack.config.js**
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
// Uncomment if you need source maps
|
||||
// devtool: "source-map",
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new CssMinimizerPlugin({
|
||||
minify: CssMinimizerPlugin.swcMinify,
|
||||
// Uncomment this line for options
|
||||
// minimizerOptions: {},
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Please take a moment to read our contributing guidelines if you haven't yet done so.
|
||||
|
||||
[CONTRIBUTING](./.github/CONTRIBUTING.md)
|
||||
|
||||
## License
|
||||
|
||||
[MIT](./LICENSE)
|
||||
|
||||
[npm]: https://img.shields.io/npm/v/css-minimizer-webpack-plugin.svg
|
||||
[npm-url]: https://npmjs.com/package/css-minimizer-webpack-plugin
|
||||
[node]: https://img.shields.io/node/v/css-minimizer-webpack-plugin.svg
|
||||
[node-url]: https://nodejs.org
|
||||
[tests]: https://github.com/webpack-contrib/css-minimizer-webpack-plugin/workflows/css-minimizer-webpack-plugin/badge.svg
|
||||
[tests-url]: https://github.com/webpack-contrib/css-minimizer-webpack-plugin/actions
|
||||
[cover]: https://codecov.io/gh/webpack-contrib/css-minimizer-webpack-plugin/branch/master/graph/badge.svg
|
||||
[cover-url]: https://codecov.io/gh/webpack-contrib/css-minimizer-webpack-plugin
|
||||
[discussion]: https://img.shields.io/github/discussions/webpack/webpack
|
||||
[discussion-url]: https://github.com/webpack/webpack/discussions
|
||||
[size]: https://packagephobia.now.sh/badge?p=css-minimizer-webpack-plugin
|
||||
[size-url]: https://packagephobia.now.sh/result?p=css-minimizer-webpack-plugin
|
||||
+613
@@ -0,0 +1,613 @@
|
||||
"use strict";
|
||||
|
||||
const os = require("os");
|
||||
const {
|
||||
validate
|
||||
} = require("schema-utils");
|
||||
const {
|
||||
throttleAll,
|
||||
cssnanoMinify,
|
||||
cssoMinify,
|
||||
cleanCssMinify,
|
||||
esbuildMinify,
|
||||
parcelCssMinify,
|
||||
lightningCssMinify,
|
||||
swcMinify
|
||||
} = require("./utils");
|
||||
const schema = require("./options.json");
|
||||
const {
|
||||
minify: minifyWorker
|
||||
} = require("./minify");
|
||||
|
||||
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
|
||||
/** @typedef {import("webpack").Compiler} Compiler */
|
||||
/** @typedef {import("webpack").Compilation} Compilation */
|
||||
/** @typedef {import("webpack").WebpackError} WebpackError */
|
||||
/** @typedef {import("jest-worker").Worker} JestWorker */
|
||||
/** @typedef {import("@jridgewell/trace-mapping").EncodedSourceMap} RawSourceMap */
|
||||
/** @typedef {import("webpack").Asset} Asset */
|
||||
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
|
||||
/** @typedef {import("postcss").Syntax} Syntax */
|
||||
/** @typedef {import("postcss").Parser} Parser */
|
||||
/** @typedef {import("postcss").Stringifier} Stringifier */
|
||||
/** @typedef {import("@jridgewell/trace-mapping").TraceMap} TraceMap */
|
||||
|
||||
/**
|
||||
* @typedef {Object} CssNanoOptions
|
||||
* @property {string} [configFile]
|
||||
* @property {[string, object] | string | undefined} [preset]
|
||||
*/
|
||||
|
||||
/** @typedef {Error & { plugin?: string, text?: string, source?: string } | string} Warning */
|
||||
|
||||
/**
|
||||
* @typedef {Object} WarningObject
|
||||
* @property {string} message
|
||||
* @property {string} [plugin]
|
||||
* @property {string} [text]
|
||||
* @property {number} [line]
|
||||
* @property {number} [column]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} ErrorObject
|
||||
* @property {string} message
|
||||
* @property {number} [line]
|
||||
* @property {number} [column]
|
||||
* @property {string} [stack]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} MinimizedResult
|
||||
* @property {string} code
|
||||
* @property {RawSourceMap} [map]
|
||||
* @property {Array<Error | ErrorObject| string>} [errors]
|
||||
* @property {Array<Warning | WarningObject | string>} [warnings]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ [file: string]: string }} Input
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ [key: string]: any }} CustomOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends infer U ? U : CustomOptions} InferDefaultType
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @callback BasicMinimizerImplementation
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {InferDefaultType<T>} minifyOptions
|
||||
* @returns {Promise<MinimizedResult>}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends any[] ? { [P in keyof T]: BasicMinimizerImplementation<T[P]>; } : BasicMinimizerImplementation<T>} MinimizerImplementation
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends any[] ? { [P in keyof T]?: InferDefaultType<T[P]> } : InferDefaultType<T>} MinimizerOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {Object} InternalOptions
|
||||
* @property {string} name
|
||||
* @property {string} input
|
||||
* @property {RawSourceMap | undefined} inputSourceMap
|
||||
* @property {{ implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> }} minimizer
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef InternalResult
|
||||
* @property {Array<{ code: string, map: RawSourceMap | undefined }>} outputs
|
||||
* @property {Array<Warning | WarningObject | string>} warnings
|
||||
* @property {Array<Error | ErrorObject | string>} errors
|
||||
*/
|
||||
|
||||
/** @typedef {undefined | boolean | number} Parallel */
|
||||
|
||||
/** @typedef {RegExp | string} Rule */
|
||||
/** @typedef {Rule[] | Rule} Rules */
|
||||
|
||||
/** @typedef {(warning: Warning | WarningObject | string, file: string, source?: string) => boolean} WarningsFilter */
|
||||
|
||||
/**
|
||||
* @typedef {Object} BasePluginOptions
|
||||
* @property {Rule} [test]
|
||||
* @property {Rule} [include]
|
||||
* @property {Rule} [exclude]
|
||||
* @property {WarningsFilter} [warningsFilter]
|
||||
* @property {Parallel} [parallel]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {JestWorker & { transform: (options: string) => InternalResult, minify: (options: InternalOptions<T>) => InternalResult }} MinimizerWorker
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef{ProcessOptions | { from?: string, to?: string, parser?: string | Syntax | Parser, stringifier?: string | Syntax | Stringifier, syntax?: string | Syntax } } ProcessOptionsExtender
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {CssNanoOptions & { processorOptions?: ProcessOptionsExtender }} CssNanoOptionsExtended
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {T extends CssNanoOptionsExtended ? { minify?: MinimizerImplementation<T> | undefined, minimizerOptions?: MinimizerOptions<T> | undefined } : { minify: MinimizerImplementation<T>, minimizerOptions?: MinimizerOptions<T> | undefined }} DefinedDefaultMinimizerAndOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {BasePluginOptions & { minimizer: { implementation: MinimizerImplementation<T>, options: MinimizerOptions<T> } }} InternalPluginOptions
|
||||
*/
|
||||
|
||||
const warningRegex = /\s.+:+([0-9]+):+([0-9]+)/;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param fn {(function(): any) | undefined}
|
||||
* @returns {function(): T}
|
||||
*/
|
||||
const memoize = fn => {
|
||||
let cache = false;
|
||||
/** @type {T} */
|
||||
let result;
|
||||
return () => {
|
||||
if (cache) {
|
||||
return result;
|
||||
}
|
||||
result = /** @type {function(): any} */fn();
|
||||
cache = true;
|
||||
// Allow to clean up memory for fn
|
||||
// and all dependent resources
|
||||
// eslint-disable-next-line no-undefined, no-param-reassign
|
||||
fn = undefined;
|
||||
return result;
|
||||
};
|
||||
};
|
||||
const getSerializeJavascript = memoize(() =>
|
||||
// eslint-disable-next-line global-require
|
||||
require("serialize-javascript"));
|
||||
const getTraceMapping = memoize(() =>
|
||||
// eslint-disable-next-line global-require
|
||||
require("@jridgewell/trace-mapping"));
|
||||
|
||||
/**
|
||||
* @template [T=CssNanoOptionsExtended]
|
||||
*/
|
||||
class CssMinimizerPlugin {
|
||||
/**
|
||||
* @param {BasePluginOptions & DefinedDefaultMinimizerAndOptions<T>} [options]
|
||||
*/
|
||||
constructor(options) {
|
||||
validate( /** @type {Schema} */schema, options || {}, {
|
||||
name: "Css Minimizer Plugin",
|
||||
baseDataPath: "options"
|
||||
});
|
||||
const {
|
||||
minify = /** @type {BasicMinimizerImplementation<T>} */cssnanoMinify,
|
||||
minimizerOptions = /** @type {MinimizerOptions<T>} */{},
|
||||
test = /\.css(\?.*)?$/i,
|
||||
warningsFilter = () => true,
|
||||
parallel = true,
|
||||
include,
|
||||
exclude
|
||||
} = options || {};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {InternalPluginOptions<T>}
|
||||
*/
|
||||
this.options = {
|
||||
test,
|
||||
warningsFilter,
|
||||
parallel,
|
||||
include,
|
||||
exclude,
|
||||
minimizer: {
|
||||
implementation: /** @type {MinimizerImplementation<T>} */minify,
|
||||
options: minimizerOptions
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {any} input
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static isSourceMap(input) {
|
||||
// All required options for `new SourceMapConsumer(...options)`
|
||||
// https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
|
||||
return Boolean(input && input.version && input.sources && Array.isArray(input.sources) && typeof input.mappings === "string");
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {Warning | WarningObject | string} warning
|
||||
* @param {string} file
|
||||
* @param {WarningsFilter} [warningsFilter]
|
||||
* @param {TraceMap} [sourceMap]
|
||||
* @param {Compilation["requestShortener"]} [requestShortener]
|
||||
* @returns {Error & { hideStack?: boolean, file?: string } | undefined}
|
||||
*/
|
||||
static buildWarning(warning, file, warningsFilter, sourceMap, requestShortener) {
|
||||
let warningMessage = typeof warning === "string" ? warning : `${warning.plugin ? `[${warning.plugin}] ` : ""}${warning.text || warning.message}`;
|
||||
let locationMessage = "";
|
||||
let source;
|
||||
if (sourceMap) {
|
||||
let line;
|
||||
let column;
|
||||
if (typeof warning === "string") {
|
||||
const match = warningRegex.exec(warning);
|
||||
if (match) {
|
||||
line = +match[1];
|
||||
column = +match[2];
|
||||
}
|
||||
} else {
|
||||
({
|
||||
line,
|
||||
column
|
||||
} = /** @type {WarningObject} */warning);
|
||||
}
|
||||
if (line && column) {
|
||||
const original = sourceMap && getTraceMapping().originalPositionFor(sourceMap, {
|
||||
line,
|
||||
column
|
||||
});
|
||||
if (original && original.source && original.source !== file && requestShortener) {
|
||||
({
|
||||
source
|
||||
} = original);
|
||||
warningMessage = `${warningMessage.replace(warningRegex, "")}`;
|
||||
locationMessage = `${requestShortener.shorten(original.source)}:${original.line}:${original.column}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (warningsFilter && !warningsFilter(warning, file, source)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Error & { hideStack?: boolean, file?: string }}
|
||||
*/
|
||||
const builtWarning = new Error(`${file} from Css Minimizer plugin\n${warningMessage}${locationMessage ? ` ${locationMessage}` : ""}`);
|
||||
builtWarning.name = "Warning";
|
||||
builtWarning.hideStack = true;
|
||||
builtWarning.file = file;
|
||||
|
||||
// eslint-disable-next-line consistent-return
|
||||
return builtWarning;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {Error | ErrorObject | string} error
|
||||
* @param {string} file
|
||||
* @param {TraceMap} [sourceMap]
|
||||
* @param {Compilation["requestShortener"]} [requestShortener]
|
||||
* @returns {Error}
|
||||
*/
|
||||
static buildError(error, file, sourceMap, requestShortener) {
|
||||
/**
|
||||
* @type {Error & { file?: string }}
|
||||
*/
|
||||
let builtError;
|
||||
if (typeof error === "string") {
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
if ( /** @type {ErrorObject} */error.line && /** @type {ErrorObject} */error.column) {
|
||||
const {
|
||||
line,
|
||||
column
|
||||
} = /** @type {ErrorObject & { line: number, column: number }} */error;
|
||||
const original = sourceMap && getTraceMapping().originalPositionFor(sourceMap, {
|
||||
line,
|
||||
column
|
||||
});
|
||||
if (original && original.source && requestShortener) {
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${line},${column}]${error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.message} [${file}:${line},${column}]${error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
if (error.stack) {
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.stack}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
builtError = new Error(`${file} from Css Minimizer plugin\n${error.message}`);
|
||||
builtError.file = file;
|
||||
return builtError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {Parallel} parallel
|
||||
* @returns {number}
|
||||
*/
|
||||
static getAvailableNumberOfCores(parallel) {
|
||||
// In some cases cpus() returns undefined
|
||||
// https://github.com/nodejs/node/issues/19022
|
||||
const cpus = os.cpus() || {
|
||||
length: 1
|
||||
};
|
||||
return parallel === true ? cpus.length - 1 : Math.min(Number(parallel) || 0, cpus.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {Compiler} compiler
|
||||
* @param {Compilation} compilation
|
||||
* @param {Record<string, import("webpack").sources.Source>} assets
|
||||
* @param {{availableNumberOfCores: number}} optimizeOptions
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async optimize(compiler, compilation, assets, optimizeOptions) {
|
||||
const cache = compilation.getCache("CssMinimizerWebpackPlugin");
|
||||
let numberOfAssetsForMinify = 0;
|
||||
const assetsForMinify = await Promise.all(Object.keys(typeof assets === "undefined" ? compilation.assets : assets).filter(name => {
|
||||
const {
|
||||
info
|
||||
} = /** @type {Asset} */compilation.getAsset(name);
|
||||
if (
|
||||
// Skip double minimize assets from child compilation
|
||||
info.minimized) {
|
||||
return false;
|
||||
}
|
||||
if (!compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined, this.options)(name)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}).map(async name => {
|
||||
const {
|
||||
info,
|
||||
source
|
||||
} = /** @type {Asset} */
|
||||
compilation.getAsset(name);
|
||||
const eTag = cache.getLazyHashedEtag(source);
|
||||
const cacheItem = cache.getItemCache(name, eTag);
|
||||
const output = await cacheItem.getPromise();
|
||||
if (!output) {
|
||||
numberOfAssetsForMinify += 1;
|
||||
}
|
||||
return {
|
||||
name,
|
||||
info,
|
||||
inputSource: source,
|
||||
output,
|
||||
cacheItem
|
||||
};
|
||||
}));
|
||||
if (assetsForMinify.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @type {undefined | (() => MinimizerWorker<T>)} */
|
||||
let getWorker;
|
||||
/** @type {undefined | MinimizerWorker<T>} */
|
||||
let initializedWorker;
|
||||
/** @type {undefined | number} */
|
||||
let numberOfWorkers;
|
||||
if (optimizeOptions.availableNumberOfCores > 0) {
|
||||
// Do not create unnecessary workers when the number of files is less than the available cores, it saves memory
|
||||
numberOfWorkers = Math.min(numberOfAssetsForMinify, optimizeOptions.availableNumberOfCores);
|
||||
getWorker = () => {
|
||||
if (initializedWorker) {
|
||||
return initializedWorker;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line global-require
|
||||
const {
|
||||
Worker
|
||||
} = require("jest-worker");
|
||||
initializedWorker = /** @type {MinimizerWorker<T>} */
|
||||
new Worker(require.resolve("./minify"), {
|
||||
numWorkers: numberOfWorkers,
|
||||
enableWorkerThreads: true
|
||||
});
|
||||
|
||||
// https://github.com/facebook/jest/issues/8872#issuecomment-524822081
|
||||
const workerStdout = initializedWorker.getStdout();
|
||||
if (workerStdout) {
|
||||
workerStdout.on("data", chunk => process.stdout.write(chunk));
|
||||
}
|
||||
const workerStderr = initializedWorker.getStderr();
|
||||
if (workerStderr) {
|
||||
workerStderr.on("data", chunk => process.stderr.write(chunk));
|
||||
}
|
||||
return initializedWorker;
|
||||
};
|
||||
}
|
||||
const {
|
||||
SourceMapSource,
|
||||
RawSource
|
||||
} = compiler.webpack.sources;
|
||||
const scheduledTasks = [];
|
||||
for (const asset of assetsForMinify) {
|
||||
scheduledTasks.push(async () => {
|
||||
const {
|
||||
name,
|
||||
inputSource,
|
||||
cacheItem
|
||||
} = asset;
|
||||
let {
|
||||
output
|
||||
} = asset;
|
||||
if (!output) {
|
||||
let input;
|
||||
/** @type {RawSourceMap | undefined} */
|
||||
let inputSourceMap;
|
||||
const {
|
||||
source: sourceFromInputSource,
|
||||
map
|
||||
} = inputSource.sourceAndMap();
|
||||
input = sourceFromInputSource;
|
||||
if (map) {
|
||||
if (!CssMinimizerPlugin.isSourceMap(map)) {
|
||||
compilation.warnings.push( /** @type {WebpackError} */
|
||||
new Error(`${name} contains invalid source map`));
|
||||
} else {
|
||||
inputSourceMap = /** @type {RawSourceMap} */map;
|
||||
}
|
||||
}
|
||||
if (Buffer.isBuffer(input)) {
|
||||
input = input.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {InternalOptions<T>}
|
||||
*/
|
||||
const options = {
|
||||
name,
|
||||
input,
|
||||
inputSourceMap,
|
||||
minimizer: {
|
||||
implementation: this.options.minimizer.implementation,
|
||||
options: this.options.minimizer.options
|
||||
}
|
||||
};
|
||||
let result;
|
||||
try {
|
||||
result = await (getWorker ? getWorker().transform(getSerializeJavascript()(options)) : minifyWorker(options));
|
||||
} catch (error) {
|
||||
const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
|
||||
compilation.errors.push( /** @type {WebpackError} */
|
||||
CssMinimizerPlugin.buildError( /** @type {any} */error, name, hasSourceMap ? new (getTraceMapping().TraceMap)( /** @type {RawSourceMap} */inputSourceMap) :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
// eslint-disable-next-line no-undefined
|
||||
hasSourceMap ? compilation.requestShortener : undefined));
|
||||
return;
|
||||
}
|
||||
output = {
|
||||
warnings: [],
|
||||
errors: []
|
||||
};
|
||||
for (const item of result.outputs) {
|
||||
if (item.map) {
|
||||
let originalSource;
|
||||
let innerSourceMap;
|
||||
if (output.source) {
|
||||
({
|
||||
source: originalSource,
|
||||
map: innerSourceMap
|
||||
} = output.source.sourceAndMap());
|
||||
} else {
|
||||
originalSource = input;
|
||||
innerSourceMap = inputSourceMap;
|
||||
}
|
||||
|
||||
// TODO need API for merging source maps in `webpack-source`
|
||||
output.source = new SourceMapSource(item.code, name, item.map, originalSource, innerSourceMap, true);
|
||||
} else {
|
||||
output.source = new RawSource(item.code);
|
||||
}
|
||||
}
|
||||
if (result.errors && result.errors.length > 0) {
|
||||
const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
|
||||
for (const error of result.errors) {
|
||||
output.warnings.push(CssMinimizerPlugin.buildError(error, name, hasSourceMap ? new (getTraceMapping().TraceMap)( /** @type {RawSourceMap} */inputSourceMap) :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
// eslint-disable-next-line no-undefined
|
||||
hasSourceMap ? compilation.requestShortener : undefined));
|
||||
}
|
||||
}
|
||||
if (result.warnings && result.warnings.length > 0) {
|
||||
const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
|
||||
for (const warning of result.warnings) {
|
||||
const buildWarning = CssMinimizerPlugin.buildWarning(warning, name, this.options.warningsFilter, hasSourceMap ? new (getTraceMapping().TraceMap)( /** @type {RawSourceMap} */inputSourceMap) :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
// eslint-disable-next-line no-undefined
|
||||
hasSourceMap ? compilation.requestShortener : undefined);
|
||||
if (buildWarning) {
|
||||
output.warnings.push(buildWarning);
|
||||
}
|
||||
}
|
||||
}
|
||||
await cacheItem.storePromise({
|
||||
source: output.source,
|
||||
warnings: output.warnings,
|
||||
errors: output.errors
|
||||
});
|
||||
}
|
||||
if (output.warnings && output.warnings.length > 0) {
|
||||
for (const warning of output.warnings) {
|
||||
compilation.warnings.push(warning);
|
||||
}
|
||||
}
|
||||
if (output.errors && output.errors.length > 0) {
|
||||
for (const error of output.errors) {
|
||||
compilation.errors.push(error);
|
||||
}
|
||||
}
|
||||
const newInfo = {
|
||||
minimized: true
|
||||
};
|
||||
const {
|
||||
source
|
||||
} = output;
|
||||
compilation.updateAsset(name, source, newInfo);
|
||||
});
|
||||
}
|
||||
const limit = getWorker && numberOfAssetsForMinify > 0 ? /** @type {number} */numberOfWorkers : scheduledTasks.length;
|
||||
await throttleAll(limit, scheduledTasks);
|
||||
if (initializedWorker) {
|
||||
await initializedWorker.end();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Compiler} compiler
|
||||
* @returns {void}
|
||||
*/
|
||||
apply(compiler) {
|
||||
const pluginName = this.constructor.name;
|
||||
const availableNumberOfCores = CssMinimizerPlugin.getAvailableNumberOfCores(this.options.parallel);
|
||||
compiler.hooks.compilation.tap(pluginName, compilation => {
|
||||
compilation.hooks.processAssets.tapPromise({
|
||||
name: pluginName,
|
||||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
||||
additionalAssets: true
|
||||
}, assets => this.optimize(compiler, compilation, assets, {
|
||||
availableNumberOfCores
|
||||
}));
|
||||
compilation.hooks.statsPrinter.tap(pluginName, stats => {
|
||||
stats.hooks.print.for("asset.info.minimized").tap("css-minimizer-webpack-plugin", (minimized, {
|
||||
green,
|
||||
formatFlag
|
||||
}) =>
|
||||
// eslint-disable-next-line no-undefined
|
||||
minimized ? /** @type {Function} */green( /** @type {Function} */formatFlag("minimized")) : "");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
CssMinimizerPlugin.cssnanoMinify = cssnanoMinify;
|
||||
CssMinimizerPlugin.cssoMinify = cssoMinify;
|
||||
CssMinimizerPlugin.cleanCssMinify = cleanCssMinify;
|
||||
CssMinimizerPlugin.esbuildMinify = esbuildMinify;
|
||||
CssMinimizerPlugin.parcelCssMinify = parcelCssMinify;
|
||||
CssMinimizerPlugin.lightningCssMinify = lightningCssMinify;
|
||||
CssMinimizerPlugin.swcMinify = swcMinify;
|
||||
module.exports = CssMinimizerPlugin;
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
|
||||
/** @typedef {import("./index.js").InternalResult} InternalResult */
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {import("./index.js").InternalOptions<T>} options
|
||||
* @returns {Promise<InternalResult>}
|
||||
*/
|
||||
const minify = async options => {
|
||||
const minifyFns = Array.isArray(options.minimizer.implementation) ? options.minimizer.implementation : [options.minimizer.implementation];
|
||||
|
||||
/** @type {InternalResult} */
|
||||
const result = {
|
||||
outputs: [],
|
||||
warnings: [],
|
||||
errors: []
|
||||
};
|
||||
let needSourceMap = false;
|
||||
for (let i = 0; i <= minifyFns.length - 1; i++) {
|
||||
const minifyFn = minifyFns[i];
|
||||
const minifyOptions = Array.isArray(options.minimizer.options) ? options.minimizer.options[i] : options.minimizer.options;
|
||||
const prevResult = result.outputs.length > 0 ? result.outputs[result.outputs.length - 1] : {
|
||||
code: options.input,
|
||||
map: options.inputSourceMap
|
||||
};
|
||||
const {
|
||||
code,
|
||||
map
|
||||
} = prevResult;
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const minifyResult = await minifyFn({
|
||||
[options.name]: code
|
||||
}, map, minifyOptions);
|
||||
if (typeof minifyResult.code !== "string") {
|
||||
throw new Error("minimizer function doesn't return the 'code' property or result is not a string value");
|
||||
}
|
||||
if (minifyResult.map) {
|
||||
needSourceMap = true;
|
||||
}
|
||||
if (minifyResult.errors) {
|
||||
result.errors = result.errors.concat(minifyResult.errors);
|
||||
}
|
||||
if (minifyResult.warnings) {
|
||||
result.warnings = result.warnings.concat(minifyResult.warnings);
|
||||
}
|
||||
result.outputs.push({
|
||||
code: minifyResult.code,
|
||||
map: minifyResult.map
|
||||
});
|
||||
}
|
||||
if (!needSourceMap) {
|
||||
result.outputs = [result.outputs[result.outputs.length - 1]];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} options
|
||||
* @returns {Promise<InternalResult>}
|
||||
*/
|
||||
async function transform(options) {
|
||||
// 'use strict' => this === undefined (Clean Scope)
|
||||
// Safer for possible security issues, albeit not critical at all here
|
||||
// eslint-disable-next-line no-new-func, no-param-reassign
|
||||
const evaluatedOptions = new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
|
||||
return minify(evaluatedOptions);
|
||||
}
|
||||
module.exports = {
|
||||
minify,
|
||||
transform
|
||||
};
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
{
|
||||
"definitions": {
|
||||
"Rule": {
|
||||
"description": "Filtering rule as regex or string.",
|
||||
"anyOf": [
|
||||
{
|
||||
"instanceof": "RegExp",
|
||||
"tsType": "RegExp"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"Rules": {
|
||||
"description": "Filtering rules.",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"description": "A rule condition.",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rule"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/Rule"
|
||||
}
|
||||
]
|
||||
},
|
||||
"MinimizerOptions": {
|
||||
"additionalProperties": true,
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"title": "CssMinimizerWebpackPluginOptions",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"test": {
|
||||
"description": "Include all modules that pass test assertion.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#test",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rules"
|
||||
}
|
||||
]
|
||||
},
|
||||
"include": {
|
||||
"description": "Include all modules matching any of these conditions.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#include",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rules"
|
||||
}
|
||||
]
|
||||
},
|
||||
"exclude": {
|
||||
"description": "Exclude all modules matching any of these conditions.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#exclude",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/Rules"
|
||||
}
|
||||
]
|
||||
},
|
||||
"minimizerOptions": {
|
||||
"description": "Options for `cssMinimizerOptions`.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#minimizeroptions",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MinimizerOptions"
|
||||
},
|
||||
{
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"$ref": "#/definitions/MinimizerOptions"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"parallel": {
|
||||
"description": "Use multi-process parallel running to improve the build speed.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#parallel",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"type": "integer"
|
||||
}
|
||||
]
|
||||
},
|
||||
"warningsFilter": {
|
||||
"description": "Allow to filter `css minimizer` warnings.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#warningsfilter",
|
||||
"instanceof": "Function"
|
||||
},
|
||||
"minify": {
|
||||
"description": "Allows you to override default minify function.",
|
||||
"link": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/#minify",
|
||||
"anyOf": [
|
||||
{
|
||||
"instanceof": "Function"
|
||||
},
|
||||
{
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"instanceof": "Function"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
+417
@@ -0,0 +1,417 @@
|
||||
"use strict";
|
||||
|
||||
/** @typedef {import("./index.js").Input} Input */
|
||||
/** @typedef {import("@jridgewell/trace-mapping").EncodedSourceMap} RawSourceMap */
|
||||
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
|
||||
/** @typedef {import("./index.js").CustomOptions} CustomOptions */
|
||||
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
|
||||
/** @typedef {import("postcss").Postcss} Postcss */
|
||||
|
||||
const notSettled = Symbol(`not-settled`);
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {() => Promise<T>} Task
|
||||
*/
|
||||
|
||||
/**
|
||||
* Run tasks with limited concurrency.
|
||||
* @template T
|
||||
* @param {number} limit - Limit of tasks that run at once.
|
||||
* @param {Task<T>[]} tasks - List of tasks to run.
|
||||
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
|
||||
*/
|
||||
function throttleAll(limit, tasks) {
|
||||
if (!Number.isInteger(limit) || limit < 1) {
|
||||
throw new TypeError(`Expected \`limit\` to be a finite number > 0, got \`${limit}\` (${typeof limit})`);
|
||||
}
|
||||
if (!Array.isArray(tasks) || !tasks.every(task => typeof task === `function`)) {
|
||||
throw new TypeError(`Expected \`tasks\` to be a list of functions returning a promise`);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const result = Array(tasks.length).fill(notSettled);
|
||||
const entries = tasks.entries();
|
||||
const next = () => {
|
||||
const {
|
||||
done,
|
||||
value
|
||||
} = entries.next();
|
||||
if (done) {
|
||||
const isLast = !result.includes(notSettled);
|
||||
if (isLast) resolve(result);
|
||||
return;
|
||||
}
|
||||
const [index, task] = value;
|
||||
|
||||
/**
|
||||
* @param {T} x
|
||||
*/
|
||||
const onFulfilled = x => {
|
||||
result[index] = x;
|
||||
next();
|
||||
};
|
||||
task().then(onFulfilled, reject);
|
||||
};
|
||||
Array(limit).fill(0).forEach(next);
|
||||
});
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function cssnanoMinify(input, sourceMap, minimizerOptions = {
|
||||
preset: "default"
|
||||
}) {
|
||||
/**
|
||||
* @template T
|
||||
* @param {string} module
|
||||
* @returns {Promise<T>}
|
||||
*/
|
||||
const load = async module => {
|
||||
let exports;
|
||||
try {
|
||||
// eslint-disable-next-line import/no-dynamic-require, global-require
|
||||
exports = require(module);
|
||||
return exports;
|
||||
} catch (requireError) {
|
||||
let importESM;
|
||||
try {
|
||||
// eslint-disable-next-line no-new-func
|
||||
importESM = new Function("id", "return import(id);");
|
||||
} catch (e) {
|
||||
importESM = null;
|
||||
}
|
||||
if ( /** @type {Error & {code: string}} */
|
||||
requireError.code === "ERR_REQUIRE_ESM" && importESM) {
|
||||
exports = await importESM(module);
|
||||
return exports.default;
|
||||
}
|
||||
throw requireError;
|
||||
}
|
||||
};
|
||||
const [[name, code]] = Object.entries(input);
|
||||
/** @type {ProcessOptions} */
|
||||
const postcssOptions = {
|
||||
from: name,
|
||||
...minimizerOptions.processorOptions
|
||||
};
|
||||
if (typeof postcssOptions.parser === "string") {
|
||||
try {
|
||||
postcssOptions.parser = await load(postcssOptions.parser);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading PostCSS "${postcssOptions.parser}" parser failed: ${
|
||||
/** @type {Error} */error.message}\n\n(@${name})`);
|
||||
}
|
||||
}
|
||||
if (typeof postcssOptions.stringifier === "string") {
|
||||
try {
|
||||
postcssOptions.stringifier = await load(postcssOptions.stringifier);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading PostCSS "${postcssOptions.stringifier}" stringifier failed: ${
|
||||
/** @type {Error} */error.message}\n\n(@${name})`);
|
||||
}
|
||||
}
|
||||
if (typeof postcssOptions.syntax === "string") {
|
||||
try {
|
||||
postcssOptions.syntax = await load(postcssOptions.syntax);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading PostCSS "${postcssOptions.syntax}" syntax failed: ${
|
||||
/** @type {Error} */error.message}\n\n(@${name})`);
|
||||
}
|
||||
}
|
||||
if (sourceMap) {
|
||||
postcssOptions.map = {
|
||||
annotation: false
|
||||
};
|
||||
}
|
||||
|
||||
/** @type {Postcss} */
|
||||
// eslint-disable-next-line global-require
|
||||
const postcss = require("postcss").default;
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line global-require
|
||||
const cssnano = require("cssnano");
|
||||
// @ts-ignore
|
||||
// Types are broken
|
||||
const result = await postcss([cssnano(minimizerOptions)]).process(code, postcssOptions);
|
||||
return {
|
||||
code: result.css,
|
||||
// @ts-ignore
|
||||
map: result.map ? result.map.toJSON() :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
warnings: result.warnings().map(String)
|
||||
};
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function cssoMinify(input, sourceMap, minimizerOptions) {
|
||||
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
||||
const csso = require("csso");
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
const result = csso.minify(code, {
|
||||
filename,
|
||||
sourceMap: Boolean(sourceMap),
|
||||
...minimizerOptions
|
||||
});
|
||||
return {
|
||||
code: result.css,
|
||||
map: result.map ? /** @type {any & { toJSON(): RawSourceMap }} */
|
||||
result.map.toJSON() :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined
|
||||
};
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function cleanCssMinify(input, sourceMap, minimizerOptions) {
|
||||
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
||||
const CleanCSS = require("clean-css");
|
||||
const [[name, code]] = Object.entries(input);
|
||||
const result = await new CleanCSS({
|
||||
sourceMap: Boolean(sourceMap),
|
||||
...minimizerOptions,
|
||||
returnPromise: true
|
||||
}).minify({
|
||||
[name]: {
|
||||
styles: code
|
||||
}
|
||||
});
|
||||
const generatedSourceMap = result.sourceMap && /** @type {any & { toJSON(): RawSourceMap }} */
|
||||
result.sourceMap.toJSON();
|
||||
|
||||
// workaround for source maps on windows
|
||||
if (generatedSourceMap) {
|
||||
// eslint-disable-next-line global-require
|
||||
const isWindowsPathSep = require("path").sep === "\\";
|
||||
generatedSourceMap.sources = generatedSourceMap.sources.map(
|
||||
/**
|
||||
* @param {string} item
|
||||
* @returns {string}
|
||||
*/
|
||||
item => isWindowsPathSep ? item.replace(/\\/g, "/") : item);
|
||||
}
|
||||
return {
|
||||
code: result.styles,
|
||||
map: generatedSourceMap,
|
||||
warnings: result.warnings
|
||||
};
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function esbuildMinify(input, sourceMap, minimizerOptions) {
|
||||
/**
|
||||
* @param {import("esbuild").TransformOptions} [esbuildOptions={}]
|
||||
* @returns {import("esbuild").TransformOptions}
|
||||
*/
|
||||
const buildEsbuildOptions = (esbuildOptions = {}) => {
|
||||
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
|
||||
return {
|
||||
loader: "css",
|
||||
minify: true,
|
||||
legalComments: "inline",
|
||||
...esbuildOptions,
|
||||
sourcemap: false
|
||||
};
|
||||
};
|
||||
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
|
||||
const esbuild = require("esbuild");
|
||||
|
||||
// Copy `esbuild` options
|
||||
const esbuildOptions = buildEsbuildOptions(minimizerOptions);
|
||||
|
||||
// Let `esbuild` generate a SourceMap
|
||||
if (sourceMap) {
|
||||
esbuildOptions.sourcemap = true;
|
||||
esbuildOptions.sourcesContent = false;
|
||||
}
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
esbuildOptions.sourcefile = filename;
|
||||
const result = await esbuild.transform(code, esbuildOptions);
|
||||
return {
|
||||
code: result.code,
|
||||
// eslint-disable-next-line no-undefined
|
||||
map: result.map ? JSON.parse(result.map) : undefined,
|
||||
warnings: result.warnings.length > 0 ? result.warnings.map(item => {
|
||||
return {
|
||||
source: item.location && item.location.file,
|
||||
line: item.location && item.location.line ? item.location.line :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
column: item.location && item.location.column ? item.location.column :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined,
|
||||
plugin: item.pluginName,
|
||||
message: `${item.text}${item.detail ? `\nDetails:\n${item.detail}` : ""}${item.notes.length > 0 ? `\n\nNotes:\n${item.notes.map(note => `${note.location ? `[${note.location.file}:${note.location.line}:${note.location.column}] ` : ""}${note.text}${note.location ? `\nSuggestion: ${note.location.suggestion}` : ""}${note.location ? `\nLine text:\n${note.location.lineText}\n` : ""}`).join("\n")}` : ""}`
|
||||
};
|
||||
}) : []
|
||||
};
|
||||
}
|
||||
|
||||
// TODO remove in the next major release
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function parcelCssMinify(input, sourceMap, minimizerOptions) {
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
/**
|
||||
* @param {Partial<import("@parcel/css").TransformOptions<any>>} [parcelCssOptions={}]
|
||||
* @returns {import("@parcel/css").TransformOptions<any>}
|
||||
*/
|
||||
const buildParcelCssOptions = (parcelCssOptions = {}) => {
|
||||
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
|
||||
return {
|
||||
minify: true,
|
||||
...parcelCssOptions,
|
||||
sourceMap: false,
|
||||
filename,
|
||||
code: Buffer.from(code)
|
||||
};
|
||||
};
|
||||
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
|
||||
const parcelCss = require("@parcel/css");
|
||||
|
||||
// Copy `parcel-css` options
|
||||
const parcelCssOptions = buildParcelCssOptions(minimizerOptions);
|
||||
|
||||
// Let `esbuild` generate a SourceMap
|
||||
if (sourceMap) {
|
||||
parcelCssOptions.sourceMap = true;
|
||||
}
|
||||
const result = await parcelCss.transform(parcelCssOptions);
|
||||
return {
|
||||
code: result.code.toString(),
|
||||
// eslint-disable-next-line no-undefined
|
||||
map: result.map ? JSON.parse(result.map.toString()) : undefined
|
||||
};
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function lightningCssMinify(input, sourceMap, minimizerOptions) {
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
/**
|
||||
* @param {Partial<import("lightningcss").TransformOptions<any>>} [lightningCssOptions={}]
|
||||
* @returns {import("lightningcss").TransformOptions<any>}
|
||||
*/
|
||||
const buildLightningCssOptions = (lightningCssOptions = {}) => {
|
||||
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
|
||||
return {
|
||||
minify: true,
|
||||
...lightningCssOptions,
|
||||
sourceMap: false,
|
||||
filename,
|
||||
code: Buffer.from(code)
|
||||
};
|
||||
};
|
||||
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
|
||||
const lightningCss = require("lightningcss");
|
||||
|
||||
// Copy `lightningCss` options
|
||||
const lightningCssOptions = buildLightningCssOptions(minimizerOptions);
|
||||
|
||||
// Let `esbuild` generate a SourceMap
|
||||
if (sourceMap) {
|
||||
lightningCssOptions.sourceMap = true;
|
||||
}
|
||||
const result = await lightningCss.transform(lightningCssOptions);
|
||||
return {
|
||||
code: result.code.toString(),
|
||||
// eslint-disable-next-line no-undefined
|
||||
map: result.map ? JSON.parse(result.map.toString()) : undefined
|
||||
};
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
async function swcMinify(input, sourceMap, minimizerOptions) {
|
||||
const [[filename, code]] = Object.entries(input);
|
||||
/**
|
||||
* @param {Partial<import("@swc/css").Options>} [swcOptions={}]
|
||||
* @returns {import("@swc/css").Options}
|
||||
*/
|
||||
const buildSwcOptions = (swcOptions = {}) => {
|
||||
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
|
||||
return {
|
||||
...swcOptions,
|
||||
filename
|
||||
};
|
||||
};
|
||||
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
|
||||
const swc = require("@swc/css");
|
||||
|
||||
// Copy `swc` options
|
||||
const swcOptions = buildSwcOptions(minimizerOptions);
|
||||
|
||||
// Let `swc` generate a SourceMap
|
||||
if (sourceMap) {
|
||||
swcOptions.sourceMap = true;
|
||||
}
|
||||
const result = await swc.minify(Buffer.from(code), swcOptions);
|
||||
return {
|
||||
code: result.code.toString(),
|
||||
// eslint-disable-next-line no-undefined
|
||||
map: result.map ? JSON.parse(result.map.toString()) : undefined,
|
||||
errors: result.errors ? result.errors.map(diagnostic => {
|
||||
const error = new Error(diagnostic.message);
|
||||
|
||||
// @ts-ignore
|
||||
error.span = diagnostic.span;
|
||||
// @ts-ignore
|
||||
error.level = diagnostic.level;
|
||||
return error;
|
||||
}) :
|
||||
// eslint-disable-next-line no-undefined
|
||||
undefined
|
||||
};
|
||||
}
|
||||
module.exports = {
|
||||
throttleAll,
|
||||
cssnanoMinify,
|
||||
cssoMinify,
|
||||
cleanCssMinify,
|
||||
esbuildMinify,
|
||||
parcelCssMinify,
|
||||
lightningCssMinify,
|
||||
swcMinify
|
||||
};
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
{
|
||||
"_from": "css-minimizer-webpack-plugin@^5.0.1",
|
||||
"_id": "css-minimizer-webpack-plugin@5.0.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-3caImjKFQkS+ws1TGcFn0V1HyDJFq1Euy589JlD6/3rV2kj+w7r5G9WDMgSHvpvXHNZ2calVypZWuEDQd9wfLg==",
|
||||
"_location": "/css-minimizer-webpack-plugin",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "css-minimizer-webpack-plugin@^5.0.1",
|
||||
"name": "css-minimizer-webpack-plugin",
|
||||
"escapedName": "css-minimizer-webpack-plugin",
|
||||
"rawSpec": "^5.0.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^5.0.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#DEV:/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/css-minimizer-webpack-plugin/-/css-minimizer-webpack-plugin-5.0.1.tgz",
|
||||
"_shasum": "33effe662edb1a0bf08ad633c32fa75d0f7ec565",
|
||||
"_spec": "css-minimizer-webpack-plugin@^5.0.1",
|
||||
"_where": "/root/dns",
|
||||
"author": {
|
||||
"name": "Loann Neveu"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"@jridgewell/trace-mapping": "^0.3.18",
|
||||
"cssnano": "^6.0.1",
|
||||
"jest-worker": "^29.4.3",
|
||||
"postcss": "^8.4.24",
|
||||
"schema-utils": "^4.0.1",
|
||||
"serialize-javascript": "^6.0.1"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "CSS minimizer (minifier) plugin for Webpack",
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.21.5",
|
||||
"@babel/core": "^7.22.1",
|
||||
"@babel/preset-env": "^7.22.4",
|
||||
"@commitlint/cli": "^17.6.5",
|
||||
"@commitlint/config-conventional": "^17.6.5",
|
||||
"@parcel/css": "^1.8.3",
|
||||
"@swc/css": "^0.0.20",
|
||||
"@types/clean-css": "^4.2.5",
|
||||
"@types/csso": "^5.0.0",
|
||||
"@types/node": "^18.16.3",
|
||||
"@types/serialize-javascript": "^5.0.2",
|
||||
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
|
||||
"babel-jest": "^29.4.3",
|
||||
"clean-css": "^5.3.2",
|
||||
"copy-webpack-plugin": "^9.1.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"cspell": "^6.27.0",
|
||||
"css-loader": "^6.8.1",
|
||||
"cssnano-preset-simple": "^4.0.0",
|
||||
"csso": "^5.0.3",
|
||||
"del": "^6.1.0",
|
||||
"del-cli": "^5.0.0",
|
||||
"esbuild": "^0.17.19",
|
||||
"eslint": "^8.41.0",
|
||||
"eslint-config-prettier": "^8.6.0",
|
||||
"eslint-plugin-import": "^2.27.5",
|
||||
"husky": "^8.0.3",
|
||||
"jest": "^29.4.3",
|
||||
"lightningcss": "^1.20.0",
|
||||
"lint-staged": "^13.2.2",
|
||||
"memfs": "^3.5.1",
|
||||
"mini-css-extract-plugin": "^2.7.6",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.8.8",
|
||||
"sass": "^1.62.1",
|
||||
"sass-loader": "^13.3.1",
|
||||
"source-map": "^0.6.1",
|
||||
"standard-version": "^9.5.0",
|
||||
"sugarss": "^4.0.1",
|
||||
"typescript": "^5.0.4",
|
||||
"webpack": "^5.85.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 14.15.0"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"types"
|
||||
],
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/webpack"
|
||||
},
|
||||
"homepage": "https://github.com/webpack-contrib/css-minimizer-webpack-plugin",
|
||||
"keywords": [
|
||||
"cssnano",
|
||||
"css",
|
||||
"csso",
|
||||
"clean-css",
|
||||
"swc",
|
||||
"esbuild",
|
||||
"webpack",
|
||||
"webpack-plugin",
|
||||
"minimize",
|
||||
"minimizer",
|
||||
"minify",
|
||||
"minifier",
|
||||
"optimize",
|
||||
"optimizer"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"name": "css-minimizer-webpack-plugin",
|
||||
"peerDependencies": {
|
||||
"webpack": "^5.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"csso": {
|
||||
"optional": true
|
||||
},
|
||||
"esbuild": {
|
||||
"optional": true
|
||||
},
|
||||
"@swc/css": {
|
||||
"optional": true
|
||||
},
|
||||
"clean-css": {
|
||||
"optional": true
|
||||
},
|
||||
"@parcel/css": {
|
||||
"optional": true
|
||||
},
|
||||
"lightningcss": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/webpack-contrib/css-minimizer-webpack-plugin.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npm-run-all -p \"build:**\"",
|
||||
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
|
||||
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
|
||||
"clean": "del-cli dist",
|
||||
"commitlint": "commitlint --from=master",
|
||||
"fix": "npm-run-all -l fix:js fix:prettier",
|
||||
"fix:js": "npm run lint:js -- --fix",
|
||||
"fix:prettier": "npm run lint:prettier -- --write",
|
||||
"lint": "npm-run-all -l -p \"lint:**\"",
|
||||
"lint:js": "eslint --cache .",
|
||||
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
|
||||
"lint:spelling": "cspell \"**/*.*\"",
|
||||
"lint:types": "tsc --pretty --noEmit",
|
||||
"prebuild": "npm run clean types",
|
||||
"prepare": "husky install && npm run build",
|
||||
"pretest": "npm run lint",
|
||||
"release": "standard-version",
|
||||
"security": "npm audit",
|
||||
"start": "npm run build -- -w",
|
||||
"test": "npm run test:coverage",
|
||||
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
|
||||
"test:only": "cross-env NODE_ENV=test jest",
|
||||
"test:watch": "npm run test:only -- --watch"
|
||||
},
|
||||
"types": "types/index.d.ts",
|
||||
"version": "5.0.1"
|
||||
}
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
export = CssMinimizerPlugin;
|
||||
/**
|
||||
* @template [T=CssNanoOptionsExtended]
|
||||
*/
|
||||
declare class CssMinimizerPlugin<T = CssNanoOptionsExtended> {
|
||||
/**
|
||||
* @private
|
||||
* @param {any} input
|
||||
* @returns {boolean}
|
||||
*/
|
||||
private static isSourceMap;
|
||||
/**
|
||||
* @private
|
||||
* @param {Warning | WarningObject | string} warning
|
||||
* @param {string} file
|
||||
* @param {WarningsFilter} [warningsFilter]
|
||||
* @param {TraceMap} [sourceMap]
|
||||
* @param {Compilation["requestShortener"]} [requestShortener]
|
||||
* @returns {Error & { hideStack?: boolean, file?: string } | undefined}
|
||||
*/
|
||||
private static buildWarning;
|
||||
/**
|
||||
* @private
|
||||
* @param {Error | ErrorObject | string} error
|
||||
* @param {string} file
|
||||
* @param {TraceMap} [sourceMap]
|
||||
* @param {Compilation["requestShortener"]} [requestShortener]
|
||||
* @returns {Error}
|
||||
*/
|
||||
private static buildError;
|
||||
/**
|
||||
* @private
|
||||
* @param {Parallel} parallel
|
||||
* @returns {number}
|
||||
*/
|
||||
private static getAvailableNumberOfCores;
|
||||
/**
|
||||
* @param {BasePluginOptions & DefinedDefaultMinimizerAndOptions<T>} [options]
|
||||
*/
|
||||
constructor(
|
||||
options?:
|
||||
| (BasePluginOptions & DefinedDefaultMinimizerAndOptions<T>)
|
||||
| undefined
|
||||
);
|
||||
/**
|
||||
* @private
|
||||
* @type {InternalPluginOptions<T>}
|
||||
*/
|
||||
private options;
|
||||
/**
|
||||
* @private
|
||||
* @param {Compiler} compiler
|
||||
* @param {Compilation} compilation
|
||||
* @param {Record<string, import("webpack").sources.Source>} assets
|
||||
* @param {{availableNumberOfCores: number}} optimizeOptions
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
private optimize;
|
||||
/**
|
||||
* @param {Compiler} compiler
|
||||
* @returns {void}
|
||||
*/
|
||||
apply(compiler: Compiler): void;
|
||||
}
|
||||
declare namespace CssMinimizerPlugin {
|
||||
export {
|
||||
cssnanoMinify,
|
||||
cssoMinify,
|
||||
cleanCssMinify,
|
||||
esbuildMinify,
|
||||
parcelCssMinify,
|
||||
lightningCssMinify,
|
||||
swcMinify,
|
||||
Schema,
|
||||
Compiler,
|
||||
Compilation,
|
||||
WebpackError,
|
||||
JestWorker,
|
||||
RawSourceMap,
|
||||
Asset,
|
||||
ProcessOptions,
|
||||
Syntax,
|
||||
Parser,
|
||||
Stringifier,
|
||||
TraceMap,
|
||||
CssNanoOptions,
|
||||
Warning,
|
||||
WarningObject,
|
||||
ErrorObject,
|
||||
MinimizedResult,
|
||||
Input,
|
||||
CustomOptions,
|
||||
InferDefaultType,
|
||||
BasicMinimizerImplementation,
|
||||
MinimizerImplementation,
|
||||
MinimizerOptions,
|
||||
InternalOptions,
|
||||
InternalResult,
|
||||
Parallel,
|
||||
Rule,
|
||||
Rules,
|
||||
WarningsFilter,
|
||||
BasePluginOptions,
|
||||
MinimizerWorker,
|
||||
ProcessOptionsExtender,
|
||||
CssNanoOptionsExtended,
|
||||
DefinedDefaultMinimizerAndOptions,
|
||||
InternalPluginOptions,
|
||||
};
|
||||
}
|
||||
type CssNanoOptionsExtended = CssNanoOptions & {
|
||||
processorOptions?: ProcessOptionsExtender;
|
||||
};
|
||||
type Compiler = import("webpack").Compiler;
|
||||
type BasePluginOptions = {
|
||||
test?: Rule | undefined;
|
||||
include?: Rule | undefined;
|
||||
exclude?: Rule | undefined;
|
||||
warningsFilter?: WarningsFilter | undefined;
|
||||
parallel?: Parallel;
|
||||
};
|
||||
type DefinedDefaultMinimizerAndOptions<T> = T extends CssNanoOptionsExtended
|
||||
? {
|
||||
minify?: MinimizerImplementation<T> | undefined;
|
||||
minimizerOptions?: MinimizerOptions<T> | undefined;
|
||||
}
|
||||
: {
|
||||
minify: MinimizerImplementation<T>;
|
||||
minimizerOptions?: MinimizerOptions<T> | undefined;
|
||||
};
|
||||
import { cssnanoMinify } from "./utils";
|
||||
import { cssoMinify } from "./utils";
|
||||
import { cleanCssMinify } from "./utils";
|
||||
import { esbuildMinify } from "./utils";
|
||||
import { parcelCssMinify } from "./utils";
|
||||
import { lightningCssMinify } from "./utils";
|
||||
import { swcMinify } from "./utils";
|
||||
type Schema = import("schema-utils/declarations/validate").Schema;
|
||||
type Compilation = import("webpack").Compilation;
|
||||
type WebpackError = import("webpack").WebpackError;
|
||||
type JestWorker = import("jest-worker").Worker;
|
||||
type RawSourceMap = import("@jridgewell/trace-mapping").EncodedSourceMap;
|
||||
type Asset = import("webpack").Asset;
|
||||
type ProcessOptions = import("postcss").ProcessOptions;
|
||||
type Syntax = import("postcss").Syntax;
|
||||
type Parser = import("postcss").Parser;
|
||||
type Stringifier = import("postcss").Stringifier;
|
||||
type TraceMap = import("@jridgewell/trace-mapping").TraceMap;
|
||||
type CssNanoOptions = {
|
||||
configFile?: string | undefined;
|
||||
preset?: [string, object] | string | undefined;
|
||||
};
|
||||
type Warning =
|
||||
| (Error & {
|
||||
plugin?: string;
|
||||
text?: string;
|
||||
source?: string;
|
||||
})
|
||||
| string;
|
||||
type WarningObject = {
|
||||
message: string;
|
||||
plugin?: string | undefined;
|
||||
text?: string | undefined;
|
||||
line?: number | undefined;
|
||||
column?: number | undefined;
|
||||
};
|
||||
type ErrorObject = {
|
||||
message: string;
|
||||
line?: number | undefined;
|
||||
column?: number | undefined;
|
||||
stack?: string | undefined;
|
||||
};
|
||||
type MinimizedResult = {
|
||||
code: string;
|
||||
map?: import("@jridgewell/trace-mapping").EncodedSourceMap | undefined;
|
||||
errors?: (string | Error | ErrorObject)[] | undefined;
|
||||
warnings?: (Warning | WarningObject)[] | undefined;
|
||||
};
|
||||
type Input = {
|
||||
[file: string]: string;
|
||||
};
|
||||
type CustomOptions = {
|
||||
[key: string]: any;
|
||||
};
|
||||
type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
|
||||
type BasicMinimizerImplementation<T> = (
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minifyOptions: InferDefaultType<T>
|
||||
) => Promise<MinimizedResult>;
|
||||
type MinimizerImplementation<T> = T extends any[]
|
||||
? { [P in keyof T]: BasicMinimizerImplementation<T[P]> }
|
||||
: BasicMinimizerImplementation<T>;
|
||||
type MinimizerOptions<T> = T extends any[]
|
||||
? { [P in keyof T]?: InferDefaultType<T[P]> | undefined }
|
||||
: InferDefaultType<T>;
|
||||
type InternalOptions<T> = {
|
||||
name: string;
|
||||
input: string;
|
||||
inputSourceMap: RawSourceMap | undefined;
|
||||
minimizer: {
|
||||
implementation: MinimizerImplementation<T>;
|
||||
options: MinimizerOptions<T>;
|
||||
};
|
||||
};
|
||||
type InternalResult = {
|
||||
outputs: Array<{
|
||||
code: string;
|
||||
map: RawSourceMap | undefined;
|
||||
}>;
|
||||
warnings: Array<Warning | WarningObject | string>;
|
||||
errors: Array<Error | ErrorObject | string>;
|
||||
};
|
||||
type Parallel = undefined | boolean | number;
|
||||
type Rule = RegExp | string;
|
||||
type Rules = Rule[] | Rule;
|
||||
type WarningsFilter = (
|
||||
warning: Warning | WarningObject | string,
|
||||
file: string,
|
||||
source?: string
|
||||
) => boolean;
|
||||
type MinimizerWorker<T> = import("jest-worker").Worker & {
|
||||
transform: (options: string) => InternalResult;
|
||||
minify: (options: InternalOptions<T>) => InternalResult;
|
||||
};
|
||||
type ProcessOptionsExtender =
|
||||
| ProcessOptions
|
||||
| {
|
||||
from?: string;
|
||||
to?: string;
|
||||
parser?: string | Syntax | Parser;
|
||||
stringifier?: string | Syntax | Stringifier;
|
||||
syntax?: string | Syntax;
|
||||
};
|
||||
type InternalPluginOptions<T> = BasePluginOptions & {
|
||||
minimizer: {
|
||||
implementation: MinimizerImplementation<T>;
|
||||
options: MinimizerOptions<T>;
|
||||
};
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
export type MinimizedResult = import("./index.js").MinimizedResult;
|
||||
export type InternalResult = import("./index.js").InternalResult;
|
||||
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
|
||||
/** @typedef {import("./index.js").InternalResult} InternalResult */
|
||||
/**
|
||||
* @template T
|
||||
* @param {import("./index.js").InternalOptions<T>} options
|
||||
* @returns {Promise<InternalResult>}
|
||||
*/
|
||||
export function minify<T>(
|
||||
options: import("./index.js").InternalOptions<T>
|
||||
): Promise<InternalResult>;
|
||||
/**
|
||||
* @param {string} options
|
||||
* @returns {Promise<InternalResult>}
|
||||
*/
|
||||
export function transform(options: string): Promise<InternalResult>;
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
export type Task<T> = () => Promise<T>;
|
||||
export type Input = import("./index.js").Input;
|
||||
export type RawSourceMap = import("@jridgewell/trace-mapping").EncodedSourceMap;
|
||||
export type MinimizedResult = import("./index.js").MinimizedResult;
|
||||
export type CustomOptions = import("./index.js").CustomOptions;
|
||||
export type ProcessOptions = import("postcss").ProcessOptions;
|
||||
export type Postcss = import("postcss").Postcss;
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {() => Promise<T>} Task
|
||||
*/
|
||||
/**
|
||||
* Run tasks with limited concurrency.
|
||||
* @template T
|
||||
* @param {number} limit - Limit of tasks that run at once.
|
||||
* @param {Task<T>[]} tasks - List of tasks to run.
|
||||
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
|
||||
*/
|
||||
export function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function cssnanoMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions?: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function cssoMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function cleanCssMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function esbuildMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function parcelCssMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function lightningCssMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
/**
|
||||
* @param {Input} input
|
||||
* @param {RawSourceMap | undefined} sourceMap
|
||||
* @param {CustomOptions} minimizerOptions
|
||||
* @return {Promise<MinimizedResult>}
|
||||
*/
|
||||
export function swcMinify(
|
||||
input: Input,
|
||||
sourceMap: RawSourceMap | undefined,
|
||||
minimizerOptions: CustomOptions
|
||||
): Promise<MinimizedResult>;
|
||||
Reference in New Issue
Block a user