Skip to content

Commit 70efde2

Browse files
authored
Merge pull request #604 from Stackla/copilot/add-cssnano-integration
Integrate cssnano for CSS optimization
2 parents 2fc499e + 190ada9 commit 70efde2

File tree

3 files changed

+774
-34
lines changed

3 files changed

+774
-34
lines changed

esbuild.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { pathToFileURL } = require("node:url")
22
const postcssUrl = require("postcss-url")
33
const postcss = require("postcss")
4+
const cssnano = require("cssnano")
45
const SVGSpriter = require('svg-sprite');
56

67
function startWebSocketServer() {
@@ -29,16 +30,31 @@ const getTemplatesEndpoint = () => {
2930
}
3031
}
3132

32-
const postcssPlugins = [
33-
postcssUrl({
34-
url: asset => {
35-
if (asset.url.includes("assets/")) {
36-
return `${getTemplatesEndpoint()}/${asset.url}`
33+
const getPostcssPlugins = (env) => {
34+
const plugins = [
35+
postcssUrl({
36+
url: asset => {
37+
if (asset.url.includes("assets/")) {
38+
return `${getTemplatesEndpoint()}/${asset.url}`
39+
}
40+
return asset.url
3741
}
38-
return asset.url
39-
}
40-
})
41-
]
42+
})
43+
]
44+
45+
// Add cssnano for production and staging builds
46+
if (env === "production" || env === "staging") {
47+
plugins.push(cssnano({
48+
preset: ['default', {
49+
discardComments: {
50+
removeAll: true
51+
}
52+
}]
53+
}))
54+
}
55+
56+
return plugins
57+
}
4258

4359
const config = {
4460
shape: {
@@ -146,7 +162,7 @@ async function buildAll() {
146162

147163
const combined = `${result.css.toString()}`
148164

149-
const postCssResult = await postcss(postcssPlugins).process(combined, { from: item.relative() })
165+
const postCssResult = await postcss(getPostcssPlugins(env)).process(combined, { from: item.relative() })
150166
const postCssCombined = postCssResult.css
151167

152168
fs.writeFileSync(`dist/${item.parent.name}/widget.css`, postCssCombined)

0 commit comments

Comments
 (0)