-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
vite.config.ts
126 lines (123 loc) · 4.79 KB
/
vite.config.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
import reactPlugin from "@vitejs/plugin-react";
import {defineConfig} from "vite";
import logseqDevPlugin from "vite-plugin-logseq";
import {nodePolyfills} from "vite-plugin-node-polyfills";
import * as path from "path";
import * as fs from "fs";
const {parseSync, traverse} = require("@babel/core");
const generate = require("@babel/generator").default;
import {build} from "esbuild";
import {fileURLToPath} from "node:url";
// https://vitejs.dev/config/
function staticFileSyncTransformPlugin() {
return {
name: "staticFileSyncTransformPlugin",
transform(code, id) {
if (/\.(js|ts|tsx|jsx|mjs)(\?.*)?$/.test(id) && code.includes("readFileSync")) {
let curDir = path.dirname(id);
if (curDir.includes("node_modules/.vite")) {
// We are in a vite cache folder. We need original path.
curDir = path.join(
__dirname,
"/node_modules/",
path.parse(path.basename(id)).name,
);
}
const ast = parseSync(code, {sourceType: "module"});
traverse(ast, {
Identifier(nodePath) {
if (nodePath.node.name === "__dirname") {
nodePath.replaceWithSourceString(JSON.stringify(curDir));
}
},
});
traverse(ast, {
CallExpression(nodePath) {
const {callee, arguments: args} = nodePath.node;
if (
callee.type === "MemberExpression" &&
callee.object.name === "path" &&
callee.property.name === "join"
) {
nodePath.replaceWithSourceString(
JSON.stringify(path.join(...args.map((arg) => arg.value))),
);
}
},
});
traverse(ast, {
CallExpression(nodePath) {
const {callee, arguments: args} = nodePath.node;
if (
callee.type === "MemberExpression" &&
callee.object.name === "fs" &&
callee.property.name === "readFileSync"
) {
const filePath = args[0].value;
try {
const fileContents = fs.readFileSync(filePath, "utf-8");
nodePath.replaceWithSourceString(JSON.stringify(fileContents));
} catch (e) {
console.error(e);
}
}
},
});
const generated = generate(ast, {retainLines: true});
code = generated.code;
const map = generated.map;
return {code, map};
}
},
};
}
function bundleJSStringPlugin() {
return {
name: "bundleJSStringPlugin",
async transform(code, id) {
if (id.endsWith(".js?string")) {
const result = await build({
stdin: {
contents: code,
resolveDir: path.dirname(id),
sourcefile: id,
},
sourceRoot: __dirname,
bundle: true,
minify: true,
//format: 'cjs',
platform: "browser",
write: false,
});
return {
code: `export default ${JSON.stringify(result.outputFiles[0].text)};`,
map: null,
};
}
},
};
}
export default defineConfig({
base: "./",
plugins: [
logseqDevPlugin(),
reactPlugin(),
nodePolyfills(),
staticFileSyncTransformPlugin(),
bundleJSStringPlugin(),
],
resolve:
{
alias: [
{ find: 'react/jsx-dev-runtime', replacement: fileURLToPath(new URL('./node_modules/react/jsx-dev-runtime.js', import.meta.url)) },
{ find: 'react/jsx-runtime', replacement: fileURLToPath(new URL('./node_modules/react/jsx-runtime.js', import.meta.url)) },
{ find: 'react', replacement: fileURLToPath(new URL('./src/ui/React.ts', import.meta.url)) },
{ find: 'react-dom', replacement: fileURLToPath(new URL('./src/ui/ReactDOM.ts', import.meta.url)) }
]
},
build: {
sourcemap: true,
target: "modules",
minify: "esbuild",
},
});