forked from wpkernel/wpkernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.base.ts
More file actions
165 lines (151 loc) · 4.3 KB
/
vite.config.base.ts
File metadata and controls
165 lines (151 loc) · 4.3 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
import { defineConfig, type UserConfig } from 'vite';
import { resolve } from 'path';
import dts from 'vite-plugin-dts';
// eslint-disable-next-line camelcase
import { wp_globals } from '@kucrut/vite-for-wp/utils';
import {
FRAMEWORK_PEERS,
type FrameworkPeerSpec,
} from '@wpkernel/scripts/config/framework-peers';
// Accept array OR predicate for externals
type ExternalOpt = Array<string | RegExp> | ((id: string) => boolean);
type KernelLibConfigOptions = {
/** Additional Rollup externals specific to the package. */
external?: ExternalOpt;
/** Enable production console/debugger drop (default: true) */
dropConsoleInProd?: boolean;
};
/**
* Create a Vite configuration for WPKernel library packages.
*
* @param packageName - The package name (e.g., '@wpkernel/core')
* @param entries - Entry points (e.g., { index: 'src/index.ts', http: 'src/http/index.ts' })
* @param options
*/
export const createWPKLibConfig = (
packageName: string,
entries: Record<string, string>,
options: KernelLibConfigOptions = {}
): UserConfig => {
const mode = process.env.NODE_ENV ?? 'development';
const isProd = mode === 'production';
// WordPress “module ids” known to WP (via vite-for-wp utils)
const wpIds = Object.keys(wp_globals());
// Normalise externals to Rollup’s accepted shapes (array OR function)
const userExternal = options.external;
const escapeRegex = (value: string): string =>
value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const peerExternal: Array<string | RegExp> = Object.entries(FRAMEWORK_PEERS)
.filter(([, spec]) => {
const peerSpec = spec as FrameworkPeerSpec & { bundle?: boolean };
return peerSpec.bundle !== true;
})
.map(([dependency, spec]) =>
spec.kind === 'internal'
? new RegExp(`^${escapeRegex(dependency)}(\/.*)?$`)
: dependency
);
// Default external capability:
// - All WP ids (incl. any @wordpress/*)
// - React bits (just in case something imports jsx-runtime)
// - Other wpk packages (avoid circular bundling across workspace)
// - Node built-ins (string & 'node:' resolver)
// - A few heavy tools used by CLI/plugins (left external on purpose)
const defaultExternalArray: Array<string | RegExp> = [
...wpIds,
...peerExternal,
'react/jsx-runtime',
/^node:/,
'fs',
'fs/promises',
'path',
'url',
'crypto',
'util',
'module',
'os',
'tty',
'worker_threads',
'child_process',
'assert',
'process',
'v8',
'net',
'prettier',
'prettier/standalone',
'@prettier/plugin-php',
'tsx',
'tsx/esm/api',
];
const rollupExternal: ExternalOpt =
typeof userExternal === 'function'
? (id: string) =>
// honour user predicate first
userExternal(id) ||
// then our defaults
defaultExternalArray.some((pat) =>
typeof pat === 'string' ? pat === id : pat.test(id)
)
: [...defaultExternalArray, ...(userExternal ?? [])];
return defineConfig({
build: {
lib: {
entry: Object.fromEntries(
Object.entries(entries).map(([name, p]) => [
name,
resolve(process.cwd(), p),
])
),
formats: ['es'], // ESM only
fileName: (_format, entryName) => `${entryName}.js`,
},
target: 'es2022', // better DCE & smaller helpers
outDir: 'dist',
sourcemap: !isProd, // small prod artefacts
emptyOutDir: true,
minify: isProd ? 'esbuild' : false,
rollupOptions: {
// Don’t bundle peers/WP/Node/etc.
external: rollupExternal,
output: {
exports: 'named',
preserveModules: true, // keep module graph; plays well with TS paths
preserveModulesRoot: 'src',
entryFileNames: '[name].js',
banner: `/**
* ${packageName}
* @license EUPL-1.2
*/`,
},
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
unknownGlobalSideEffects: false,
},
},
},
// Help DCE across dependencies that check NODE_ENV
define: {
'process.env.NODE_ENV': JSON.stringify(
isProd ? 'production' : 'development'
),
},
// Optional prod trimming; safe for libs
esbuild:
isProd && (options.dropConsoleInProd ?? true)
? { drop: ['console', 'debugger'] }
: undefined,
plugins: [
dts({
include: [
'src/**/*.ts',
'src/**/*.tsx',
'../../types/**/*.d.ts',
],
outDir: 'dist',
rollupTypes: false, // avoid TS version mismatch warnings
}),
],
});
};