/
build-css.js
97 lines (85 loc) · 2.86 KB
/
build-css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const sass = require('sass')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
const postcssLogical = require('postcss-logical')
const postcssDirPseudoClass = require('postcss-dir-pseudo-class')
const cssnano = require('cssnano')
const { promisify } = require('node:util')
const fs = require('node:fs')
const path = require('node:path')
const resolve = require('resolve')
const glob = promisify(require('glob'))
const renderScss = promisify(sass.render)
const { mkdir, writeFile } = fs.promises
const cwd = process.cwd()
let chalk
function handleErr (err) {
console.error(chalk.red('✗ Error:'), chalk.red(err.message))
}
async function compileCSS () {
({ default:chalk } = await import('chalk'))
const files = await glob('packages/{,@uppy/}*/src/style.scss')
for (const file of files) {
const importedFiles = new Set()
const scssResult = await renderScss({
file,
importer (url, from, done) {
resolve(url, {
basedir: path.dirname(from),
filename: from,
extensions: ['.scss'],
}, (err, resolved) => {
if (err) {
done(err)
return
}
const realpath = fs.realpathSync(resolved)
if (importedFiles.has(realpath)) {
done({ contents: '' })
return
}
importedFiles.add(realpath)
done({ file: realpath })
})
},
})
const plugins = [
autoprefixer,
postcssLogical(),
postcssDirPseudoClass(),
]
const postcssResult = await postcss(plugins)
.process(scssResult.css, { from: file })
postcssResult.warnings().forEach((warn) => {
console.warn(warn.toString())
})
const outdir = path.join(path.dirname(file), '../dist')
// Save the `uppy` package's CSS as `uppy.css`,
// the rest as `style.css`.
// const outfile = path.join(outdir, outdir.includes(path.normalize('packages/uppy/')) ? 'uppy.css' : 'style.css')
let outfile = path.join(outdir, 'style.css')
if (outdir.includes(path.normalize('packages/uppy/'))) {
outfile = path.join(outdir, 'uppy.css')
}
await mkdir(outdir, { recursive: true })
await writeFile(outfile, postcssResult.css)
console.info(
chalk.green('✓ Built Uppy CSS:'),
chalk.magenta(path.relative(cwd, outfile)),
)
const minifiedResult = await postcss([
cssnano({ safe: true }),
]).process(postcssResult.css, { from: outfile })
minifiedResult.warnings().forEach((warn) => {
console.warn(warn.toString())
})
await writeFile(outfile.replace(/\.css$/, '.min.css'), minifiedResult.css)
console.info(
chalk.green('✓ Minified Bundle CSS:'),
chalk.magenta(path.relative(cwd, outfile).replace(/\.css$/, '.min.css')),
)
}
}
compileCSS().then(() => {
console.info(chalk.yellow('✓ CSS Bundles 🎉'))
}, handleErr)