-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
110 lines (99 loc) · 2.93 KB
/
Copy pathvite.config.ts
File metadata and controls
110 lines (99 loc) · 2.93 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
import path from "node:path"
import babelPlugin, { defineRolldownBabelPreset } from "@rolldown/plugin-babel"
import react from "@vitejs/plugin-react"
import { defineConfig, type Plugin } from "vite"
const linguiPreset = defineRolldownBabelPreset({
preset: () => ({ plugins: ["@lingui/babel-plugin-lingui-macro"] }),
rolldown: {
filter: {
code: /from ['"]@lingui\/(?:react|core)\/macro['"]/,
},
},
})
const isModuleName = (id: string) =>
!id.startsWith(".") && !id.startsWith("\0") && !path.isAbsolute(id)
interface LibBundleOptions {
entries: Record<string, { path: string; outFile: string }>
disabled?: boolean
includeInBundle?: (string | RegExp)[]
}
const libBundle = ({
disabled,
entries,
includeInBundle = [],
}: LibBundleOptions): Plugin => {
if (disabled) return { name: "lib-bundle" }
const shouldBundle = (id: string) => {
const forceBundling = includeInBundle.some(pattern =>
typeof pattern === "string" ? id.startsWith(pattern) : pattern.test(id)
)
return forceBundling || !isModuleName(id)
}
return {
name: "lib-bundle",
enforce: "pre",
config: config => {
config.build ??= {}
config.build.sourcemap ??= false
config.build.minify ??= false
config.build.copyPublicDir ??= false
config.build.rolldownOptions = {
...config.build.rolldownOptions,
external: id => !shouldBundle(id),
}
config.build.lib = {
...config.build.lib,
formats: ["es"],
cssFileName: "index",
entry: Object.fromEntries(
Object.entries(entries).map(([key, { path }]) => [key, path])
),
fileName: (format, entry) => {
const filePath = entries[entry]?.outFile
if (!filePath) throw new Error(`Unknown entry point: ${entry}`)
if (["es", "esm", "module"].includes(format)) {
return `${filePath}.mjs`
}
throw new Error(`Unsupported format "${format}"`)
},
}
},
}
}
export default defineConfig(({ command }) => ({
plugins: [
react(),
babelPlugin({ presets: [linguiPreset] }),
libBundle({
disabled: command !== "build",
entries: {
"src/index": {
outFile: "index",
path: path.resolve(__dirname, "./src/index.ts"),
},
"src/index-utils": {
outFile: "index-utils",
path: path.resolve(__dirname, "./src/index-utils.ts"),
},
"src/index-icons": {
outFile: "index-icons",
path: path.resolve(__dirname, "./src/index-icons.ts"),
},
"src/index-theme": {
outFile: "index-theme",
path: path.resolve(__dirname, "./src/index-theme.ts"),
},
},
}),
],
resolve: { tsconfigPaths: true },
test: {
dir: "src",
include: ["**/*.test.*"],
setupFiles: ["tests/test-setup.ts"],
environment: "happy-dom",
isolate: false,
pool: "threads",
watch: false,
},
}))