-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
executable file
·228 lines (209 loc) · 7.03 KB
/
Copy pathvite.config.js
File metadata and controls
executable file
·228 lines (209 loc) · 7.03 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Library build config for `@fest-lib/subsystem` sources under modules/shared/src.
* Named export `initiate` is reused by modules/projects/subsystem and fl.ui vite configs.
* Dev playground with HTTPS: npm run dev → vite.dev.config.js
*/
import { resolve } from "node:path";
import { readFile } from "node:fs/promises";
import { defineConfig, searchForWorkspaceRoot } from "vite";
import pluginExternal from "vite-plugin-external";
import deduplicate from "postcss-discard-duplicates";
import autoprefixer from "autoprefixer";
import cssnano from "cssnano";
import { npmFestImportRewritePlugin } from "./vite-npm-imports.mjs";
const NAME = "subsystem";
//
export const importConfig = (url, ...args)=>{ return import(url)?.then?.((m)=>m?.default?.(...args)); }
export const objectAssign = (target, ...sources) => {
if (!sources.length) return target;
const source = sources.shift();
if (source && typeof source === 'object') {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
if (source[key] && typeof source[key] === 'object') {
if (!target[key] || typeof target[key] !== 'object') {
target[key] = Array.isArray(source[key]) ? [] : {};
}
objectAssign(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
}
return objectAssign(target, ...sources);
}
export function normalizeAliasPattern(pattern) {
return pattern.replace(/\/\*+$/, "");
}
export function importFromTSConfig(tsconfig, dir) {
const paths = tsconfig?.compilerOptions?.paths || {};
/** Longer `find` first so e.g. `com/config/Names` wins over `com` from `com/*`. */
const keys = Object.keys(paths).sort((a, b) => {
const na = normalizeAliasPattern(a);
const nb = normalizeAliasPattern(b);
if (nb.length !== na.length) return nb.length - na.length;
return a.localeCompare(b);
});
const out = [];
for (const key of keys) {
const normalizedKey = normalizeAliasPattern(key);
const target = paths[key][0];
const normalizedTarget = normalizeAliasPattern(target);
out.push({
find: normalizedKey,
replacement: resolve(dir, normalizedTarget)
});
}
return out;
}
export const projectMap = new Map([
["fest/core", "core.ts"],
["fest/icon", "icon.ts"],
["fest/fl-ui", "fl.ui"],
["fest/object", "object.ts"],
["fest/uniform", "uniform.ts"],
["fest/dom", "dom.ts"],
["fest/veela", "veela.css"],
["veela-lib", "veela.css"],
["fest/lure", "lur.e"],
["fest/image", "image.ts"]
]);
export function initiate(name = NAME, tsconfig = {}, dir = resolve(import.meta.dirname, "./")) {
const $resolve = { alias: importFromTSConfig(tsconfig, dir) };
const plugins = [
pluginExternal({
include: Array.from(projectMap.keys()).filter((n) => !n.endsWith(name)),
exclude: [
resolve(dir, "./src/index.ts"),
"./src/index.ts",
resolve(dir, `./dist/${name}.js`),
`./dist/${name}.js`
]
}),
...(process.env.FEST_NPM_IMPORTS === "1" ? [npmFestImportRewritePlugin()] : [])
];
const rollupOptions = {
shimMissingExports: true,
treeshake: {
annotations: false,
moduleSideEffects: true,
tryCatchDeoptimization: false,
unknownGlobalSideEffects: true,
correctVarValueBeforeDeclaration: true,
propertyReadSideEffects: true
},
input: "./src/index.ts",
external: (source) => {
if (source?.includes?.("node_modules/")) return false;
if (
source?.includes?.(`fest/${name}`) ||
source?.includes?.("./src/index.ts") ||
source?.includes?.(projectMap.get(`fest/${name}`)) ||
source?.includes?.("dist/")
)
return false;
if (Array.from(projectMap.keys()).some((n) => source.includes(n))) return true;
return false;
},
output: {
compact: true,
name,
dir: "./dist",
exports: "auto",
minifyInternalExports: true
}
};
const css = {
postcss: {
plugins: [
deduplicate(),
autoprefixer(),
cssnano({
preset: [
"advanced",
{
calc: false,
layer: false,
scope: false,
discardComments: {
removeAll: true
}
}
]
})
]
}
};
const optimizeDeps = {
include: [
"./node_modules/**/*.mjs",
"./node_modules/**/*.js",
"./node_modules/**/*.ts",
"./src/**/*.mjs",
"./src/**/*.js",
"./src/**/*.ts",
"./src/*.mjs",
"./src/*.js",
"./src/*.ts",
"./test/*.mjs",
"./test/*.js",
"./test/*.ts"
],
entries: [resolve(dir, "./src/index.ts")],
force: true
};
const server = {
port: 8434,
open: false,
host: "0.0.0.0",
strictPort: false,
origin: "https://localhost:8434",
allowedHosts: ["localhost", "127.0.0.1", "0.0.0.0", "192.168.0.200", "95.188.82.223"],
appType: "spa",
fs: {
strict: false,
allow: [
searchForWorkspaceRoot(process.cwd()),
"../**/*",
"../*",
"..",
resolve(dir, "./**/*"),
resolve(dir, "./*"),
dir
]
}
};
const build = {
chunkSizeWarningLimit: 1600,
assetsInlineLimit: 1024 * 1024,
minify: "esbuild",
emptyOutDir: true,
target: "esnext",
modulePreload: {
polyfill: true,
include: ["fest/core", "fest/dom", "fest/lure", "fest/object", "fest/uniform"]
},
rollupOptions,
name,
lib: {
formats: ["es"],
entry: resolve(dir, "./src/index.ts"),
name,
fileName: name
}
};
const esbuild = {
legalComments: "none",
minify: true,
minifySyntax: true,
minifyIdentifiers: true,
minifyWhitespace: true
};
return { esbuild, rollupOptions, plugins, resolve: $resolve, build, css, optimizeDeps, server };
}
const pkgDir = resolve(import.meta.dirname, "./");
export default defineConfig(async () => {
const tsconfig = JSON.parse(await readFile(resolve(pkgDir, "./tsconfig.json"), { encoding: "utf8" }));
return initiate(NAME, tsconfig, pkgDir);
});