@moluoxixi/vite-config 0.0.29 → 0.0.31
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/es/index.mjs +54 -7
- package/es/src/_types/index.d.ts +2 -1
- package/es/src/_utils/detectFramework.d.ts +42 -0
- package/es/src/_utils/index.d.ts +1 -0
- package/es/src/index.d.ts +1 -1
- package/lib/index.cjs +54 -7
- package/lib/src/_types/index.d.ts +2 -1
- package/lib/src/_utils/detectFramework.d.ts +42 -0
- package/lib/src/_utils/index.d.ts +1 -0
- package/lib/src/index.d.ts +1 -1
- package/package.json +1 -1
package/es/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
import "vue";
|
|
5
4
|
import fs from "node:fs";
|
|
6
5
|
import path from "node:path";
|
|
7
6
|
import process$1 from "node:process";
|
|
7
|
+
import "vue";
|
|
8
8
|
import "dotenv";
|
|
9
9
|
import tailwindcss from "@tailwindcss/postcss";
|
|
10
10
|
import autoprefixer from "autoprefixer";
|
|
@@ -78,6 +78,48 @@ function validateMutuallyExclusive(values, defaultKey) {
|
|
|
78
78
|
}
|
|
79
79
|
return result;
|
|
80
80
|
}
|
|
81
|
+
function detectFramework(config, rootDir) {
|
|
82
|
+
const root = rootDir || process$1.cwd();
|
|
83
|
+
const packageJsonPath = path.resolve(root, "package.json");
|
|
84
|
+
let vue = config.vue;
|
|
85
|
+
let react = config.react;
|
|
86
|
+
let vitepress = config.vitepress;
|
|
87
|
+
const hasExplicitFramework = vue === true || react === true || vitepress === true;
|
|
88
|
+
if (!hasExplicitFramework) {
|
|
89
|
+
let dependencies = {};
|
|
90
|
+
let devDependencies = {};
|
|
91
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
92
|
+
try {
|
|
93
|
+
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf-8");
|
|
94
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
95
|
+
dependencies = packageJson.dependencies || {};
|
|
96
|
+
devDependencies = packageJson.devDependencies || {};
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.warn(`无法读取 package.json: ${packageJsonPath}`, error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (vue === void 0) {
|
|
102
|
+
if (dependencies.vue || devDependencies.vue) {
|
|
103
|
+
vue = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (react === void 0 && vue !== true) {
|
|
107
|
+
if (dependencies.react || devDependencies.react) {
|
|
108
|
+
react = true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (vitepress === void 0 && vue !== true && react !== true) {
|
|
112
|
+
if (dependencies.vitepress || devDependencies.vitepress) {
|
|
113
|
+
vitepress = true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
vue: vue ?? false,
|
|
119
|
+
react: react ?? false,
|
|
120
|
+
vitepress: vitepress ?? false
|
|
121
|
+
};
|
|
122
|
+
}
|
|
81
123
|
function changeHtmlClassPrefix(htmlString = "", oldPrefix = "", newPrefix = "") {
|
|
82
124
|
const regex = new RegExp(
|
|
83
125
|
`(class|style)\\s*:\\s*((["']((${oldPrefix}\\b)-).*["'])|((_normalizeClass|_normalizeStyle)\\(.*(${oldPrefix}\\b)-.*\\)))`,
|
|
@@ -129,14 +171,11 @@ function addScopedAndReplacePrefixPlugin({
|
|
|
129
171
|
}
|
|
130
172
|
};
|
|
131
173
|
}
|
|
132
|
-
async function getViteConfig(Config, params) {
|
|
174
|
+
async function getViteConfig(Config = {}, params) {
|
|
133
175
|
const configResult = typeof Config === "function" ? Config(params) : Config;
|
|
134
|
-
if (!configResult || !configResult.rootPath) {
|
|
135
|
-
throw new Error("rootPath is required in ViteConfig");
|
|
136
|
-
}
|
|
137
176
|
const config = configResult;
|
|
138
177
|
const { mode = "base" } = params || {};
|
|
139
|
-
const rootPath = config.rootPath;
|
|
178
|
+
const rootPath = config.rootPath || process.cwd();
|
|
140
179
|
const modeConfig = config.mode || {};
|
|
141
180
|
const baseConfig = modeConfig.base || {};
|
|
142
181
|
const currentModeConfig = modeConfig[mode] || {};
|
|
@@ -164,8 +203,16 @@ async function getViteConfig(Config, params) {
|
|
|
164
203
|
react: reactRaw = config.react,
|
|
165
204
|
vitepress: vitepressRaw = config.vitepress
|
|
166
205
|
} = viteEnv;
|
|
206
|
+
const frameworkResult = detectFramework(
|
|
207
|
+
{
|
|
208
|
+
vue: vueRaw,
|
|
209
|
+
react: reactRaw,
|
|
210
|
+
vitepress: vitepressRaw
|
|
211
|
+
},
|
|
212
|
+
rootPath
|
|
213
|
+
);
|
|
167
214
|
const { vue, react, vitepress } = validateMutuallyExclusive(
|
|
168
|
-
|
|
215
|
+
frameworkResult,
|
|
169
216
|
"vue"
|
|
170
217
|
);
|
|
171
218
|
const appTitle = config.appTitle;
|
package/es/src/_types/index.d.ts
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 框架配置类型
|
|
3
|
+
*/
|
|
4
|
+
export interface FrameworkConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Vue 框架配置
|
|
7
|
+
*/
|
|
8
|
+
vue?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* React 框架配置
|
|
11
|
+
*/
|
|
12
|
+
react?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* VitePress 框架配置
|
|
15
|
+
*/
|
|
16
|
+
vitepress?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 框架检测结果类型
|
|
20
|
+
*/
|
|
21
|
+
export interface FrameworkResult {
|
|
22
|
+
/**
|
|
23
|
+
* Vue 框架
|
|
24
|
+
*/
|
|
25
|
+
vue: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* React 框架
|
|
28
|
+
*/
|
|
29
|
+
react: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* VitePress 框架
|
|
32
|
+
*/
|
|
33
|
+
vitepress: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 检测项目依赖中的框架
|
|
37
|
+
* 优先级:显示传入 > 检测,且优先级为 vue/react > vitepress
|
|
38
|
+
* @param config - 框架配置对象,包含 vue、react、vitepress
|
|
39
|
+
* @param rootDir - 项目根目录,如果不传入则使用 process.cwd()
|
|
40
|
+
* @returns 检测后的框架配置对象
|
|
41
|
+
*/
|
|
42
|
+
export declare function detectFramework(config: FrameworkConfig, rootDir?: string): FrameworkResult;
|
package/es/src/_utils/index.d.ts
CHANGED
package/es/src/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ConfigEnv } from 'vite';
|
|
2
2
|
import { ViteConfigType } from './_types/index.ts';
|
|
3
|
-
declare function getViteConfig(Config
|
|
3
|
+
declare function getViteConfig(Config?: ViteConfigType, params?: ConfigEnv): Promise<Record<string, any>>;
|
|
4
4
|
declare function createViteConfig(Config: ViteConfigType): import('vite').UserConfigFnPromise;
|
|
5
5
|
export { createViteConfig, getViteConfig, };
|
|
6
6
|
export default createViteConfig;
|
package/lib/index.cjs
CHANGED
|
@@ -24,10 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
));
|
|
25
25
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
26
26
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
27
|
-
require("vue");
|
|
28
27
|
const fs = require("node:fs");
|
|
29
28
|
const path = require("node:path");
|
|
30
29
|
const process$1 = require("node:process");
|
|
30
|
+
require("vue");
|
|
31
31
|
require("dotenv");
|
|
32
32
|
const tailwindcss = require("@tailwindcss/postcss");
|
|
33
33
|
const autoprefixer = require("autoprefixer");
|
|
@@ -101,6 +101,48 @@ function validateMutuallyExclusive(values, defaultKey) {
|
|
|
101
101
|
}
|
|
102
102
|
return result;
|
|
103
103
|
}
|
|
104
|
+
function detectFramework(config, rootDir) {
|
|
105
|
+
const root = rootDir || process$1.cwd();
|
|
106
|
+
const packageJsonPath = path.resolve(root, "package.json");
|
|
107
|
+
let vue = config.vue;
|
|
108
|
+
let react = config.react;
|
|
109
|
+
let vitepress = config.vitepress;
|
|
110
|
+
const hasExplicitFramework = vue === true || react === true || vitepress === true;
|
|
111
|
+
if (!hasExplicitFramework) {
|
|
112
|
+
let dependencies = {};
|
|
113
|
+
let devDependencies = {};
|
|
114
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
115
|
+
try {
|
|
116
|
+
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf-8");
|
|
117
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
118
|
+
dependencies = packageJson.dependencies || {};
|
|
119
|
+
devDependencies = packageJson.devDependencies || {};
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.warn(`无法读取 package.json: ${packageJsonPath}`, error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (vue === void 0) {
|
|
125
|
+
if (dependencies.vue || devDependencies.vue) {
|
|
126
|
+
vue = true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (react === void 0 && vue !== true) {
|
|
130
|
+
if (dependencies.react || devDependencies.react) {
|
|
131
|
+
react = true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (vitepress === void 0 && vue !== true && react !== true) {
|
|
135
|
+
if (dependencies.vitepress || devDependencies.vitepress) {
|
|
136
|
+
vitepress = true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
vue: vue ?? false,
|
|
142
|
+
react: react ?? false,
|
|
143
|
+
vitepress: vitepress ?? false
|
|
144
|
+
};
|
|
145
|
+
}
|
|
104
146
|
function changeHtmlClassPrefix(htmlString = "", oldPrefix = "", newPrefix = "") {
|
|
105
147
|
const regex = new RegExp(
|
|
106
148
|
`(class|style)\\s*:\\s*((["']((${oldPrefix}\\b)-).*["'])|((_normalizeClass|_normalizeStyle)\\(.*(${oldPrefix}\\b)-.*\\)))`,
|
|
@@ -152,14 +194,11 @@ function addScopedAndReplacePrefixPlugin({
|
|
|
152
194
|
}
|
|
153
195
|
};
|
|
154
196
|
}
|
|
155
|
-
async function getViteConfig(Config, params) {
|
|
197
|
+
async function getViteConfig(Config = {}, params) {
|
|
156
198
|
const configResult = typeof Config === "function" ? Config(params) : Config;
|
|
157
|
-
if (!configResult || !configResult.rootPath) {
|
|
158
|
-
throw new Error("rootPath is required in ViteConfig");
|
|
159
|
-
}
|
|
160
199
|
const config = configResult;
|
|
161
200
|
const { mode = "base" } = params || {};
|
|
162
|
-
const rootPath = config.rootPath;
|
|
201
|
+
const rootPath = config.rootPath || process.cwd();
|
|
163
202
|
const modeConfig = config.mode || {};
|
|
164
203
|
const baseConfig = modeConfig.base || {};
|
|
165
204
|
const currentModeConfig = modeConfig[mode] || {};
|
|
@@ -187,8 +226,16 @@ async function getViteConfig(Config, params) {
|
|
|
187
226
|
react: reactRaw = config.react,
|
|
188
227
|
vitepress: vitepressRaw = config.vitepress
|
|
189
228
|
} = viteEnv;
|
|
229
|
+
const frameworkResult = detectFramework(
|
|
230
|
+
{
|
|
231
|
+
vue: vueRaw,
|
|
232
|
+
react: reactRaw,
|
|
233
|
+
vitepress: vitepressRaw
|
|
234
|
+
},
|
|
235
|
+
rootPath
|
|
236
|
+
);
|
|
190
237
|
const { vue, react, vitepress } = validateMutuallyExclusive(
|
|
191
|
-
|
|
238
|
+
frameworkResult,
|
|
192
239
|
"vue"
|
|
193
240
|
);
|
|
194
241
|
const appTitle = config.appTitle;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 框架配置类型
|
|
3
|
+
*/
|
|
4
|
+
export interface FrameworkConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Vue 框架配置
|
|
7
|
+
*/
|
|
8
|
+
vue?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* React 框架配置
|
|
11
|
+
*/
|
|
12
|
+
react?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* VitePress 框架配置
|
|
15
|
+
*/
|
|
16
|
+
vitepress?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 框架检测结果类型
|
|
20
|
+
*/
|
|
21
|
+
export interface FrameworkResult {
|
|
22
|
+
/**
|
|
23
|
+
* Vue 框架
|
|
24
|
+
*/
|
|
25
|
+
vue: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* React 框架
|
|
28
|
+
*/
|
|
29
|
+
react: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* VitePress 框架
|
|
32
|
+
*/
|
|
33
|
+
vitepress: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 检测项目依赖中的框架
|
|
37
|
+
* 优先级:显示传入 > 检测,且优先级为 vue/react > vitepress
|
|
38
|
+
* @param config - 框架配置对象,包含 vue、react、vitepress
|
|
39
|
+
* @param rootDir - 项目根目录,如果不传入则使用 process.cwd()
|
|
40
|
+
* @returns 检测后的框架配置对象
|
|
41
|
+
*/
|
|
42
|
+
export declare function detectFramework(config: FrameworkConfig, rootDir?: string): FrameworkResult;
|
package/lib/src/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ConfigEnv } from 'vite';
|
|
2
2
|
import { ViteConfigType } from './_types/index.ts';
|
|
3
|
-
declare function getViteConfig(Config
|
|
3
|
+
declare function getViteConfig(Config?: ViteConfigType, params?: ConfigEnv): Promise<Record<string, any>>;
|
|
4
4
|
declare function createViteConfig(Config: ViteConfigType): import('vite').UserConfigFnPromise;
|
|
5
5
|
export { createViteConfig, getViteConfig, };
|
|
6
6
|
export default createViteConfig;
|