forked from TeselaGen/tg-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.react.config.ts
190 lines (185 loc) · 5.51 KB
/
vite.react.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
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
187
188
189
190
/// <reference types="vitest" />
import { defineConfig } from "vite";
import fs from "node:fs";
import react from "@vitejs/plugin-react";
import viteTsConfigPaths from "vite-tsconfig-paths";
import * as esbuild from "esbuild";
import libCss from "vite-plugin-libcss";
import { camelCase } from "lodash-es";
import { getPort } from "./getPort";
import { viteStaticCopy } from "vite-plugin-static-copy";
import path from "node:path";
import dts from "vite-plugin-dts";
import { joinPathFragments } from "nx/src/devkit-exports";
// import million from "million/compiler";
//vite config for react packages
const justSrc = [
/\/src\/.*\.js$/,
/\/src\/.*\.jsx$/,
/\/src\/.*\.ts$/,
/\/src\/.*\.tsx$/
];
const sourceJSPattern = [
...justSrc,
/\/demo\/.*\.js$/,
/\/helperUtils\/.*\.js$/
];
const rollupPlugin = (matchers: RegExp[]) => ({
name: "js-in-jsx",
load(id: string) {
if (matchers.some(matcher => matcher.test(id))) {
const file = fs.readFileSync(id, { encoding: "utf-8" });
return esbuild.transformSync(file, { loader: "jsx" }).code;
}
return undefined;
}
});
export default ({ name, dir }: { name: string; dir: string }) =>
defineConfig(({ command, mode = "production" }) => {
const isDemo = mode === "demo";
const isUmd = mode === "umd";
if (!isDemo) {
mode = "production";
}
const port = getPort(name);
return {
...(command === "build"
? {
define: {
"process.env.NODE_ENV": JSON.stringify("production")
}
}
: {}),
cacheDir: `../../node_modules/.vite/${name}`,
server: {
port
},
base: "./",
plugins: [
//tnw - maybe add this back later after adding performance metrics https://twitter.com/aidenybai/status/1689773623827943424
// million.vite({
// auto: true
// }),
dts({
entryRoot: "src",
tsconfigPath: joinPathFragments(dir, "tsconfig.json")
// skipDiagnostics: true,
}),
react(),
libCss(),
viteTsConfigPaths({
root: "../../"
}),
...(command === "build" && !isDemo
? [
// noBundlePlugin({ copy: "**/*.css" }),
viteStaticCopy({
targets: [
{
src: "./src",
dest: "."
},
{
src: "./README.md",
dest: "."
}
]
})
]
: [])
],
esbuild: {
loader: "jsx",
include: sourceJSPattern,
exclude: [],
keepNames: true,
minifyIdentifiers: false,
minifySyntax: false
},
optimizeDeps: {
esbuildOptions: {
loader: {
".js": "jsx",
".ts": "tsx"
}
}
},
// Uncomment this if you are using workers.
// worker: {
// plugins: [
// viteTsConfigPaths({
// root: '../../',
// }),
// ],
// },
// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
minify: false,
target: "es2015",
outDir: `../../${isDemo ? "demo-dist" : "dist"}/${name}`,
...(isDemo
? {}
: {
lib: {
// Could also be a dictionary or array of multiple entry points.
entry: "src/index.js",
name,
fileName: format => `index.${format}.js`,
formats: isUmd ? ["umd"] : ["es", "cjs"]
// Change this to the formats you want to support.
// Don't forgot to update your package.json as well.
}
}),
rollupOptions: {
plugins: [rollupPlugin(justSrc)],
output: {
name: camelCase(name)
},
// External packages that should not be bundled into your library.
external:
mode === "demo" || isUmd
? []
: [
"react",
"react-dom",
"react/jsx-runtime",
"redux",
"react-redux",
"redux-form",
"@blueprintjs/core",
"@blueprintjs/select",
"@blueprintjs/datetime"
]
}
},
resolve: {
alias: {
react: path.join(__dirname, "node_modules/react"),
"@blueprintjs/core": path.join(
__dirname,
"node_modules/@blueprintjs/core"
),
"@blueprintjs/datetime": path.join(
__dirname,
"node_modules/@blueprintjs/datetime"
),
// "@teselagen/react-table/react-table.css": `/Users/tnrich/Sites/react-table/react-table.css`,
// "@teselagen/react-table": `/Users/tnrich/Sites/react-table/src`,
"react-dom": path.join(__dirname, "node_modules/react-dom"),
"react-redux": path.join(__dirname, "node_modules/react-redux"),
"redux-form": path.join(__dirname, "node_modules/redux-form"),
redux: path.join(__dirname, "node_modules/redux")
}
},
test: {
globals: true,
cache: {
dir: "../../node_modules/.vitest"
},
setupFiles: ["../../vitest.setup.ts"],
environment: "jsdom",
include: ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"]
}
};
});