-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (50 loc) · 1.33 KB
/
index.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
import esbuild from 'esbuild'
import postcss from 'postcss'
import env from 'postcss-preset-env'
import atimport from 'postcss-import'
import cssnano from 'cssnano'
import { readFile, writeFile } from 'fs/promises'
const module = code => `
const style = new CSSStyleSheet
style.replace(\`${code}\`)
export default style
`
const name = 'CSS module'
const filter = /\.css$/
export default config => ({
name,
setup (build) {
const { minify } = build.initialOptions
const plugins = [
env({
stage: 0,
browsers: 'safari 15',
features: { 'cascade-layers': false }
}),
...config.plugins || []
]
build.onLoad({ filter }, async ({ path: from, with: { type } }) => {
const { code } = await readFile(from, 'utf-8').then(
code => esbuild.transform(code, { loader: 'css', minify })
)
plugins.unshift(
atimport({
resolve: async (id, resolveDir) => {
if (id.startsWith('.')) return id
const { path, errors = [] } = await build.resolve(id, { resolveDir, kind: 'import-statement' })
if (errors.length)
throw new Error(errors[0].text)
return path
}
})
)
const { css } = await postcss(plugins).process(code, { from })
if (type === 'css') return {
loader: 'js', contents: module(css)
}
else return {
loader: 'css', contents: css
}
})
}
})