-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
186 lines (172 loc) · 4.14 KB
/
vite.config.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
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
import { resolve, join } from 'path'
import { defineConfig } from 'vite'
import unsafeVars from './transform/unsafeVariables';
import assertRemove from './transform/assertRemove';
import staticMount from './transform/staticMount';
import compileHTMLLiteral from './transform/htmlLiteral';
import fs from 'fs';
const createTransform = (name, transform, jsx, options) => ({
name,
transform(code, id) {
if (id.endsWith('.js') || (jsx && id.endsWith('.jsx'))) {
const transformed = transform(code, {
sourceFileName: id,
plugins: id.endsWith('.jsx') ? ['jsx'] : [],
...options,
});
return {
code: transformed.code,
map: transformed.decodedMap,
};
}
}
});
const libs = {
destamd: {
entry: resolve(__dirname, 'index.js'),
fileName: 'destam-dom',
},
};
const lib = process.env.LIB;
let config;
if (lib in libs) {
config = defineConfig({
define: {
'process.env.NODE_ENV': '"development"',
},
plugins: !process.env.N_DEBUG ? [] : [
createTransform('assert-remove', assertRemove),
{
name: 'unsafe-vars',
renderChunk: (code, chunk, options) => {
const transformed = unsafeVars(code, {
sourceFileName: chunk.fileName,
});
return {
code: transformed.code,
map: transformed.decodedMap,
};
},
},
],
build: {
minify: 'terser',
target: 'es2020',
terserOptions: {
compress: {
unsafe_math: true,
unsafe_comps: true,
unsafe_symbols: true,
ecma: 2020,
passes: 2,
},
mangle: {
properties: {
regex: /_$/,
}
}
},
lib: {
...libs[lib],
name: lib,
formats: process.env.FORMATS?.split(',') || ['es', 'iife', 'umd'],
},
sourcemap: true,
},
});
} else {
const getExample = (file) => {
if (!file.startsWith('/')) return null;
file = file.substring(1);
let i = file.lastIndexOf('.');
const name = file.substring(0, i);
const existed = ['.html', '.js', '.jsx'].find(ex => fs.existsSync('examples/' + name + ex));
if (!existed) {
return null;
}
const relative = '/' + name + '.html';
return {
name,
file: name + existed,
relative,
location: resolve(__dirname, '/examples/' + name + existed),
resolved: join(__dirname, relative),
};
};
let examples;
const getExamples = () => {
if (examples) {
return examples;
}
return examples = fs.readdirSync(resolve(__dirname, 'examples')).map(file => {
return getExample('/' + file);
});
};
const generateTemplate = (entry, hot) => {
return `
<!doctype html>
<html lang="en">
${hot ? '<script type="module" src="/@vite/client"></script>' : ""}
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Destam-Dom</title>
</head>
<body>
<script type="module" src="./examples/${entry.file}"></script>
</body>
</html>
`;
};
config = defineConfig({
optimizeDeps: {
entries: [],
},
plugins: [
...(process.env.N_DEBUG ? [createTransform('assert-remove', assertRemove)] : []),
createTransform('transform-literal-html', compileHTMLLiteral, true, {
jsx_auto_import: {h: 'destam-dom'},
}),
{
name: 'examples',
resolveId (id) {
let found = getExamples().find(ex => ex.resolved === id);
if (found) {
return found.resolved;
}
},
load(id) {
let found = getExamples().find(ex => ex.resolved === id);
if (found) return generateTemplate(found);
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
let found = getExample(req.originalUrl);
if (found && found.relative === req.originalUrl && !found.file.endsWith('.html')) {
res.end(generateTemplate(found, true));
} else {
next();
}
});
},
},
...(process.env.STATIC_ANALYZE ? [createTransform('static-mount', staticMount, true, {
util_import: 'destam-dom'
})] : []),
],
esbuild: {
jsx: 'preserve',
},
build: {
rollupOptions: {
input: Object.fromEntries(getExamples().map(ex => [ex.name, ex.resolved])),
},
},
resolve: {
alias: [
{find: /^destam-dom($|\/)/, replacement: '/'},
]
}
});
}
export default config;