/
postcss.ts
142 lines (122 loc) · 3.83 KB
/
postcss.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import fs from 'fs'
import path from 'path'
import { Loader, Plugin, transform } from 'esbuild'
import { getPostcss } from '../utils'
export const postcssPlugin = ({
css,
inject,
cssLoader,
}: {
css?: Map<string, string>
inject?: boolean
cssLoader?: Loader
}): Plugin => {
return {
name: 'postcss',
setup(build) {
const configCache = new Map()
const getPostcssConfig = async (file: string) => {
const loadConfig = require('postcss-load-config')
if (configCache.has(file)) {
return configCache.get(file)
}
try {
const result = await loadConfig({}, path.dirname(file))
configCache.set(file, result)
return result
} catch (error: any) {
if (error.message.includes('No PostCSS Config found in')) {
const result = { plugins: [], options: {} }
return result
}
throw error
}
}
build.onResolve({ filter: /^#style-inject$/ }, () => {
return { path: '#style-inject', namespace: '#style-inject' }
})
build.onLoad(
{ filter: /^#style-inject$/, namespace: '#style-inject' },
() => {
return {
// Taken from https://github.com/egoist/style-inject/blob/master/src/index.js (MIT)
contents: `
export default function styleInject(css, { insertAt } = {}) {
if (!css || typeof document === 'undefined') return
const head = document.head || document.getElementsByTagName('head')[0]
const style = document.createElement('style')
style.type = 'text/css'
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild)
} else {
head.appendChild(style)
}
} else {
head.appendChild(style)
}
if (style.styleSheet) {
style.styleSheet.cssText = css
} else {
style.appendChild(document.createTextNode(css))
}
}
`,
loader: 'js',
}
}
)
build.onLoad({ filter: /\.css$/ }, async (args) => {
let contents: string
if (css && args.path.endsWith('.svelte.css')) {
contents = css.get(args.path)!
} else {
contents = await fs.promises.readFile(args.path, 'utf8')
}
// Load postcss config
const { plugins, options } = await getPostcssConfig(args.path)
if (plugins && plugins.length > 0) {
// Load postcss
const postcss = getPostcss()
if (!postcss) {
return {
errors: [
{
text: `postcss is not installed`,
},
],
}
}
// Transform CSS
const result = await postcss
?.default(plugins)
.process(contents, { ...options, from: args.path })
contents = result.css
}
if (inject) {
contents = (
await transform(contents, {
minify: build.initialOptions.minify,
minifyIdentifiers: build.initialOptions.minifyIdentifiers,
minifySyntax: build.initialOptions.minifySyntax,
minifyWhitespace: build.initialOptions.minifyWhitespace,
logLevel: build.initialOptions.logLevel,
loader: 'css',
})
).code
contents = `import styleInject from '#style-inject';styleInject(${JSON.stringify(
contents
)})`
return {
contents,
loader: 'js',
}
}
return {
contents,
loader: cssLoader ?? 'css',
}
})
},
}
}