/
css.ts
677 lines (605 loc) · 17.9 KB
/
css.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
import {
// createDebugger,
isExternalUrl,
asyncReplace,
cleanUrl,
generateCodeFrame,
isDataUrl
} from '../utils'
import path from 'path'
import { Plugin } from '../plugin'
import { ResolvedConfig } from '../config'
import postcssrc from 'postcss-load-config'
import merge from 'merge-source-map'
import {
NormalizedOutputOptions,
RenderedChunk,
RollupError,
SourceMap
} from 'rollup'
import { dataToEsm } from '@rollup/pluginutils'
import chalk from 'chalk'
import { CLIENT_PUBLIC_PATH } from '../constants'
import { ProcessOptions, Result, Plugin as PostcssPlugin } from 'postcss'
import { ViteDevServer } from '../'
import { assetUrlRE, urlToBuiltUrl } from './asset'
import MagicString from 'magic-string'
// const debug = createDebugger('vite:css')
export interface CSSOptions {
/**
* https://github.com/css-modules/postcss-modules
*/
modules?: CSSModulesOptions | false
preprocessorOptions?: Record<string, any>
}
export interface CSSModulesOptions {
scopeBehaviour?: 'global' | 'local'
globalModulePaths?: string[]
generateScopedName?:
| string
| ((name: string, filename: string, css: string) => string)
hashPrefix?: string
/**
* default: 'camelCaseOnly'
*/
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
}
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|postcss)($|\\?)`
const cssLangRE = new RegExp(cssLangs)
const cssModuleRE = new RegExp(`\\.module${cssLangs}`)
const directRequestRE = /(\?|&)direct\b/
export const isCSSRequest = (request: string) =>
cssLangRE.test(request) && !directRequestRE.test(request)
export const isDirectCSSRequest = (request: string) =>
cssLangRE.test(request) && directRequestRE.test(request)
const cssModulesCache = new WeakMap<
ResolvedConfig,
Map<string, Record<string, string>>
>()
export const chunkToEmittedCssFileMap = new WeakMap<RenderedChunk, string>()
/**
* Plugin applied before user plugins
*/
export function cssPlugin(config: ResolvedConfig): Plugin {
let server: ViteDevServer
const moduleCache = new Map<string, Record<string, string>>()
cssModulesCache.set(config, moduleCache)
return {
name: 'vite:css',
configureServer(_server) {
server = _server
},
async transform(raw, id) {
if (!cssLangRE.test(id)) {
return
}
let { code: css, modules, deps } = await compileCSS(id, raw, config)
if (modules) {
moduleCache.set(id, modules)
}
if (server) {
// server only logic for handling CSS @import dependency hmr
const { moduleGraph } = server
const thisModule = moduleGraph.getModuleById(id)!
// CSS modules cannot self-accept since it exports values
const isSelfAccepting = !modules
if (deps) {
// record deps in the module graph so edits to @import css can trigger
// main import to hot update
const depModules = new Set(
[...deps].map((file) => moduleGraph.createFileOnlyEntry(file))
)
moduleGraph.updateModuleInfo(
thisModule,
depModules,
// The root CSS proxy module is self-accepting and should not
// have an explicit accept list
new Set(),
isSelfAccepting
)
for (const file of deps) {
this.addWatchFile(file)
}
} else {
thisModule.isSelfAccepting = isSelfAccepting
}
// rewrite urls using current module's url as base
css = await rewriteCssUrls(css, thisModule.url)
} else {
// if build, analyze url() asset reference
// account for comments https://github.com/vitejs/vite/issues/426
css = css.replace(/\/\*[\s\S]*?\*\//gm, '')
if (cssUrlRE.test(css)) {
css = await rewriteCssUrls(css, async (url) =>
urlToBuiltUrl(url, id, config, this)
)
}
}
return css
}
}
}
/**
* Plugin applied after user plugins
*/
export function cssPostPlugin(config: ResolvedConfig): Plugin {
const styles = new Map<string, string>()
const emptyChunks = new Set<string>()
const moduleCache = cssModulesCache.get(config)!
// when there are multiple rollup outputs and extracting CSS, only emit once,
// since output formats have no effect on the generated CSS.
const outputToExtractedCSSMap = new Map<NormalizedOutputOptions, string>()
let hasEmitted = false
return {
name: 'vite:css-post',
transform(css, id, ssr) {
if (!cssLangRE.test(id)) {
return
}
const modules = moduleCache.get(id)
const modulesCode =
modules && dataToEsm(modules, { namedExports: true, preferConst: true })
if (config.command === 'serve') {
if (isDirectCSSRequest(id)) {
return css
} else {
// server only
if (ssr) {
return modulesCode || `export default ${JSON.stringify(css)}`
}
return [
`import { updateStyle, removeStyle } from ${JSON.stringify(
CLIENT_PUBLIC_PATH
)}`,
`const id = ${JSON.stringify(id)}`,
`const css = ${JSON.stringify(css)}`,
`updateStyle(id, css)`,
// css modules exports change on edit so it can't self accept
`${modulesCode || `import.meta.hot.accept()\nexport default css`}`,
`import.meta.hot.prune(() => removeStyle(id))`
].join('\n')
}
}
// build CSS handling ----------------------------------------------------
// record css
styles.set(id, css)
return {
code: modulesCode || `export default ${JSON.stringify(css)}`,
map: null,
// avoid the css module from being tree-shaken so that we can retrieve
// it in renderChunk()
moduleSideEffects: 'no-treeshake'
}
},
async renderChunk(code, chunk, opts) {
let chunkCSS = ''
const ids = Object.keys(chunk.modules)
for (const id of ids) {
if (styles.has(id)) {
chunkCSS += styles.get(id)
}
}
if (!chunkCSS) {
return null
}
// replace asset url references with resolved url
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileId, postfix = '') => {
return config.build.base + this.getFileName(fileId) + postfix
})
if (config.build.cssCodeSplit) {
if (!code.trim()) {
// this is a shared CSS-only chunk that is empty.
emptyChunks.add(chunk.fileName)
}
// minify
if (config.build.minify) {
chunkCSS = await minifyCSS(chunkCSS, config)
}
if (opts.format === 'es') {
// emit corresponding css file
const fileHandle = this.emitFile({
name: chunk.name + '.css',
type: 'asset',
source: chunkCSS
})
chunkToEmittedCssFileMap.set(chunk, fileHandle)
} else {
// legacy build, inline css
const style = `__vite_style__`
const injectCode =
`var ${style} = document.createElement('style');` +
`${style}.innerHTML = ${JSON.stringify(chunkCSS)};` +
`document.head.appendChild(${style});`
if (config.build.sourcemap) {
const s = new MagicString(code)
s.prepend(injectCode)
return {
code: s.toString(),
map: s.generateMap({ hires: true })
}
} else {
return { code: injectCode + code }
}
}
return {
code,
map: null
}
} else {
const extractedCss = outputToExtractedCSSMap.get(opts) || ''
outputToExtractedCSSMap.set(opts, extractedCss + chunkCSS)
return null
}
},
async generateBundle(opts, bundle) {
// remove empty css chunks and their imports
if (emptyChunks.size) {
emptyChunks.forEach((fileName) => {
delete bundle[fileName]
})
const emptyChunkFiles = [...emptyChunks].join('|').replace(/\./g, '\\.')
const emptyChunkRE = new RegExp(
`\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?`,
'g'
)
for (const file in bundle) {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
chunk.code = chunk.code.replace(emptyChunkRE, '')
}
}
}
let extractedCss = outputToExtractedCSSMap.get(opts)
if (extractedCss && !hasEmitted) {
hasEmitted = true
// minify css
if (config.build.minify) {
extractedCss = await minifyCSS(extractedCss, config)
}
this.emitFile({
name: 'style.css',
type: 'asset',
source: extractedCss
})
}
}
}
}
async function compileCSS(
id: string,
code: string,
config: ResolvedConfig
): Promise<{
code: string
map?: SourceMap
ast?: Result
modules?: Record<string, string>
deps?: Set<string>
}> {
const { modules: modulesOptions, preprocessorOptions } = config.css || {}
const isModule = modulesOptions !== false && cssModuleRE.test(id)
// although at serve time it can work without processing, we do need to
// crawl them in order to register watch dependencies.
const needInlineImport = code.includes('@import')
const postcssConfig = await loadPostcssConfig(config.root)
const lang = id.match(cssLangRE)?.[1]
// 1. plain css that needs no processing
if (lang === 'css' && !postcssConfig && !isModule && !needInlineImport) {
return { code }
}
let map: SourceMap | undefined
let modules: Record<string, string> | undefined
const deps = new Set<string>()
// 2. pre-processors: sass etc.
if (lang && lang in preProcessors) {
const preProcessor = preProcessors[lang as PreprocessLang]
let opts = (preprocessorOptions && preprocessorOptions[lang]) || {}
// support @import from node dependencies by default
switch (lang) {
case 'scss':
case 'sass':
opts = {
includePaths: ['node_modules'],
...opts
}
break
case 'less':
case 'styl':
case 'stylus':
opts = {
paths: ['node_modules'],
...opts
}
}
// important: set this for relative import resolving
opts.filename = cleanUrl(id)
const preprocessResult = await preProcessor(code, undefined, opts)
if (preprocessResult.errors.length) {
throw preprocessResult.errors[0]
}
code = preprocessResult.code
map = preprocessResult.map as SourceMap
if (preprocessResult.deps) {
preprocessResult.deps.forEach((dep) => {
// sometimes sass registers the file itself as a dep
if (dep !== opts.filename) {
deps.add(dep)
}
})
}
}
// 3. postcss
const postcssOptions = (postcssConfig && postcssConfig.options) || {}
const postcssPlugins =
postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : []
if (needInlineImport) {
postcssPlugins.unshift((await import('postcss-import')).default())
}
if (isModule) {
postcssPlugins.unshift(
(await import('postcss-modules')).default({
localsConvention: 'camelCaseOnly',
...modulesOptions,
getJSON(_: string, _modules: Record<string, string>) {
modules = _modules
}
})
)
}
if (!postcssPlugins.length) {
return {
code,
map
}
}
// postcss is an unbundled dep and should be lazy imported
const postcssResult = await (await import('postcss'))
.default(postcssPlugins)
.process(code, {
...postcssOptions,
to: id,
from: id,
map: {
inline: false,
annotation: false,
prev: map
}
})
// record CSS dependencies from @imports
for (const message of postcssResult.messages) {
if (message.type === 'dependency') {
deps.add(message.file as string)
} else if (message.type === 'warning') {
let msg = `[vite:css] ${message.text}`
if (message.line && message.column) {
msg += `\n${generateCodeFrame(code, {
line: message.line,
column: message.column
})}`
}
config.logger.warn(chalk.yellow(msg))
}
}
return {
ast: postcssResult,
code: postcssResult.css,
map: postcssResult.map as any,
modules,
deps
}
}
interface PostCSSConfigResult {
options: ProcessOptions
plugins: PostcssPlugin[]
}
let cachedPostcssConfig: PostCSSConfigResult | null | undefined
async function loadPostcssConfig(
root: string
): Promise<PostCSSConfigResult | null> {
if (cachedPostcssConfig !== undefined) {
return cachedPostcssConfig
}
try {
return (cachedPostcssConfig = await postcssrc({}, root))
} catch (e) {
if (!/No PostCSS Config found/.test(e.message)) {
throw e
}
return (cachedPostcssConfig = null)
}
}
// Preprocessor support. This logic is largely replicated from @vue/compiler-sfc
type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'
type StylePreprocessor = (
source: string,
map: SourceMap | undefined,
options: {
[key: string]: any
additionalData?: string | ((source: string, filename: string) => string)
filename: string
}
) => StylePreprocessorResults | Promise<StylePreprocessorResults>
export interface StylePreprocessorResults {
code: string
map?: object
errors: RollupError[]
deps: string[]
}
function loadPreprocessor(lang: PreprocessLang) {
try {
return require(lang)
} catch (e) {
throw new Error(
`Preprocessor dependency "${lang}" not found. Did you install it?`
)
}
}
// .scss/.sass processor
const scss: StylePreprocessor = async (source, map, options) => {
const nodeSass = loadPreprocessor('sass')
const finalOptions = {
...options,
data: getSource(source, options.filename, options.additionalData),
file: options.filename,
outFile: options.filename,
sourceMap: !!map
}
try {
const result = await new Promise<any>((resolve, reject) => {
nodeSass.render(finalOptions, (err: Error | null, res: any) => {
if (err) {
reject(err)
} else {
resolve(res)
}
})
})
const deps = result.stats.includedFiles
if (map) {
return {
code: result.css.toString(),
map: merge(map, JSON.parse(result.map.toString())),
errors: [],
deps
}
}
return { code: result.css.toString(), errors: [], deps }
} catch (e) {
return { code: '', errors: [e], deps: [] }
}
}
const sass: StylePreprocessor = (source, map, options) =>
scss(source, map, {
...options,
indentedSyntax: true
})
// .less
interface LessError {
message: string
line: number
column: number
}
const less: StylePreprocessor = (source, map, options) => {
const nodeLess = loadPreprocessor('less')
let result: any
let error: LessError | null = null
nodeLess.render(
getSource(source, options.filename, options.additionalData),
{ ...options, syncImport: true },
(err: LessError | null, output: any) => {
error = err
result = output
}
)
if (error) {
// normalize error info
const normalizedError: RollupError = new Error(error!.message)
normalizedError.loc = {
file: options.filename,
line: error!.line,
column: error!.column
}
return { code: '', errors: [normalizedError], deps: [] }
}
const deps = result.imports
if (map) {
return {
code: result.css.toString(),
map: merge(map, result.map),
errors: [],
deps
}
}
return {
code: result.css.toString(),
errors: [],
deps
}
}
// .styl
const styl: StylePreprocessor = (source, map, options) => {
const nodeStylus = loadPreprocessor('stylus')
try {
const ref = nodeStylus(source)
Object.keys(options).forEach((key) => ref.set(key, options[key]))
if (map) ref.set('sourcemap', { inline: false, comment: false })
const result = ref.render()
const deps = ref.deps()
if (map) {
return {
code: result,
map: merge(map, ref.sourcemap),
errors: [],
deps
}
}
return { code: result, errors: [], deps }
} catch (e) {
return { code: '', errors: [e], deps: [] }
}
}
function getSource(
source: string,
filename: string,
additionalData?: string | ((source: string, filename: string) => string)
) {
if (!additionalData) return source
if (typeof additionalData === 'function') {
return additionalData(source, filename)
}
return additionalData + source
}
const preProcessors = {
less,
sass,
scss,
styl,
stylus: styl
}
type Replacer = (url: string) => string | Promise<string>
const cssUrlRE = /url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/
function rewriteCssUrls(
css: string,
replacerOrBase: string | Replacer
): Promise<string> {
let replacer: Replacer
if (typeof replacerOrBase === 'string') {
replacer = (rawUrl) => {
return path.posix.resolve(path.posix.dirname(replacerOrBase), rawUrl)
}
} else {
replacer = replacerOrBase
}
return asyncReplace(css, cssUrlRE, async (match) => {
let [matched, rawUrl] = match
let wrap = ''
const first = rawUrl[0]
if (first === `"` || first === `'`) {
wrap = first
rawUrl = rawUrl.slice(1, -1)
}
if (isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl.startsWith('#')) {
return matched
}
return `url(${wrap}${await replacer(rawUrl)}${wrap})`
})
}
let CleanCSS: any
async function minifyCSS(css: string, config: ResolvedConfig) {
CleanCSS = CleanCSS || (await import('clean-css')).default
const res = new CleanCSS({
rebase: false,
...config.build.cleanCssOptions
}).minify(css)
if (res.errors && res.errors.length) {
config.logger.error(chalk.red(`error when minifying css:\n${res.errors}`))
// TODO format this
throw res.errors[0]
}
if (res.warnings && res.warnings.length) {
config.logger.warn(
chalk.yellow(`warnings when minifying css:\n${res.warnings}`)
)
}
return res.styles
}