Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
testEnvironment: 'node',
snapshotResolver: './test/helpers/snapshotResolver.js',
};
1,456 changes: 814 additions & 642 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"cacache": "^15.0.5",
"cssnano": "^4.1.10",
"find-cache-dir": "^3.3.1",
"jest-worker": "^26.1.0",
"jest-worker": "^26.2.0",
"p-limit": "^3.0.2",
"schema-utils": "^2.7.0",
"serialize-javascript": "^4.0.0",
Expand All @@ -59,8 +59,9 @@
"@commitlint/config-conventional": "^9.1.1",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.1.0",
"babel-jest": "^26.2.0",
"commitlint-azure-pipelines-cli": "^1.0.3",
"copy-webpack-plugin": "^6.0.3",
"cross-env": "^7.0.2",
"css-loader": "^3.6.0",
"del": "^5.1.0",
Expand All @@ -69,17 +70,18 @@
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"jest": "^26.2.0",
"jest-junit": "^11.0.1",
"lint-staged": "^10.2.11",
"memfs": "^3.2.0",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.14.1",
"npm-run-all": "^4.1.5",
"postcss": "^7.0.32",
"prettier": "^2.0.5",
"sass-loader": "^9.0.2",
"standard-version": "^8.0.2",
"webpack": "^4.44.0"
"webpack": "^4.44.1"
},
"keywords": [
"cssnano",
Expand Down
42 changes: 12 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CssnanoPlugin {
parallel = true,
include,
exclude,
minify,
} = options;

this.options = {
Expand All @@ -53,6 +54,7 @@ class CssnanoPlugin {
parallel,
include,
exclude,
minify,
};

if (this.options.sourceMap === true) {
Expand Down Expand Up @@ -86,7 +88,7 @@ class CssnanoPlugin {
sourceMap &&
sourceMap.originalPositionFor({
line: error.line,
column: error.col,
column: error.column,
});

if (original && original.source && requestShortener) {
Expand All @@ -95,7 +97,7 @@ class CssnanoPlugin {
error.message
} [${requestShortener.shorten(original.source)}:${original.line},${
original.column
}][${file}:${error.line},${error.col}]${
}][${file}:${error.line},${error.column}]${
error.stack
? `\n${error.stack.split('\n').slice(1).join('\n')}`
: ''
Expand All @@ -106,7 +108,7 @@ class CssnanoPlugin {
return new Error(
`${file} from Cssnano Webpack Plugin\n${error.message} [${file}:${
error.line
},${error.col}]${
},${error.column}]${
error.stack ? `\n${error.stack.split('\n').slice(1).join('\n')}` : ''
}`
);
Expand Down Expand Up @@ -273,19 +275,13 @@ class CssnanoPlugin {

const postcssOptions = { to: file, from: file, map: false };

if (inputSourceMap) {
postcssOptions.map = Object.assign(
{ prev: inputSourceMap },
this.options.sourceMap
);
}

const task = {
file,
input,
inputSourceMap,
postcssOptions,
map: this.options.sourceMap,
cssnanoOptions: this.options.cssnanoOptions,
minify: this.options.minify,
callback,
};

Expand Down Expand Up @@ -377,7 +373,7 @@ class CssnanoPlugin {
if (worker) {
taskResult = await worker.transform(serialize(task));
} else {
taskResult = minifyFn(task);
taskResult = await minifyFn(task);
}
} catch (error) {
taskResult = { error };
Expand Down Expand Up @@ -452,23 +448,9 @@ class CssnanoPlugin {
);

const optimizeFn = async (compilation, chunksOrAssets) => {
let assetNames;

if (CssnanoPlugin.isWebpack4()) {
assetNames = []
.concat(Array.from(compilation.additionalChunkAssets || []))
.concat(
Array.from(chunksOrAssets).reduce(
(acc, chunk) => acc.concat(Array.from(chunk.files || [])),
[]
)
)
.filter((file) => matchObject(file));
} else {
assetNames = []
.concat(Object.keys(chunksOrAssets))
.filter((file) => matchObject(file));
}
const assetNames = Object.keys(
CssnanoPlugin.isWebpack4() ? compilation.assets : chunksOrAssets
).filter((file) => matchObject(file));

if (assetNames.length === 0) {
return Promise.resolve();
Expand Down Expand Up @@ -545,4 +527,4 @@ class CssnanoPlugin {
}
}

module.exports = CssnanoPlugin;
export default CssnanoPlugin;
38 changes: 28 additions & 10 deletions src/minify.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
const cssnano = require('cssnano');

const minify = (options) => {
const { input, postcssOptions, cssnanoOptions } = options;
const minify = async (options) => {
const {
input,
postcssOptions,
cssnanoOptions,
map,
inputSourceMap,
minify: minifyFn,
} = options;

return cssnano.process(input, postcssOptions, cssnanoOptions);
if (minifyFn) {
return minifyFn({ input, postcssOptions, cssnanoOptions }, inputSourceMap);
}

if (inputSourceMap) {
postcssOptions.map = { prev: inputSourceMap, ...map };
}

const result = await cssnano.process(input, postcssOptions, cssnanoOptions);

return {
css: result.css,
map: result.map,
error: result.error,
warnings: result.warnings(),
};
};

function transform(options) {
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
Expand All @@ -19,13 +41,9 @@ function transform(options) {
`'use strict'\nreturn ${options}`
)(exports, require, module, __filename, __dirname);

const result = minify(options);
const result = await minify(options);

if (result.error) {
throw result.error;
} else {
return result;
}
return result;
}

module.exports.minify = minify;
Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
"warningsFilter": {
"description": "Allow to filter `cssnano` warnings.",
"instanceof": "Function"
},
"minify": {
"description": "Allows you to override default minify function.",
"instanceof": "Function"
}
},
"additionalProperties": false
Expand Down
55 changes: 0 additions & 55 deletions test/CssNano.test.js

This file was deleted.

Loading