fe-stack 0.0.2 → 0.0.4

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 CHANGED
@@ -95,23 +95,18 @@ export default {
95
95
 
96
96
  #### Vue 应用模式
97
97
 
98
- 在项目中创建 `vite.config.ts`:
99
-
100
98
  ```typescript
101
- import { defineConfig } from 'vite';
102
- import { createBaseConfig } from 'fe-stack/vite.config.base.js';
103
-
104
- const baseConfig = createBaseConfig(import.meta.dirname, { mode: 'app' });
105
-
106
- export default defineConfig({
107
- ...baseConfig,
108
- // 项目特定配置
109
- server: {
110
- port: 5173,
111
- proxy: {
112
- '/api': {
113
- target: 'http://your-api-server',
114
- changeOrigin: true,
99
+ import { createBaseConfig } from 'fe-stack/vite.config.base';
100
+
101
+ export default createBaseConfig(import.meta.dirname, {
102
+ viteConfig: {
103
+ server: {
104
+ port: 5173,
105
+ proxy: {
106
+ '/api': {
107
+ target: 'http://your-api-server',
108
+ changeOrigin: true,
109
+ }
115
110
  }
116
111
  }
117
112
  }
@@ -120,27 +115,26 @@ export default defineConfig({
120
115
 
121
116
  #### Lib 打包模式
122
117
 
123
- 用于打包纯 TypeScript 库:
124
-
125
118
  ```typescript
126
- import { defineConfig } from 'vite';
127
- import { createBaseConfig } from 'fe-stack/vite.config.base.js';
119
+ import { createBaseConfig } from 'fe-stack/vite.config.base';
128
120
 
129
- const baseConfig = createBaseConfig(import.meta.dirname, {
121
+ export default createBaseConfig(import.meta.dirname, {
130
122
  mode: 'lib',
131
- libOptions: {
132
- lib: {
133
- name: 'MyLib', // UMD 全局变量名
123
+ viteConfig: {
124
+ build: {
125
+ lib: {
126
+ entry: './src/index.ts',
127
+ name: 'MyLib',
128
+ fileName: 'index',
129
+ },
134
130
  },
135
- dtsExclude: ['src/**/*.test.ts'], // 排除测试文件
136
131
  },
137
132
  });
138
-
139
- export default defineConfig(baseConfig);
140
133
  ```
141
134
 
142
135
  **特性:**
143
- - ✅ 自动配置 `vite-plugin-dts` 生成类型文件
144
- - ✅ 自动合并所有类型到单个 `.d.ts` 文件
136
+ - ✅ 完整的 TypeScript 类型支持
137
+ - ✅ 使用 `mergeConfig` 优雅合并配置
138
+ - ✅ lib 模式自动配置 `vite-plugin-dts`
145
139
  - ✅ 默认打包为 ESM 格式
146
- - ✅ entry 默认为 `src/index.ts`
140
+ - ✅ 所有配置通过 `viteConfig` 统一管理,简单直观
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "fe-stack",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "共享的配置文件集合,用于 Vite、Tailwind、Biome、Prettier 和 TypeScript",
5
5
  "type": "module",
6
6
  "exports": {
7
- "./vite.config.base.js": "./vite.config.base.js",
7
+ "./vite.config.base": {
8
+ "types": "./vite.config.base.d.ts",
9
+ "import": "./vite.config.base.js"
10
+ },
8
11
  "./tsconfig.*.json": "./tsconfig.*.json",
9
12
  "./biome.json": "./biome.json",
10
13
  "./prettier.json": "./prettier.json",
@@ -17,6 +20,7 @@
17
20
  "tsconfig.app.json",
18
21
  "tsconfig.node.json",
19
22
  "vite.config.base.js",
23
+ "vite.config.base.d.ts",
20
24
  "README.md"
21
25
  ],
22
26
  "keywords": [
@@ -0,0 +1,26 @@
1
+ import type { UserConfig } from 'vite';
2
+
3
+ export interface CreateBaseConfigOptions {
4
+ /**
5
+ * 构建模式
6
+ * - 'app': Vue 应用模式,包含 Vue Router、Auto Import 等
7
+ * - 'lib': 库打包模式,配置 vite-plugin-dts
8
+ */
9
+ mode?: 'app' | 'lib';
10
+
11
+ /**
12
+ * 额外的 Vite 配置,会与基础配置合并
13
+ */
14
+ viteConfig?: UserConfig;
15
+ }
16
+
17
+ /**
18
+ * 创建基础 Vite 配置
19
+ * @param dirname - 项目根目录路径(通常使用 import.meta.dirname)
20
+ * @param options - 配置选项
21
+ * @returns Vite 配置
22
+ */
23
+ export function createBaseConfig(
24
+ dirname: string,
25
+ options?: CreateBaseConfigOptions,
26
+ ): import('vite').UserConfigExport;
@@ -4,11 +4,26 @@ import vue from '@vitejs/plugin-vue';
4
4
  import AutoImport from 'unplugin-auto-import/vite';
5
5
  import Components from 'unplugin-vue-components/vite';
6
6
  import VueRouter from 'unplugin-vue-router/vite';
7
+ import { defineConfig, mergeConfig } from 'vite';
7
8
  import dts from 'vite-plugin-dts';
8
9
 
10
+ /**
11
+ * @typedef {import('vite').UserConfig} UserConfig
12
+ * @typedef {Object} CreateBaseConfigOptions
13
+ * @property {'app' | 'lib'} [mode] - 构建模式:'app' 为 Vue 应用,'lib' 为库打包
14
+ * @property {UserConfig} [viteConfig] - 额外的 Vite 配置,会与基础配置合并
15
+ */
16
+
17
+ /**
18
+ * 创建基础 Vite 配置
19
+ * @param {string} dirname - 项目根目录路径
20
+ * @param {CreateBaseConfigOptions} [options] - 配置选项
21
+ * @returns {import('vite').UserConfigExport} Vite 配置
22
+ */
9
23
  export function createBaseConfig(dirname, options = {}) {
10
- const { mode = 'app', libOptions = {} } = options;
24
+ const { mode = 'app', viteConfig = {} } = options;
11
25
 
26
+ /** @type {UserConfig} */
12
27
  const baseConfig = {
13
28
  resolve: {
14
29
  alias: {
@@ -26,6 +41,14 @@ export function createBaseConfig(dirname, options = {}) {
26
41
  ],
27
42
  },
28
43
  plugins: [],
44
+ worker: {
45
+ format: 'es',
46
+ rollupOptions: {
47
+ output: {
48
+ format: 'es',
49
+ },
50
+ },
51
+ },
29
52
  };
30
53
 
31
54
  // App mode: 包含 Vue 全家桶
@@ -47,41 +70,26 @@ export function createBaseConfig(dirname, options = {}) {
47
70
  );
48
71
  }
49
72
 
50
- // Lib mode: 纯 TypeScript,用于打包库
73
+ // Lib mode: 提供默认配置
51
74
  if (mode === 'lib') {
52
- // 添加 lib 构建配置
53
75
  baseConfig.build = {
54
76
  lib: {
55
77
  entry: path.resolve(dirname, 'src/index.ts'),
56
78
  formats: ['es'],
57
79
  fileName: 'index',
58
- ...libOptions.lib,
59
80
  },
60
81
  };
61
82
 
62
- // 添加 dts 插件
63
- // 重要:include 需要使用绝对路径,否则在某些项目中会报错
64
- const dtsInclude = libOptions.dtsInclude || ['src/**/*.ts'];
65
- const absoluteInclude = dtsInclude.map((pattern) =>
66
- pattern.startsWith('/') ? pattern : path.resolve(dirname, pattern),
67
- );
68
-
69
- const dtsExclude = libOptions.dtsExclude || [];
70
- const absoluteExclude = dtsExclude.map((pattern) =>
71
- pattern.startsWith('/') ? pattern : path.resolve(dirname, pattern),
72
- );
73
-
83
+ // 默认添加 dts 插件
74
84
  baseConfig.plugins.push(
75
85
  dts({
76
- include: absoluteInclude,
77
- exclude: absoluteExclude,
78
- rollupTypes: true,
79
- // 确保 root 设置为项目根目录
86
+ include: ['src/**/*.ts'],
80
87
  root: dirname,
81
- ...libOptions.dts,
88
+ rollupTypes: true,
82
89
  }),
83
90
  );
84
91
  }
85
92
 
86
- return baseConfig;
93
+ // 使用 mergeConfig 合并用户自定义配置
94
+ return defineConfig(mergeConfig(baseConfig, viteConfig));
87
95
  }