-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathbuildPluginCss.ts
120 lines (110 loc) · 3.39 KB
/
buildPluginCss.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
import path from 'path'
import { Plugin } from 'rollup'
import {
AssetsOptions,
getAssetPublicPath,
registerAssets
} from './buildPluginAsset'
import { loadPostcssConfig } from './config'
import { isExternalUrl } from './utils'
const debug = require('debug')('vite:build:css')
const urlRE = /(url\(\s*['"]?)([^"')]+)(["']?\s*\))/
export const createBuildCssPlugin = (
root: string,
publicBase: string,
assetsDir: string,
cssFileName: string,
minify: boolean,
assetsOptions: AssetsOptions
): Plugin => {
const styles: Map<string, string> = new Map()
const assets = new Map()
return {
name: 'vite:css',
async transform(css: string, id: string) {
if (id.endsWith('.css')) {
// process url() - register referenced files as assets
// and rewrite the url to the resolved public path
if (urlRE.test(css)) {
const fileDir = path.dirname(id)
let match
let remaining = css
let rewritten = ''
while ((match = urlRE.exec(remaining))) {
rewritten += remaining.slice(0, match.index)
const [matched, before, rawUrl, after] = match
if (isExternalUrl(rawUrl)) {
rewritten += matched
remaining = remaining.slice(match.index + matched.length)
return
}
const file = path.join(fileDir, rawUrl)
const { fileName, content, url } = await getAssetPublicPath(
file,
publicBase,
assetsDir,
assetsOptions
)
assets.set(fileName, content)
debug(`url(${rawUrl}) -> url(${url})`)
rewritten += `${before}${url}${after}`
remaining = remaining.slice(match.index + matched.length)
}
css = rewritten + remaining
}
// postcss
let modules
const postcssConfig = await loadPostcssConfig(root)
const expectsModule = id.endsWith('.module.css')
if (postcssConfig || expectsModule) {
try {
const result = await require('postcss')([
...((postcssConfig && postcssConfig.plugins) || []),
...(expectsModule
? [
require('postcss-modules')({
getJSON(_: string, json: Record<string, string>) {
modules = json
}
})
]
: [])
]).process(css, {
...(postcssConfig && postcssConfig.options),
from: id
})
css = result.css
} catch (e) {
console.error(`[vite] error applying postcss transforms: `, e)
}
}
styles.set(id, css)
return modules
? `export default ${JSON.stringify(modules)}`
: '/* css extracted by vite */'
}
},
async generateBundle(_options, bundle) {
let css = ''
// finalize extracted css
styles.forEach((s) => {
css += s
})
// minify with cssnano
if (minify) {
css = (
await require('postcss')([require('cssnano')]).process(css, {
from: undefined
})
).css
}
bundle[cssFileName] = {
isAsset: true,
type: 'asset',
fileName: cssFileName,
source: css
}
registerAssets(assets, bundle)
}
}
}