-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Expand file tree
/
Copy pathvite.config.js
More file actions
151 lines (145 loc) · 4.38 KB
/
vite.config.js
File metadata and controls
151 lines (145 loc) · 4.38 KB
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
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import stylus from 'stylus'
import { defineConfig } from 'vite'
// trigger scss bug: https://github.com/sass/dart-sass/issues/710
// make sure Vite handles safely
// @ts-expect-error refer to https://github.com/vitejs/vite/pull/11079
globalThis.window = {}
// @ts-expect-error refer to https://github.com/vitejs/vite/pull/11079
globalThis.location = new URL('http://localhost/')
export default defineConfig({
plugins: [
{
// Emulate a UI framework component where a framework module would import
// scoped CSS files that should treeshake if the default export is not used.
name: 'treeshake-scoped-css',
enforce: 'pre',
async resolveId(id, importer) {
if (!importer || !id.endsWith('-scoped.css')) return
const resolved = await this.resolve(id, importer)
if (!resolved) return
return {
...resolved,
meta: {
vite: {
cssScopeTo: [
importer,
resolved.id.includes('barrel') ? undefined : 'default',
],
},
},
}
},
},
],
build: {
cssTarget: 'chrome61',
manifest: true,
rollupOptions: {
input: {
index: path.resolve(__dirname, './index.html'),
treeshakeScoped: path.resolve(
__dirname,
'./treeshake-scoped/index.html',
),
empty: path.resolve(__dirname, './empty.css'),
empty2: path.resolve(__dirname, './empty2.css'),
},
output: {
manualChunks(id) {
if (id.includes('manual-chunk.css')) {
return 'dir/dir2/manual-chunk'
}
},
},
},
},
esbuild: {
logOverride: {
'unsupported-css-property': 'silent',
},
},
resolve: {
alias: [
{ find: '=', replacement: __dirname },
{ find: /^=replace\/(.*)/, replacement: `${__dirname}/$1` },
{ find: 'spacefolder', replacement: __dirname + '/folder with space' },
{ find: '#alias', replacement: __dirname + '/aliased/foo.css' },
{
find: '#alias?inline',
replacement: __dirname + '/aliased/foo.css?inline',
},
{
find: '#alias-module',
replacement: __dirname + '/aliased/bar.module.css',
},
],
},
css: {
modules: {
generateScopedName: '[name]__[local]___[hash:base64:5]',
// example of how getJSON can be used to generate
// typescript typings for css modules class names
// getJSON(cssFileName, json, _outputFileName) {
// let typings = 'declare const classNames: {\n'
// for (let className in json) {
// typings += ` "${className}": string;\n`
// }
// typings += '};\n'
// typings += 'export default classNames;\n'
// const { join, dirname, basename } = require('path')
// const typingsFile = join(
// dirname(cssFileName),
// basename(cssFileName) + '.d.ts'
// )
// require('fs').writeFileSync(typingsFile, typings)
// },
},
preprocessorOptions: {
scss: {
additionalData: `$injectedColor: orange;`,
importers: [
{
canonicalize(url) {
return url === 'virtual-dep' || url.endsWith('.wxss')
? new URL('custom-importer:virtual-dep')
: null
},
load() {
return {
contents: ``,
syntax: 'scss',
}
},
},
{
canonicalize(url) {
return url === 'virtual-file-absolute'
? new URL('custom-importer:virtual-file-absolute')
: null
},
load() {
return {
contents: `@use "${pathToFileURL(path.join(import.meta.dirname, 'file-absolute.scss')).href}"`,
syntax: 'scss',
}
},
},
],
},
styl: {
additionalData: `$injectedColor ?= orange`,
imports: [
'./options/relative-import.styl',
path.join(__dirname, 'options/absolute-import.styl'),
],
define: {
$definedColor: new stylus.nodes.RGBA(51, 197, 255, 1),
definedFunction: () => new stylus.nodes.RGBA(255, 0, 98, 1),
},
},
},
preprocessorMaxWorkers: true,
},
})