-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathindex.js
72 lines (71 loc) · 1.83 KB
/
index.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
import path from 'node:path';
import fs from 'node:fs';
/**
* Ensure transform flow is not interrupted
* @returns {import('vite').Plugin[]}
*/
export function transformValidation() {
return [
{
name: 'transform-validation:1',
enforce: 'pre',
transform(code, id) {
if (id.endsWith('.svelte')) {
return code.replaceAll('__JS_TRANSFORM_1__', '__JS_TRANSFORM_2__');
} else if (id.endsWith('.css')) {
return code.replaceAll('__CSS_TRANSFORM_1__', '__CSS_TRANSFORM_2__');
}
}
},
{
name: 'transform-validation:2',
transform(code, id) {
if (id.endsWith('.svelte')) {
return code.replaceAll('__JS_TRANSFORM_2__', '__JS_TRANSFORM_3__');
} else if (id.endsWith('.css')) {
return code.replaceAll('__CSS_TRANSFORM_2__', 'red');
}
}
},
{
name: 'transform-validation:3',
enforce: 'post',
transform(code, id) {
if (id.endsWith('.svelte')) {
return code.replaceAll('__JS_TRANSFORM_3__', 'Hello world');
}
// can't handle css here as in build, it would be `export default {}`
}
}
];
}
/**
* write resolved config
* @returns {import('vite').Plugin}
*/
export function writeResolvedConfig() {
let cmd;
return {
name: 'writeResolvedConfig',
enforce: 'post',
config(_, { command }) {
cmd = command;
},
configResolved(config) {
function replacer(key, value) {
if (value instanceof RegExp) return value.toString();
else return value;
}
const serializableConfig = {
...config,
plugins: config.plugins.map((p) => p.name)
};
const dir = path.join(config.root, 'logs', 'resolved-configs');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const filename = path.join(dir, `vite.config.${cmd}${config.build.ssr ? '.ssr' : ''}.json`);
fs.writeFileSync(filename, JSON.stringify(serializableConfig, replacer, '\t'), 'utf-8');
}
};
}