-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathvite.config.js
101 lines (95 loc) · 3.42 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
import { readdirSync, readFileSync } from "fs";
import { writeFile } from "fs/promises";
import { createRequire } from "module";
import { dirname, join, resolve } from "path";
import { fileURLToPath } from "url";
import { defineConfig } from "vite";
import solidPlugin from "vite-plugin-solid";
import suidPlugin from "./../vite-plugin/src";
import readTypings from "./vite/readTypings";
const require = createRequire(import.meta.url);
const __dirname = dirname(fileURLToPath(import.meta.url));
const solidJsPath = dirname(dirname(require.resolve("solid-js")));
const SOLID_TYPINGS = readTypings(solidJsPath);
const packageDir = `${__dirname}/../`;
const parseJsonFile = (path) => JSON.parse(readFileSync(path).toString());
const SUID_PKG_NAMES = readdirSync(packageDir);
const SUID_VERSIONS = SUID_PKG_NAMES.reduce((result, name) => {
const pkg = parseJsonFile(`${packageDir}/${name}/package.json`);
result[pkg.name] = pkg.version;
if (name === "site") {
result["solid-js"] = pkg.dependencies["solid-js"];
result["vite"] = pkg.devDependencies["vite"];
result["vite-plugin-solid"] = pkg.devDependencies["vite-plugin-solid"];
} else if (name === "codemod") {
result["ts-morph"] = pkg.dependencies["ts-morph"];
}
return result;
}, {});
const basePkg = parseJsonFile(`${packageDir}/../package.json`);
SUID_VERSIONS.typescript = basePkg.devDependencies.typescript;
export default defineConfig({
define: {
SUID_PKG_NAMES: SUID_PKG_NAMES.map((name) => `@suid/${name}`),
SUID_VERSIONS,
SOLID_TYPINGS,
},
plugins: [
{
name: "examples",
enforce: "pre",
transform(code, id) {
if (id.includes("Example") && id.endsWith(".tsx")) {
const exportDefaultRegex = /export\s+default\s+function\s+(\w+)/;
const matches = exportDefaultRegex.exec(code);
if (exportDefaultRegex) {
const [, functionName] = matches;
const originalCode = code
.split(/\r?\n/)
.filter((line) => !line.trim().startsWith("// eslint-disable"))
.join("\n");
code = code.replace(/export\s+default\s+function/, "function");
code += `\n${functionName}.code = ${JSON.stringify(
originalCode
)};\n`;
code += `\nexport default ${functionName}`;
return code;
}
}
},
},
suidPlugin(),
solidPlugin({
ssr: process.env.SSR === "1" || process.env.SSR === "true",
}),
{
name: "prepare-deploy",
async closeBundle() {
const distPath = join(__dirname, "dist");
const redirectsPath = join(distPath, "_redirects");
await writeFile(redirectsPath, "/* /index.html 200");
},
},
],
build: {
target: "esnext",
external: ["perf_hooks"],
},
esbuild: {
keepNames: true,
},
resolve: {
alias: {
"~": resolve(__dirname, "src"),
"@suid/types": resolve(__dirname, "../types/src"),
"@suid/codemod": resolve(__dirname, "../codemod/src"),
"@suid/css": resolve(__dirname, "../css/src"),
"@suid/utils": resolve(__dirname, "../utils/src"),
"@suid/styled-engine": resolve(__dirname, "../styled-engine/src"),
"@suid/base": resolve(__dirname, "../base/src"),
"@suid/system": resolve(__dirname, "../system/src"),
"@suid/material": resolve(__dirname, "../material/src"),
"@suid/icons-material": resolve(__dirname, "../icons-material/lib"),
},
},
});