-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
236 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const browserslist = require("browserslist"); | ||
const lightningcss = require("lightningcss"); | ||
|
||
const decoder = new TextDecoder(); | ||
function wrapCSS(text, type) { | ||
switch (type) { | ||
case "inline": | ||
return "section{" + text + "}"; | ||
case "media": | ||
return "@media " + text + "{section{top:0}}"; | ||
default: | ||
return text; | ||
} | ||
} | ||
function unwrapCSS(text, type) { | ||
let matches; | ||
switch (type) { | ||
case "inline": | ||
matches = text.match(/^section\{([\s\S]*)\}$/); | ||
break; | ||
case "media": | ||
matches = text.match(/^@media ([\s\S]*?)\s*{[\s\S]*}$/); | ||
break; | ||
} | ||
return matches ? matches[1] : text; | ||
} | ||
|
||
module.exports.getMinifyCSS = ({ latestBuild, isProdBuild }) => { | ||
const cssTargets = lightningcss.browserslistToTargets( | ||
browserslist( | ||
browserslist.loadConfig({ | ||
path: ".", | ||
env: latestBuild ? "modern" : "legacy", | ||
}), | ||
{ throwOnMissing: true, mobileToDesktop: true } | ||
) | ||
); | ||
return (text, type) => { | ||
const input = wrapCSS(text, type); | ||
const { code, warnings } = lightningcss.transform({ | ||
filename: "style.css", | ||
code: Buffer.from(input), | ||
minify: isProdBuild, | ||
targets: cssTargets, | ||
}); | ||
if (warnings.length > 0) { | ||
console.warn("Warnings while transforming CSS:", ...warnings); | ||
} | ||
const output = decoder.decode(code); | ||
return unwrapCSS(output, type); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters