Skip to content

Commit a4867f9

Browse files
test: code (#10)
1 parent 266bd3b commit a4867f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3806
-782
lines changed

jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
snapshotResolver: './test/helpers/snapshotResolver.js',
4+
};

package-lock.json

+814-642
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"cacache": "^15.0.5",
4545
"cssnano": "^4.1.10",
4646
"find-cache-dir": "^3.3.1",
47-
"jest-worker": "^26.1.0",
47+
"jest-worker": "^26.2.0",
4848
"p-limit": "^3.0.2",
4949
"schema-utils": "^2.7.0",
5050
"serialize-javascript": "^4.0.0",
@@ -59,8 +59,9 @@
5959
"@commitlint/config-conventional": "^9.1.1",
6060
"@webpack-contrib/defaults": "^6.3.0",
6161
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
62-
"babel-jest": "^26.1.0",
62+
"babel-jest": "^26.2.0",
6363
"commitlint-azure-pipelines-cli": "^1.0.3",
64+
"copy-webpack-plugin": "^6.0.3",
6465
"cross-env": "^7.0.2",
6566
"css-loader": "^3.6.0",
6667
"del": "^5.1.0",
@@ -69,17 +70,18 @@
6970
"eslint-config-prettier": "^6.11.0",
7071
"eslint-plugin-import": "^2.22.0",
7172
"husky": "^4.2.5",
72-
"jest": "^26.1.0",
73+
"jest": "^26.2.0",
7374
"jest-junit": "^11.0.1",
7475
"lint-staged": "^10.2.11",
7576
"memfs": "^3.2.0",
7677
"mini-css-extract-plugin": "^0.9.0",
7778
"node-sass": "^4.14.1",
7879
"npm-run-all": "^4.1.5",
80+
"postcss": "^7.0.32",
7981
"prettier": "^2.0.5",
8082
"sass-loader": "^9.0.2",
8183
"standard-version": "^8.0.2",
82-
"webpack": "^4.44.0"
84+
"webpack": "^4.44.1"
8385
},
8486
"keywords": [
8587
"cssnano",

src/index.js

+12-30
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CssnanoPlugin {
4141
parallel = true,
4242
include,
4343
exclude,
44+
minify,
4445
} = options;
4546

4647
this.options = {
@@ -53,6 +54,7 @@ class CssnanoPlugin {
5354
parallel,
5455
include,
5556
exclude,
57+
minify,
5658
};
5759

5860
if (this.options.sourceMap === true) {
@@ -86,7 +88,7 @@ class CssnanoPlugin {
8688
sourceMap &&
8789
sourceMap.originalPositionFor({
8890
line: error.line,
89-
column: error.col,
91+
column: error.column,
9092
});
9193

9294
if (original && original.source && requestShortener) {
@@ -95,7 +97,7 @@ class CssnanoPlugin {
9597
error.message
9698
} [${requestShortener.shorten(original.source)}:${original.line},${
9799
original.column
98-
}][${file}:${error.line},${error.col}]${
100+
}][${file}:${error.line},${error.column}]${
99101
error.stack
100102
? `\n${error.stack.split('\n').slice(1).join('\n')}`
101103
: ''
@@ -106,7 +108,7 @@ class CssnanoPlugin {
106108
return new Error(
107109
`${file} from Cssnano Webpack Plugin\n${error.message} [${file}:${
108110
error.line
109-
},${error.col}]${
111+
},${error.column}]${
110112
error.stack ? `\n${error.stack.split('\n').slice(1).join('\n')}` : ''
111113
}`
112114
);
@@ -273,19 +275,13 @@ class CssnanoPlugin {
273275

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

276-
if (inputSourceMap) {
277-
postcssOptions.map = Object.assign(
278-
{ prev: inputSourceMap },
279-
this.options.sourceMap
280-
);
281-
}
282-
283278
const task = {
284-
file,
285279
input,
286280
inputSourceMap,
287281
postcssOptions,
282+
map: this.options.sourceMap,
288283
cssnanoOptions: this.options.cssnanoOptions,
284+
minify: this.options.minify,
289285
callback,
290286
};
291287

@@ -377,7 +373,7 @@ class CssnanoPlugin {
377373
if (worker) {
378374
taskResult = await worker.transform(serialize(task));
379375
} else {
380-
taskResult = minifyFn(task);
376+
taskResult = await minifyFn(task);
381377
}
382378
} catch (error) {
383379
taskResult = { error };
@@ -452,23 +448,9 @@ class CssnanoPlugin {
452448
);
453449

454450
const optimizeFn = async (compilation, chunksOrAssets) => {
455-
let assetNames;
456-
457-
if (CssnanoPlugin.isWebpack4()) {
458-
assetNames = []
459-
.concat(Array.from(compilation.additionalChunkAssets || []))
460-
.concat(
461-
Array.from(chunksOrAssets).reduce(
462-
(acc, chunk) => acc.concat(Array.from(chunk.files || [])),
463-
[]
464-
)
465-
)
466-
.filter((file) => matchObject(file));
467-
} else {
468-
assetNames = []
469-
.concat(Object.keys(chunksOrAssets))
470-
.filter((file) => matchObject(file));
471-
}
451+
const assetNames = Object.keys(
452+
CssnanoPlugin.isWebpack4() ? compilation.assets : chunksOrAssets
453+
).filter((file) => matchObject(file));
472454

473455
if (assetNames.length === 0) {
474456
return Promise.resolve();
@@ -545,4 +527,4 @@ class CssnanoPlugin {
545527
}
546528
}
547529

548-
module.exports = CssnanoPlugin;
530+
export default CssnanoPlugin;

src/minify.js

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
const cssnano = require('cssnano');
22

3-
const minify = (options) => {
4-
const { input, postcssOptions, cssnanoOptions } = options;
3+
const minify = async (options) => {
4+
const {
5+
input,
6+
postcssOptions,
7+
cssnanoOptions,
8+
map,
9+
inputSourceMap,
10+
minify: minifyFn,
11+
} = options;
512

6-
return cssnano.process(input, postcssOptions, cssnanoOptions);
13+
if (minifyFn) {
14+
return minifyFn({ input, postcssOptions, cssnanoOptions }, inputSourceMap);
15+
}
16+
17+
if (inputSourceMap) {
18+
postcssOptions.map = { prev: inputSourceMap, ...map };
19+
}
20+
21+
const result = await cssnano.process(input, postcssOptions, cssnanoOptions);
22+
23+
return {
24+
css: result.css,
25+
map: result.map,
26+
error: result.error,
27+
warnings: result.warnings(),
28+
};
729
};
830

9-
function transform(options) {
31+
async function transform(options) {
1032
// 'use strict' => this === undefined (Clean Scope)
1133
// Safer for possible security issues, albeit not critical at all here
1234
// eslint-disable-next-line no-new-func, no-param-reassign
@@ -19,13 +41,9 @@ function transform(options) {
1941
`'use strict'\nreturn ${options}`
2042
)(exports, require, module, __filename, __dirname);
2143

22-
const result = minify(options);
44+
const result = await minify(options);
2345

24-
if (result.error) {
25-
throw result.error;
26-
} else {
27-
return result;
28-
}
46+
return result;
2947
}
3048

3149
module.exports.minify = minify;

src/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@
107107
"warningsFilter": {
108108
"description": "Allow to filter `cssnano` warnings.",
109109
"instanceof": "Function"
110+
},
111+
"minify": {
112+
"description": "Allows you to override default minify function.",
113+
"instanceof": "Function"
110114
}
111115
},
112116
"additionalProperties": false

test/CssNano.test.js

-55
This file was deleted.

0 commit comments

Comments
 (0)