@codefluss/vite-config-lib 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/package.json +37 -0
- package/vite.config.base.ts +195 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @codefluss/vite-config-lib
|
|
2
|
+
|
|
3
|
+
Shared Vite configuration for building Codefluss plugin libraries.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @codefluss/vite-config-lib -D
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
// vite.config.ts
|
|
15
|
+
import { defineConfig } from 'vite';
|
|
16
|
+
import { createLibConfig } from '@codefluss/vite-config-lib';
|
|
17
|
+
|
|
18
|
+
export default defineConfig(
|
|
19
|
+
createLibConfig({
|
|
20
|
+
entry: './src/index.ts',
|
|
21
|
+
name: 'my-plugin',
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- Pre-configured React plugin
|
|
29
|
+
- TypeScript declaration generation via `vite-plugin-dts`
|
|
30
|
+
- Library mode settings optimized for plugins
|
|
31
|
+
- ESM output format
|
|
32
|
+
|
|
33
|
+
## Configuration Options
|
|
34
|
+
|
|
35
|
+
| Option | Type | Description |
|
|
36
|
+
| --------- | ---------- | ---------------------------------- |
|
|
37
|
+
| `entry` | `string` | Entry point for the library |
|
|
38
|
+
| `name` | `string` | Library name for UMD builds |
|
|
39
|
+
| `formats` | `string[]` | Output formats (default: `['es']`) |
|
|
40
|
+
|
|
41
|
+
## Dependencies
|
|
42
|
+
|
|
43
|
+
- `vite` ^7.2.7
|
|
44
|
+
- `@vitejs/plugin-react` ^5.1.2
|
|
45
|
+
- `vite-plugin-dts` ^4.5.4
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codefluss/vite-config-lib",
|
|
3
|
+
"version": "0.0.1-alpha.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Shared Vite configuration for library builds",
|
|
6
|
+
"main": "./vite.config.base.ts",
|
|
7
|
+
"types": "./vite.config.base.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./vite.config.base.ts",
|
|
11
|
+
"default": "./vite.config.base.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"vite.config.base.ts"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@vitejs/plugin-react": "^5.1.2",
|
|
19
|
+
"vite": "^7.2.7",
|
|
20
|
+
"vite-plugin-dts": "^4.5.4"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.9.3"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"vite",
|
|
27
|
+
"config",
|
|
28
|
+
"library",
|
|
29
|
+
"shared"
|
|
30
|
+
],
|
|
31
|
+
"author": "Codefluss",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {}
|
|
37
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { UserConfig, PluginOption } from 'vite';
|
|
2
|
+
import react from '@vitejs/plugin-react';
|
|
3
|
+
import dts from 'vite-plugin-dts';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for creating a library build with Vite
|
|
8
|
+
*/
|
|
9
|
+
export interface LibraryConfigOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Library name for UMD/IIFE builds (e.g., 'PluginCanvas2D')
|
|
12
|
+
*/
|
|
13
|
+
libraryName: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Entry point relative to package root (default: 'src/index.ts')
|
|
17
|
+
*/
|
|
18
|
+
entry?: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Output formats (default: ['es', 'cjs'])
|
|
22
|
+
* - 'es' for ESM (.mjs)
|
|
23
|
+
* - 'cjs' for CommonJS (.js)
|
|
24
|
+
*/
|
|
25
|
+
formats?: ('es' | 'cjs' | 'umd' | 'iife')[];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* External dependencies to exclude from bundle
|
|
29
|
+
* Common React dependencies are automatically included
|
|
30
|
+
*/
|
|
31
|
+
external?: string[];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Additional global variable mappings for UMD/IIFE builds
|
|
35
|
+
*/
|
|
36
|
+
globals?: Record<string, string>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Enable sourcemaps (default: true)
|
|
40
|
+
*/
|
|
41
|
+
sourcemap?: boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Enable minification with Terser (default: true)
|
|
45
|
+
*/
|
|
46
|
+
minify?: boolean | 'terser' | 'esbuild';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* DTS plugin configuration
|
|
50
|
+
*/
|
|
51
|
+
dts?: {
|
|
52
|
+
/**
|
|
53
|
+
* Include patterns (default: ['src/**\/*.ts', 'src/**\/*.tsx'])
|
|
54
|
+
*/
|
|
55
|
+
include?: string[];
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Exclude patterns (default: ['src/**\/*.test.ts', 'src/**\/*.test.tsx'])
|
|
59
|
+
*/
|
|
60
|
+
exclude?: string[];
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Skip TypeScript diagnostics (useful for React 19 + R3F compatibility)
|
|
64
|
+
*/
|
|
65
|
+
skipDiagnostics?: boolean;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Additional Vite plugins
|
|
70
|
+
*/
|
|
71
|
+
plugins?: PluginOption[];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Override any Vite config option
|
|
75
|
+
*/
|
|
76
|
+
viteConfigOverrides?: Partial<UserConfig>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates a Vite configuration for library builds with sensible defaults
|
|
81
|
+
*
|
|
82
|
+
* Features:
|
|
83
|
+
* - React plugin with automatic JSX runtime
|
|
84
|
+
* - TypeScript declaration files with vite-plugin-dts
|
|
85
|
+
* - Dual format output (ESM + CJS) by default
|
|
86
|
+
* - React, React-DOM externalized automatically
|
|
87
|
+
* - Sourcemaps and minification enabled by default
|
|
88
|
+
* - Tree-shaking friendly builds
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* // vite.config.ts
|
|
93
|
+
* import { createLibraryConfig } from '@codefluss/vite-config-lib';
|
|
94
|
+
*
|
|
95
|
+
* export default createLibraryConfig({
|
|
96
|
+
* libraryName: 'PluginCanvas2D',
|
|
97
|
+
* external: ['fabric', 'zustand'],
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function createLibraryConfig(options: LibraryConfigOptions): UserConfig {
|
|
102
|
+
const {
|
|
103
|
+
libraryName,
|
|
104
|
+
entry = 'src/index.ts',
|
|
105
|
+
formats = ['es', 'cjs'],
|
|
106
|
+
external = [],
|
|
107
|
+
globals = {},
|
|
108
|
+
sourcemap = true,
|
|
109
|
+
minify = 'terser',
|
|
110
|
+
dts: dtsConfig = {},
|
|
111
|
+
plugins = [],
|
|
112
|
+
viteConfigOverrides = {},
|
|
113
|
+
} = options;
|
|
114
|
+
|
|
115
|
+
// Standard React externals (always excluded from bundles)
|
|
116
|
+
const standardExternals = [
|
|
117
|
+
'react',
|
|
118
|
+
'react-dom',
|
|
119
|
+
'react/jsx-runtime',
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
// Combine standard externals with custom externals
|
|
123
|
+
const allExternals = [...new Set([...standardExternals, ...external])];
|
|
124
|
+
|
|
125
|
+
// Standard globals for UMD/IIFE builds
|
|
126
|
+
const standardGlobals: Record<string, string> = {
|
|
127
|
+
react: 'React',
|
|
128
|
+
'react-dom': 'ReactDOM',
|
|
129
|
+
...globals,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// DTS plugin configuration with defaults
|
|
133
|
+
const dtsPluginConfig = {
|
|
134
|
+
insertTypesEntry: true,
|
|
135
|
+
include: dtsConfig.include ?? ['src/**/*.ts', 'src/**/*.tsx'],
|
|
136
|
+
exclude: dtsConfig.exclude ?? ['src/**/*.test.ts', 'src/**/*.test.tsx'],
|
|
137
|
+
skipDiagnostics: dtsConfig.skipDiagnostics ?? false,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Base configuration
|
|
141
|
+
const config: UserConfig = {
|
|
142
|
+
plugins: [
|
|
143
|
+
react(),
|
|
144
|
+
dts(dtsPluginConfig),
|
|
145
|
+
...plugins,
|
|
146
|
+
],
|
|
147
|
+
build: {
|
|
148
|
+
lib: {
|
|
149
|
+
entry: resolve(process.cwd(), entry),
|
|
150
|
+
name: libraryName,
|
|
151
|
+
formats,
|
|
152
|
+
fileName: (format) => {
|
|
153
|
+
// ESM -> .mjs, CJS -> .js, others use default
|
|
154
|
+
if (format === 'es') return 'index.mjs';
|
|
155
|
+
if (format === 'cjs') return 'index.js';
|
|
156
|
+
return `index.${format}.js`;
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
rollupOptions: {
|
|
160
|
+
external: allExternals,
|
|
161
|
+
output: {
|
|
162
|
+
globals: standardGlobals,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
sourcemap,
|
|
166
|
+
minify,
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Apply overrides (deep merge)
|
|
171
|
+
const mergedLib = {
|
|
172
|
+
...config.build?.lib,
|
|
173
|
+
...viteConfigOverrides.build?.lib,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
...config,
|
|
178
|
+
...viteConfigOverrides,
|
|
179
|
+
plugins: [...(config.plugins ?? []), ...(viteConfigOverrides.plugins ?? [])],
|
|
180
|
+
build: {
|
|
181
|
+
...config.build,
|
|
182
|
+
...viteConfigOverrides.build,
|
|
183
|
+
lib: (mergedLib.entry ? mergedLib : config.build?.lib) as any,
|
|
184
|
+
rollupOptions: {
|
|
185
|
+
...config.build?.rollupOptions,
|
|
186
|
+
...viteConfigOverrides.build?.rollupOptions,
|
|
187
|
+
external: viteConfigOverrides.build?.rollupOptions?.external ?? allExternals,
|
|
188
|
+
output: {
|
|
189
|
+
...config.build?.rollupOptions?.output,
|
|
190
|
+
...viteConfigOverrides.build?.rollupOptions?.output,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|