Skip to content

Commit 04a3347

Browse files
feat: added lightningcss support (#192)
1 parent a6b5ee6 commit 04a3347

File tree

10 files changed

+2018
-1506
lines changed

10 files changed

+2018
-1506
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ Useful for using and testing unpublished versions or forks.
231231

232232
Possible options:
233233

234-
- CssMinimizerPlugin.cssnanoMinify
235-
- CssMinimizerPlugin.cssoMinify
236-
- CssMinimizerPlugin.cleanCssMinify
237-
- CssMinimizerPlugin.esbuildMinify
238-
- CssMinimizerPlugin.parcelCssMinify
234+
- `CssMinimizerPlugin.cssnanoMinify`
235+
- `CssMinimizerPlugin.cssoMinify`
236+
- `CssMinimizerPlugin.cleanCssMinify`
237+
- `CssMinimizerPlugin.esbuildMinify`
238+
- `CssMinimizerPlugin.lightningCssMinify` (previously`CssMinimizerPlugin.parcelCssMinify`, the package was renamed, but we keep it for backward compatibility)
239239
- `async (data, inputMap, minimizerOptions) => {return {code: "a{color: red}", map: "...", warnings: [], errors: []}}`
240240

241241
> **Warning**
@@ -558,7 +558,7 @@ module.exports = {
558558
};
559559
```
560560

561-
### Using custom minifier [@parcel/css](https://github.com/parcel-bundler/parcel-css)
561+
### Using custom minifier [lightningcss](https://github.com/parcel-bundler/lightningcss), previously `@parcel/css`
562562

563563
**webpack.config.js**
564564

@@ -570,7 +570,7 @@ module.exports = {
570570
minimize: true,
571571
minimizer: [
572572
new CssMinimizerPlugin({
573-
minify: CssMinimizerPlugin.parcelCssMinify,
573+
minify: CssMinimizerPlugin.lightningCssMinify,
574574
// Uncomment this line for options
575575
// minimizerOptions: { targets: { ie: 11 }, drafts: { nesting: true } },
576576
}),

package-lock.json

Lines changed: 1721 additions & 1468 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,53 +56,57 @@
5656
},
5757
"@parcel/css": {
5858
"optional": true
59+
},
60+
"lightningcss": {
61+
"optional": true
5962
}
6063
},
6164
"dependencies": {
62-
"cssnano": "^5.1.13",
65+
"cssnano": "^5.1.8",
6366
"jest-worker": "^27.5.1",
64-
"postcss": "^8.4.16",
67+
"postcss": "^8.4.13",
6568
"schema-utils": "^4.0.0",
6669
"serialize-javascript": "^6.0.0",
6770
"source-map": "^0.6.1"
6871
},
6972
"devDependencies": {
70-
"@babel/cli": "^7.18.10",
71-
"@babel/core": "^7.18.13",
72-
"@babel/preset-env": "^7.18.10",
73-
"@commitlint/cli": "^17.1.2",
74-
"@commitlint/config-conventional": "^17.1.0",
75-
"@parcel/css": "^1.13.0",
73+
"@babel/cli": "^7.17.10",
74+
"@babel/core": "^7.17.12",
75+
"@babel/preset-env": "^7.17.12",
76+
"@commitlint/cli": "^17.0.0",
77+
"@commitlint/config-conventional": "^17.0.0",
78+
"@parcel/css": "^1.8.3",
7679
"@types/clean-css": "^4.2.5",
7780
"@types/csso": "^5.0.0",
7881
"@types/serialize-javascript": "^5.0.2",
7982
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
80-
"babel-jest": "^28.1.2",
81-
"clean-css": "^5.3.1",
83+
"babel-jest": "^28.1.0",
84+
"clean-css": "^5.3.0",
8285
"copy-webpack-plugin": "^9.1.0",
8386
"cross-env": "^7.0.3",
8487
"css-loader": "^6.7.1",
8588
"cssnano-preset-simple": "^4.0.0",
86-
"csso": "^5.0.5",
87-
"del": "^6.1.1",
88-
"del-cli": "^4.0.0",
89-
"esbuild": "^0.14.54",
90-
"eslint": "^8.23.0",
89+
"csso": "^5.0.3",
90+
"del": "^6.1.0",
91+
"del-cli": "^5.0.0",
92+
"esbuild": "^0.15.7",
93+
"eslint": "^8.15.0",
9194
"eslint-config-prettier": "^8.5.0",
9295
"eslint-plugin-import": "^2.26.0",
9396
"husky": "^8.0.1",
94-
"jest": "^28.1.3",
95-
"lint-staged": "^12.5.0",
96-
"memfs": "^3.4.7",
97-
"mini-css-extract-plugin": "^2.6.1",
97+
"jest": "^28.1.0",
98+
"lightningcss": "^1.14.0",
99+
"lint-staged": "^12.4.1",
100+
"memfs": "^3.4.1",
101+
"mini-css-extract-plugin": "^2.6.0",
98102
"npm-run-all": "^4.1.5",
99-
"prettier": "^2.7.1",
100-
"sass": "^1.54.8",
101-
"sass-loader": "^13.0.2",
103+
"prettier": "^2.6.2",
104+
"sass": "^1.51.0",
105+
"sass-loader": "^13.0.0",
102106
"standard-version": "^9.5.0",
103107
"sugarss": "^4.0.1",
104-
"typescript": "^4.8.2",
105-
"webpack": "^5.74.0"
108+
"typescript": "^4.6.4",
109+
"webpack": "^5.72.1"
106110
},
107111
"keywords": [
108112
"cssnano",

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ const {
1212
cleanCssMinify,
1313
esbuildMinify,
1414
parcelCssMinify,
15+
lightningCssMinify,
1516
} = require("./utils");
1617

1718
const schema = require("./options.json");
18-
const { minify } = require("./minify");
19+
const { minify: minifyWorker } = require("./minify");
1920

2021
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
2122
/** @typedef {import("webpack").Compiler} Compiler */
@@ -535,7 +536,7 @@ class CssMinimizerPlugin {
535536
try {
536537
result = await (getWorker
537538
? getWorker().transform(serialize(options))
538-
: minify(options));
539+
: minifyWorker(options));
539540
} catch (error) {
540541
const hasSourceMap =
541542
inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);
@@ -721,5 +722,6 @@ CssMinimizerPlugin.cssoMinify = cssoMinify;
721722
CssMinimizerPlugin.cleanCssMinify = cleanCssMinify;
722723
CssMinimizerPlugin.esbuildMinify = esbuildMinify;
723724
CssMinimizerPlugin.parcelCssMinify = parcelCssMinify;
725+
CssMinimizerPlugin.lightningCssMinify = lightningCssMinify;
724726

725727
module.exports = CssMinimizerPlugin;

src/utils.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
306306
? result.warnings.map((item) => {
307307
return {
308308
source: item.location && item.location.file,
309-
// eslint-disable-next-line no-undefined
310309
line:
311310
item.location && item.location.line
312311
? item.location.line
313-
: undefined,
314-
// eslint-disable-next-line no-undefined
312+
: // eslint-disable-next-line no-undefined
313+
undefined,
315314
column:
316315
item.location && item.location.column
317316
? item.location.column
318-
: undefined,
317+
: // eslint-disable-next-line no-undefined
318+
undefined,
319319
plugin: item.pluginName,
320320
message: `${item.text}${
321321
item.detail ? `\nDetails:\n${item.detail}` : ""
@@ -347,6 +347,7 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
347347
};
348348
}
349349

350+
// TODO remove in the next major release
350351
/* istanbul ignore next */
351352
/**
352353
* @param {Input} input
@@ -391,11 +392,56 @@ async function parcelCssMinify(input, sourceMap, minimizerOptions) {
391392
};
392393
}
393394

395+
/* istanbul ignore next */
396+
/**
397+
* @param {Input} input
398+
* @param {RawSourceMap | undefined} sourceMap
399+
* @param {CustomOptions} minimizerOptions
400+
* @return {Promise<MinimizedResult>}
401+
*/
402+
async function lightningCssMinify(input, sourceMap, minimizerOptions) {
403+
const [[filename, code]] = Object.entries(input);
404+
/**
405+
* @param {Partial<import("lightningcss").TransformOptions>} [lightningCssOptions={}]
406+
* @returns {import("lightningcss").TransformOptions}
407+
*/
408+
const buildLightningCssOptions = (lightningCssOptions = {}) => {
409+
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
410+
return {
411+
minify: true,
412+
...lightningCssOptions,
413+
sourceMap: false,
414+
filename,
415+
code: Buffer.from(code),
416+
};
417+
};
418+
419+
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
420+
const lightningCss = require("lightningcss");
421+
422+
// Copy `esbuild` options
423+
const lightningCssOptions = buildLightningCssOptions(minimizerOptions);
424+
425+
// Let `esbuild` generate a SourceMap
426+
if (sourceMap) {
427+
lightningCssOptions.sourceMap = true;
428+
}
429+
430+
const result = await lightningCss.transform(lightningCssOptions);
431+
432+
return {
433+
code: result.code.toString(),
434+
// eslint-disable-next-line no-undefined
435+
map: result.map ? JSON.parse(result.map.toString()) : undefined,
436+
};
437+
}
438+
394439
module.exports = {
395440
throttleAll,
396441
cssnanoMinify,
397442
cssoMinify,
398443
cleanCssMinify,
399444
esbuildMinify,
400445
parcelCssMinify,
446+
lightningCssMinify,
401447
};

test/CssMinimizerPlugin.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ describe("CssMinimizerPlugin", () => {
191191
CssMinimizerPlugin.buildWarning(
192192
"Warning test.css:1:1",
193193
"test.css",
194+
// eslint-disable-next-line no-undefined
194195
undefined,
195196
new SourceMapConsumer(rawSourceMap)
196197
)
@@ -199,6 +200,7 @@ describe("CssMinimizerPlugin", () => {
199200
CssMinimizerPlugin.buildWarning(
200201
"Warning test.css:1:1",
201202
"test.css",
203+
// eslint-disable-next-line no-undefined
202204
undefined,
203205
new SourceMapConsumer(rawSourceMap),
204206
new RequestShortener("/example.com/www/js/")
@@ -483,6 +485,7 @@ describe("CssMinimizerPlugin", () => {
483485
for (const assetName of Object.keys(assets)) {
484486
const [, webpackHash] = assetName.match(/^.+?\.(.+?)\..+$/);
485487
const { hashDigestLength, hashDigest, hashFunction } = output;
488+
// eslint-disable-next-line global-require
486489
const cryptoHash = require("webpack")
487490
.util.createHash(hashFunction)
488491
.update(readAsset(assetName, compiler, stats))

test/__snapshots__/minify-option.test.js.snap

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,50 @@ exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" min
315315

316316
exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier: warning 1`] = `Array []`;
317317

318+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and generate source maps #2: assets 1`] = `
319+
Object {
320+
"foo/foo.css": "body{color:red;font-weight:700}body a{text-align:center}
321+
/*# sourceMappingURL=foo.css.map*/",
322+
"foo/foo.css.map": "{\\"version\\":3,\\"file\\":\\"foo/foo.css\\",\\"mappings\\":\\"AAAA,+BCIE\\",\\"sources\\":[\\"webpack:///./sourcemap/bar.scss\\",\\"webpack:///./sourcemap/foo.scss\\"],\\"sourcesContent\\":[\\"body {\\\\n font-weight: bold;\\\\n}\\",\\"@import 'bar';\\\\n\\\\nbody {\\\\n color: red;\\\\n a {\\\\n text-align: center;\\\\n }\\\\n}\\"],\\"names\\":[],\\"sourceRoot\\":\\"\\"}",
323+
}
324+
`;
325+
326+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and generate source maps #2: error 1`] = `Array []`;
327+
328+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and generate source maps #2: warning 1`] = `Array []`;
329+
330+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and generate source maps: assets 1`] = `
331+
Object {
332+
"foo.css": "body{color:red;font-weight:700}body a{text-align:center}
333+
/*# sourceMappingURL=foo.css.map*/",
334+
"foo.css.map": "{\\"version\\":3,\\"file\\":\\"foo.css\\",\\"mappings\\":\\"AAAA,+BCIE\\",\\"sources\\":[\\"webpack:///./sourcemap/bar.scss\\",\\"webpack:///./sourcemap/foo.scss\\"],\\"sourcesContent\\":[\\"body {\\\\n font-weight: bold;\\\\n}\\",\\"@import 'bar';\\\\n\\\\nbody {\\\\n color: red;\\\\n a {\\\\n text-align: center;\\\\n }\\\\n}\\"],\\"names\\":[],\\"sourceRoot\\":\\"\\"}",
335+
}
336+
`;
337+
338+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and generate source maps: error 1`] = `Array []`;
339+
340+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and generate source maps: warning 1`] = `Array []`;
341+
342+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and options for "lightningcss": assets 1`] = `
343+
Object {
344+
"foo.css": "table.colortable td{text-align:center}table.colortable td.c{text-transform:uppercase}table.colortable td:first-child,table.colortable td:first-child+td{border:1px solid #000}table.colortable th{text-align:center;color:#fff;background:#000}.example{user-select:none;background:linear-gradient(#fff,#000);transition:all .5s;display:grid}",
345+
}
346+
`;
347+
348+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and options for "lightningcss": error 1`] = `Array []`;
349+
350+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier and options for "lightningcss": warning 1`] = `Array []`;
351+
352+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier: assets 1`] = `
353+
Object {
354+
"foo.css": "body{color:red;font-weight:700}body a{text-align:center}",
355+
}
356+
`;
357+
358+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier: error 1`] = `Array []`;
359+
360+
exports[`"minify" option should work with "CssMinimizerPlugin.lightningCssMinify" minifier: warning 1`] = `Array []`;
361+
318362
exports[`"minify" option should work with "CssMinimizerPlugin.parcelCssMinify" minifier and generate source maps #2: assets 1`] = `
319363
Object {
320364
"foo/foo.css": "body{color:red;font-weight:700}body a{text-align:center}

0 commit comments

Comments
 (0)