[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.cssnanoMinify = cssnanoMinify;
|
---|
| 7 | exports.cssoMinify = cssoMinify;
|
---|
| 8 | exports.cleanCssMinify = cleanCssMinify;
|
---|
| 9 |
|
---|
| 10 | /* istanbul ignore next */
|
---|
| 11 | async function cssnanoMinify(data, inputSourceMap, minimizerOptions = {
|
---|
| 12 | preset: 'default'
|
---|
| 13 | }) {
|
---|
| 14 | const [[name, input]] = Object.entries(data);
|
---|
| 15 | const postcssOptions = {
|
---|
| 16 | to: name,
|
---|
| 17 | from: name,
|
---|
| 18 | ...minimizerOptions.processorOptions
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | if (typeof postcssOptions.parser === 'string') {
|
---|
| 22 | try {
|
---|
| 23 | postcssOptions.parser = await load(postcssOptions.parser);
|
---|
| 24 | } catch (error) {
|
---|
| 25 | throw new Error(`Loading PostCSS "${postcssOptions.parser}" parser failed: ${error.message}\n\n(@${name})`);
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | if (typeof postcssOptions.stringifier === 'string') {
|
---|
| 30 | try {
|
---|
| 31 | postcssOptions.stringifier = await load(postcssOptions.stringifier);
|
---|
| 32 | } catch (error) {
|
---|
| 33 | throw new Error(`Loading PostCSS "${postcssOptions.stringifier}" stringifier failed: ${error.message}\n\n(@${name})`);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | if (typeof postcssOptions.syntax === 'string') {
|
---|
| 38 | try {
|
---|
| 39 | postcssOptions.syntax = await load(postcssOptions.syntax);
|
---|
| 40 | } catch (error) {
|
---|
| 41 | throw new Error(`Loading PostCSS "${postcssOptions.syntax}" syntax failed: ${error.message}\n\n(@${name})`);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | if (inputSourceMap) {
|
---|
| 46 | postcssOptions.map = {
|
---|
| 47 | annotation: false,
|
---|
| 48 | prev: inputSourceMap
|
---|
| 49 | };
|
---|
| 50 | } // eslint-disable-next-line global-require
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | const postcss = require('postcss'); // eslint-disable-next-line global-require
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | const cssnano = require('cssnano');
|
---|
| 57 |
|
---|
| 58 | const result = await postcss([cssnano(minimizerOptions)]).process(input, postcssOptions);
|
---|
| 59 | return {
|
---|
| 60 | code: result.css,
|
---|
| 61 | map: result.map && result.map.toString(),
|
---|
| 62 | warnings: result.warnings().map(String)
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | async function load(module) {
|
---|
| 66 | let exports;
|
---|
| 67 |
|
---|
| 68 | try {
|
---|
| 69 | // eslint-disable-next-line import/no-dynamic-require, global-require
|
---|
| 70 | exports = require(module);
|
---|
| 71 | return exports;
|
---|
| 72 | } catch (requireError) {
|
---|
| 73 | let importESM;
|
---|
| 74 |
|
---|
| 75 | try {
|
---|
| 76 | // eslint-disable-next-line no-new-func
|
---|
| 77 | importESM = new Function('id', 'return import(id);');
|
---|
| 78 | } catch (e) {
|
---|
| 79 | importESM = null;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (requireError.code === 'ERR_REQUIRE_ESM' && importESM) {
|
---|
| 83 | exports = await importESM(module);
|
---|
| 84 | return exports.default;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | throw requireError;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | /* istanbul ignore next */
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | async function cssoMinify(data, inputSourceMap, minimizerOptions) {
|
---|
| 95 | // eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
---|
| 96 | const csso = require('csso'); // eslint-disable-next-line global-require
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | const sourcemap = require('source-map');
|
---|
| 100 |
|
---|
| 101 | const [[filename, input]] = Object.entries(data);
|
---|
| 102 | const result = csso.minify(input, {
|
---|
| 103 | filename,
|
---|
| 104 | sourceMap: inputSourceMap,
|
---|
| 105 | ...minimizerOptions
|
---|
| 106 | });
|
---|
| 107 |
|
---|
| 108 | if (inputSourceMap) {
|
---|
| 109 | result.map.applySourceMap(new sourcemap.SourceMapConsumer(inputSourceMap), filename);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | return {
|
---|
| 113 | code: result.css,
|
---|
| 114 | map: result.map && result.map.toJSON()
|
---|
| 115 | };
|
---|
| 116 | }
|
---|
| 117 | /* istanbul ignore next */
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | async function cleanCssMinify(data, inputSourceMap, minimizerOptions) {
|
---|
| 121 | // eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
---|
| 122 | const CleanCSS = require('clean-css');
|
---|
| 123 |
|
---|
| 124 | const [[name, input]] = Object.entries(data);
|
---|
| 125 | const result = await new CleanCSS({
|
---|
| 126 | sourceMap: inputSourceMap,
|
---|
| 127 | ...minimizerOptions
|
---|
| 128 | }).minify({
|
---|
| 129 | [name]: {
|
---|
| 130 | styles: input
|
---|
| 131 | }
|
---|
| 132 | });
|
---|
| 133 | return {
|
---|
| 134 | code: result.styles,
|
---|
| 135 | map: result.sourceMap && result.sourceMap.toJSON(),
|
---|
| 136 | warnings: result.warnings
|
---|
| 137 | };
|
---|
| 138 | } |
---|