akfun 5.1.13 → 6.0.0
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 +342 -215
- package/module/main.js +14 -1
- package/package.json +15 -15
- package/src/build2esm.js +9 -7
- package/src/config/babel.config.js +2 -0
- package/src/config/index.js +19 -12
- package/src/config/rollup.config.js +22 -5
- package/src/initData/akfun-package.json +14 -10
- package/src/initData/config/babel.config.js +3 -1
- package/src/manage/ConfigManager.js +315 -0
- package/src/utils/configValidator.js +435 -0
- package/src/webpack/webpack.base.conf.js +52 -11
- package/src/webpack/webpack.dev.conf.js +1 -1
- package/src/webpack/webpack.library.conf.js +1 -1
- package/src/webpack/webpack.prod.conf.js +1 -1
package/module/main.js
CHANGED
|
@@ -12,11 +12,18 @@ const getCurWebpackConfig = require('../src/utils/getCurWebpackConfig.js'); //
|
|
|
12
12
|
const aliBOS = require('../src/oss/aliBos.js');
|
|
13
13
|
const baiduBOS = require('../src/oss/baiduBos.js');
|
|
14
14
|
|
|
15
|
+
// 新增:配置管理和环境管理
|
|
16
|
+
const configManager = require('../src/config/ConfigManager');
|
|
17
|
+
const { validateConfig } = require('../src/utils/configValidator');
|
|
18
|
+
|
|
15
19
|
module.exports = {
|
|
20
|
+
// 核心功能
|
|
16
21
|
dev: devAction,
|
|
17
22
|
build: buildAction,
|
|
18
23
|
build2esm,
|
|
19
24
|
inspect,
|
|
25
|
+
|
|
26
|
+
// 工具方法
|
|
20
27
|
gitClone,
|
|
21
28
|
createFile,
|
|
22
29
|
resolve,
|
|
@@ -24,6 +31,12 @@ module.exports = {
|
|
|
24
31
|
deepMergeConfig,
|
|
25
32
|
getCurWebpackConfig,
|
|
26
33
|
curWebpackBaseConfPath: getCurWebpackConfig('base'),
|
|
34
|
+
|
|
35
|
+
// OSS 上传
|
|
27
36
|
aliBOS,
|
|
28
|
-
baiduBOS
|
|
37
|
+
baiduBOS,
|
|
38
|
+
|
|
39
|
+
// 新增:配置管理
|
|
40
|
+
configManager,
|
|
41
|
+
validateConfig
|
|
29
42
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "akfun",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "前端脚手架:支持Vue技术栈和react技术栈",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"前端工程",
|
|
@@ -62,21 +62,21 @@
|
|
|
62
62
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
|
63
63
|
"@babel/plugin-syntax-import-meta": "^7.10.4",
|
|
64
64
|
"@babel/plugin-transform-runtime": "^7.27.1",
|
|
65
|
-
"@babel/polyfill": "^7.10.1",
|
|
66
65
|
"@babel/runtime": "^7.27.1",
|
|
67
66
|
"@babel/preset-env": "^7.16.11",
|
|
68
67
|
"@babel/preset-react": "^7.16.7",
|
|
69
68
|
"@babel/preset-typescript": "^7.16.7",
|
|
70
69
|
"@babel/register": "^7.17.0",
|
|
71
70
|
"@mapbox/stylelint-processor-arbitrary-tags": "^0.4.0",
|
|
72
|
-
"@rollup/plugin-alias": "^
|
|
73
|
-
"@rollup/plugin-babel": "^
|
|
74
|
-
"@rollup/plugin-commonjs": "^
|
|
75
|
-
"@rollup/plugin-image": "^
|
|
71
|
+
"@rollup/plugin-alias": "^6.0.0",
|
|
72
|
+
"@rollup/plugin-babel": "^6.1.0",
|
|
73
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
74
|
+
"@rollup/plugin-image": "^3.0.3",
|
|
76
75
|
"@svgr/rollup": "^8.1.0",
|
|
77
|
-
"@rollup/plugin-json": "^
|
|
78
|
-
"@rollup/plugin-node-resolve": "^
|
|
79
|
-
"@rollup/plugin-typescript": "^
|
|
76
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
77
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
78
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
79
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
80
80
|
"@svgr/webpack": "^6.2.1",
|
|
81
81
|
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
|
82
82
|
"@typescript-eslint/parser": "^5.10.2",
|
|
@@ -88,6 +88,7 @@
|
|
|
88
88
|
"chalk": "^4.0.0",
|
|
89
89
|
"compression-webpack-plugin": "^9.2.0",
|
|
90
90
|
"connect-history-api-fallback": "^2.0.0",
|
|
91
|
+
"core-js": "^3.47.0",
|
|
91
92
|
"copy-webpack-plugin": "^10.2.4",
|
|
92
93
|
"css-loader": "^6.6.0",
|
|
93
94
|
"css-minimizer-webpack-plugin": "^3.4.1",
|
|
@@ -129,12 +130,11 @@
|
|
|
129
130
|
"progress-bar-webpack-plugin": "^2.1.0",
|
|
130
131
|
"qs": "^6.10.3",
|
|
131
132
|
"rimraf": "^3.0.2",
|
|
132
|
-
"rollup": "^
|
|
133
|
-
"rollup-plugin-node-externals": "^
|
|
133
|
+
"rollup": "^4.53.3",
|
|
134
|
+
"rollup-plugin-node-externals": "^8.1.2",
|
|
134
135
|
"rollup-plugin-postcss": "^4.0.2",
|
|
135
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
136
136
|
"rollup-plugin-vue": "^6.0.0",
|
|
137
|
-
"sass": "^1.
|
|
137
|
+
"sass": "^1.94.2",
|
|
138
138
|
"sass-loader": "^12.4.0",
|
|
139
139
|
"less": "^4.1.3",
|
|
140
140
|
"less-loader": "^11.0.0",
|
|
@@ -175,8 +175,8 @@
|
|
|
175
175
|
"prettier": "^2.5.1"
|
|
176
176
|
},
|
|
177
177
|
"engines": {
|
|
178
|
-
"node": ">=
|
|
179
|
-
"npm": ">=
|
|
178
|
+
"node": ">= 16.0.0",
|
|
179
|
+
"npm": ">= 8.0.0"
|
|
180
180
|
},
|
|
181
181
|
"browserslist": [
|
|
182
182
|
"> 1%",
|
package/src/build2esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const ora = require('ora');
|
|
2
2
|
const rollup = require('rollup');
|
|
3
|
-
const { terser } = require('rollup
|
|
3
|
+
const { terser } = require('@rollup/plugin-terser'); // 压缩
|
|
4
4
|
const projectConfig = require('./config/index'); // 引入当前项目配置文件
|
|
5
5
|
const defaultConfig = require('./config/default.config');
|
|
6
6
|
const rollupConfig = require('./config/rollup.config'); // rollup的配置文件
|
|
@@ -12,18 +12,20 @@ async function build2esmFunc(options, curConfig) {
|
|
|
12
12
|
// create a bundle
|
|
13
13
|
const bundle = await rollup.rollup({
|
|
14
14
|
input: options.input,
|
|
15
|
-
external: options.externals,
|
|
15
|
+
external: options.external || options.externals, // 兼容新旧两种写法
|
|
16
16
|
plugins: options.plugins
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
if (isArray(options.output)) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
});
|
|
20
|
+
// 等待所有输出完成
|
|
21
|
+
await Promise.all(options.output.map((outputItem) => bundle.write(outputItem)));
|
|
23
22
|
} else if (isObject(options.output)) {
|
|
24
23
|
// or write the bundle to disk
|
|
25
24
|
await bundle.write(options.output);
|
|
26
25
|
}
|
|
26
|
+
|
|
27
|
+
// 关闭 bundle 以释放资源
|
|
28
|
+
await bundle.close();
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
// 构建脚本:一般用于构建生产环境的代码
|
|
@@ -60,8 +62,8 @@ module.exports = function (fileName, akfunConfig, _consoleTag) {
|
|
|
60
62
|
externals = externals.concat(Object.keys(build2esmExternal));
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
// 添加到 rollup
|
|
64
|
-
curRollupConfig.
|
|
65
|
+
// 添加到 rollup 配置中(rollup 使用 external 而不是 externals)
|
|
66
|
+
curRollupConfig.external = externals;
|
|
65
67
|
|
|
66
68
|
if (build2esm && build2esm.output) {
|
|
67
69
|
curRollupConfig.output = build2esm.output;
|
|
@@ -5,6 +5,8 @@ module.exports = {
|
|
|
5
5
|
{
|
|
6
6
|
loose: true,
|
|
7
7
|
modules: false, // 是否启用将ES6模块语法转换为其他模块类型的功能,当前设置为false,以便webpack进行tree shaking。
|
|
8
|
+
useBuiltIns: 'usage', // 按需引入 polyfill,替代 @babel/polyfill
|
|
9
|
+
corejs: 3, // 使用 core-js 3.x 版本
|
|
8
10
|
targets: {
|
|
9
11
|
browsers: ['> 1%', 'last 2 versions', 'not ie <= 8']
|
|
10
12
|
}
|
package/src/config/index.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const getConfigObj = require('../utils/getConfigObj');
|
|
5
|
-
const deepMergeConfig = require('../utils/deepMergeConfig');
|
|
6
|
-
|
|
7
|
-
/** akfun脚手架赋予当前项目的默认配置
|
|
8
|
-
* 备注:项目根目录的akfun.config.js的配置内容优先级高于defultAKFunConfig
|
|
1
|
+
/**
|
|
2
|
+
* 配置入口文件
|
|
3
|
+
* 统一加载和管理 AKFun 配置
|
|
9
4
|
*/
|
|
10
5
|
|
|
11
|
-
//
|
|
12
|
-
const
|
|
6
|
+
// 使用新的配置管理器
|
|
7
|
+
const configManager = require('../manage/ConfigManager');
|
|
13
8
|
|
|
14
|
-
//
|
|
15
|
-
|
|
9
|
+
// 自动加载用户配置文件
|
|
10
|
+
configManager.autoLoadConfig();
|
|
11
|
+
|
|
12
|
+
// 合并配置
|
|
13
|
+
const mergedConfig = configManager.mergeConfig();
|
|
14
|
+
|
|
15
|
+
// 校验当前项目配置
|
|
16
|
+
configManager.validateConfig(mergedConfig);
|
|
17
|
+
|
|
18
|
+
// 导出合并后的配置(向后兼容)
|
|
19
|
+
module.exports = mergedConfig;
|
|
20
|
+
|
|
21
|
+
// 同时导出配置管理器实例,供高级用户使用
|
|
22
|
+
module.exports.configManager = configManager;
|
|
@@ -7,7 +7,7 @@ const commonjs = require('@rollup/plugin-commonjs'); // 识别cmd模块
|
|
|
7
7
|
const vue = require('rollup-plugin-vue');
|
|
8
8
|
const json = require('@rollup/plugin-json'); // 识别json类型文件
|
|
9
9
|
const image = require('@rollup/plugin-image'); // 图片处理器
|
|
10
|
-
const { terser } = require('rollup
|
|
10
|
+
const { terser } = require('@rollup/plugin-terser'); // 压缩
|
|
11
11
|
const alias = require('@rollup/plugin-alias'); // 简写配置
|
|
12
12
|
// css相关处理器
|
|
13
13
|
const postcss = require('rollup-plugin-postcss');
|
|
@@ -57,9 +57,12 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
57
57
|
input: rollupInput,
|
|
58
58
|
plugins: [
|
|
59
59
|
alias({
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
entries: Object.entries(curConfig.webpack.resolve.alias || {}).map(
|
|
61
|
+
([find, replacement]) => ({
|
|
62
|
+
find,
|
|
63
|
+
replacement
|
|
64
|
+
})
|
|
65
|
+
)
|
|
63
66
|
}),
|
|
64
67
|
/**
|
|
65
68
|
* excludeList(在akfun.config.js中配置)
|
|
@@ -67,7 +70,17 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
67
70
|
*/
|
|
68
71
|
externals({
|
|
69
72
|
include: build2esm.excludeList || []
|
|
73
|
+
// exclude: ['./**', '../**'], // 排除所有相对路径模块
|
|
74
|
+
// deps: true, // 只标记 node_modules 中的依赖
|
|
75
|
+
// devDeps: false, // 不标记 devDependencies
|
|
76
|
+
// peerDeps: true, // 标记 peerDependencies
|
|
70
77
|
}),
|
|
78
|
+
/**
|
|
79
|
+
* nodeResolve 插件用于解析模块路径
|
|
80
|
+
* extensions: 默认识别的文件后缀列表
|
|
81
|
+
* 默认值(来自 webpack.resolve.extensions): ['.js', '.jsx', '.ts', '.tsx', '.vue', '.json']
|
|
82
|
+
* 如果不配置此插件,Rollup 默认只识别 .js 和 .mjs 文件
|
|
83
|
+
*/
|
|
71
84
|
nodeResolve({
|
|
72
85
|
extensions: curConfig.webpack.resolve.extensions
|
|
73
86
|
}),
|
|
@@ -82,7 +95,11 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
82
95
|
// jsx( {factory: 'React.createElement'} ),
|
|
83
96
|
buildType === 'ts' ? undefined : jsx({ factory: 'React.createElement' }),
|
|
84
97
|
vue(),
|
|
85
|
-
commonjs(
|
|
98
|
+
commonjs({
|
|
99
|
+
transformMixedEsModules: true, // 转换混合的 ES 模块和 CommonJS 模块
|
|
100
|
+
strictRequires: true, // 严格模式处理 require,确保所有 require 都被正确处理
|
|
101
|
+
ignoreDynamicRequires: true // 忽略动态 require
|
|
102
|
+
}),
|
|
86
103
|
postcss({
|
|
87
104
|
extensions: ['.css', '.scss', '.sass', '.styl', '.stylus', '.less'],
|
|
88
105
|
// Or with custom file name, it will generate file relative to bundle.js in v3
|
|
@@ -55,18 +55,20 @@
|
|
|
55
55
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
|
56
56
|
"@babel/plugin-syntax-import-meta": "^7.10.4",
|
|
57
57
|
"@babel/plugin-transform-runtime": "^7.17.0",
|
|
58
|
-
"@babel/polyfill": "^7.10.1",
|
|
59
58
|
"@babel/preset-env": "^7.16.11",
|
|
60
59
|
"@babel/preset-react": "^7.16.7",
|
|
61
60
|
"@babel/preset-typescript": "^7.16.7",
|
|
62
61
|
"@babel/register": "^7.17.0",
|
|
63
62
|
"@mapbox/stylelint-processor-arbitrary-tags": "^0.4.0",
|
|
64
|
-
"@rollup/plugin-alias": "^
|
|
65
|
-
"@rollup/plugin-babel": "^
|
|
66
|
-
"@rollup/plugin-commonjs": "^
|
|
67
|
-
"@rollup/plugin-image": "^
|
|
68
|
-
"@rollup/plugin-json": "^
|
|
69
|
-
"@rollup/plugin-node-resolve": "^
|
|
63
|
+
"@rollup/plugin-alias": "^6.0.0",
|
|
64
|
+
"@rollup/plugin-babel": "^6.1.0",
|
|
65
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
66
|
+
"@rollup/plugin-image": "^3.0.3",
|
|
67
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
68
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
69
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
70
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
71
|
+
"@svgr/rollup": "^8.1.0",
|
|
70
72
|
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
|
71
73
|
"@typescript-eslint/parser": "^5.10.2",
|
|
72
74
|
"@vue/compiler-sfc": "^3.2.29",
|
|
@@ -77,6 +79,7 @@
|
|
|
77
79
|
"chalk": "^4.0.0",
|
|
78
80
|
"compression-webpack-plugin": "^9.2.0",
|
|
79
81
|
"connect-history-api-fallback": "^1.6.0",
|
|
82
|
+
"core-js": "^3.47.0",
|
|
80
83
|
"copy-webpack-plugin": "^10.2.4",
|
|
81
84
|
"css-loader": "^6.6.0",
|
|
82
85
|
"cssnano": "^5.0.16",
|
|
@@ -104,7 +107,7 @@
|
|
|
104
107
|
"http-proxy-middleware": "^2.0.2",
|
|
105
108
|
"inquirer": "^8.2.0",
|
|
106
109
|
"mini-css-extract-plugin": "^2.5.3",
|
|
107
|
-
"sass": "^1.
|
|
110
|
+
"sass": "^1.94.2",
|
|
108
111
|
"open": "^8.4.0",
|
|
109
112
|
"ora": "^4.0.4",
|
|
110
113
|
"params-replace-loader": "^1.1.6",
|
|
@@ -116,9 +119,10 @@
|
|
|
116
119
|
"progress-bar-webpack-plugin": "^2.1.0",
|
|
117
120
|
"qs": "^6.10.3",
|
|
118
121
|
"rimraf": "^3.0.2",
|
|
119
|
-
"rollup": "^
|
|
122
|
+
"rollup": "^4.53.3",
|
|
123
|
+
"rollup-plugin-jsx": "^1.0.3",
|
|
124
|
+
"rollup-plugin-node-externals": "^8.1.2",
|
|
120
125
|
"rollup-plugin-postcss": "^4.0.2",
|
|
121
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
122
126
|
"rollup-plugin-vue": "^6.0.0",
|
|
123
127
|
"sass-loader": "^12.4.0",
|
|
124
128
|
"sass-resources-loader": "^2.2.4",
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
module.
|
|
1
|
+
module.exports = {
|
|
2
2
|
presets: [
|
|
3
3
|
[
|
|
4
4
|
'@babel/preset-env',
|
|
5
5
|
{
|
|
6
6
|
loose: false,
|
|
7
7
|
modules: false,
|
|
8
|
+
useBuiltIns: 'usage', // 按需引入 polyfill,替代 @babel/polyfill
|
|
9
|
+
corejs: 3, // 使用 core-js 3.x 版本
|
|
8
10
|
targets: {
|
|
9
11
|
browsers: ['> 1%', 'last 2 versions', 'not ie <= 8']
|
|
10
12
|
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配置管理器
|
|
3
|
+
* 负责加载、合并和验证配置
|
|
4
|
+
*/
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const chalk = require('chalk');
|
|
8
|
+
const deepMergeConfig = require('../utils/deepMergeConfig');
|
|
9
|
+
|
|
10
|
+
class ConfigManager {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.defaultConfig = require('../config/default.config');
|
|
13
|
+
this.userConfig = null;
|
|
14
|
+
this.mergedConfig = null;
|
|
15
|
+
this.configPath = null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 加载用户配置文件
|
|
20
|
+
* @param {string} configPath - 配置文件路径(相对于项目根目录)
|
|
21
|
+
* @returns {Object|null} 用户配置对象,如果不存在则返回 null
|
|
22
|
+
*/
|
|
23
|
+
loadUserConfig(configPath) {
|
|
24
|
+
try {
|
|
25
|
+
// 支持相对路径和绝对路径
|
|
26
|
+
const resolvedPath = path.isAbsolute(configPath)
|
|
27
|
+
? configPath
|
|
28
|
+
: path.resolve(process.cwd(), configPath);
|
|
29
|
+
|
|
30
|
+
this.configPath = resolvedPath;
|
|
31
|
+
|
|
32
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
33
|
+
console.log(chalk.yellow(`\n⚠️ 未找到用户配置文件: ${configPath}\n 使用默认配置运行\n`));
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 清除 require 缓存,确保获取最新配置
|
|
38
|
+
delete require.cache[resolvedPath];
|
|
39
|
+
this.userConfig = require(resolvedPath);
|
|
40
|
+
|
|
41
|
+
console.log(chalk.green(`✓ 已加载配置文件: ${configPath}`));
|
|
42
|
+
return this.userConfig;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error(chalk.red(`\n❌ 加载配置文件失败: ${configPath}\n`));
|
|
45
|
+
console.error(chalk.red(` 错误信息: ${error.message}\n`));
|
|
46
|
+
|
|
47
|
+
if (error.stack && process.env.AKFUN_DEBUG) {
|
|
48
|
+
console.error(chalk.gray(error.stack));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
console.log(chalk.yellow(' 将使用默认配置继续运行\n'));
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 自动查找并加载配置文件
|
|
58
|
+
* 按优先级查找:akfun.config.js -> akfun.config.json
|
|
59
|
+
* @returns {Object|null}
|
|
60
|
+
*/
|
|
61
|
+
autoLoadConfig() {
|
|
62
|
+
const configFiles = ['akfun.config.js', 'akfun.config.json', '.akfunrc.js', '.akfunrc.json'];
|
|
63
|
+
|
|
64
|
+
for (const configFile of configFiles) {
|
|
65
|
+
const configPath = path.resolve(process.cwd(), configFile);
|
|
66
|
+
if (fs.existsSync(configPath)) {
|
|
67
|
+
return this.loadUserConfig(configPath);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
console.log(chalk.gray(' 未找到配置文件,使用默认配置'));
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 合并配置
|
|
77
|
+
* @param {Object} userConfig - 用户配置(可选)
|
|
78
|
+
* @returns {Object} 合并后的配置
|
|
79
|
+
*/
|
|
80
|
+
mergeConfig(userConfig) {
|
|
81
|
+
const configToMerge = userConfig || this.userConfig || {};
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
this.mergedConfig = deepMergeConfig(this.defaultConfig, configToMerge);
|
|
85
|
+
|
|
86
|
+
// 输出配置摘要
|
|
87
|
+
if (process.env.AKFUN_DEBUG) {
|
|
88
|
+
this.printConfigSummary();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return this.mergedConfig;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error(chalk.red('\n❌ 配置合并失败:'));
|
|
94
|
+
console.error(chalk.red(` ${error.message}\n`));
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 获取配置
|
|
101
|
+
* @param {string} key - 配置键路径(支持点号分隔,如 'webpack.resolve.alias')
|
|
102
|
+
* @returns {any} 配置值
|
|
103
|
+
*/
|
|
104
|
+
getConfig(key) {
|
|
105
|
+
if (!this.mergedConfig) {
|
|
106
|
+
this.mergeConfig();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!key) {
|
|
110
|
+
return this.mergedConfig;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 支持点号分隔的键路径
|
|
114
|
+
const keys = key.split('.');
|
|
115
|
+
let result = this.mergedConfig;
|
|
116
|
+
|
|
117
|
+
for (const k of keys) {
|
|
118
|
+
if (result && typeof result === 'object' && k in result) {
|
|
119
|
+
result = result[k];
|
|
120
|
+
} else {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 设置配置值(仅在内存中)
|
|
130
|
+
* @param {string} key - 配置键路径
|
|
131
|
+
* @param {any} value - 配置值
|
|
132
|
+
*/
|
|
133
|
+
setConfig(key, value) {
|
|
134
|
+
if (!this.mergedConfig) {
|
|
135
|
+
this.mergeConfig();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const keys = key.split('.');
|
|
139
|
+
let target = this.mergedConfig;
|
|
140
|
+
|
|
141
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
142
|
+
const k = keys[i];
|
|
143
|
+
if (!(k in target) || typeof target[k] !== 'object') {
|
|
144
|
+
target[k] = {};
|
|
145
|
+
}
|
|
146
|
+
target = target[k];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
target[keys[keys.length - 1]] = value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 验证配置
|
|
154
|
+
* @returns {Object} 验证后的配置
|
|
155
|
+
*/
|
|
156
|
+
validateConfig() {
|
|
157
|
+
if (!this.mergedConfig) {
|
|
158
|
+
this.mergeConfig();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
// 基础验证
|
|
163
|
+
this._validateBasicConfig();
|
|
164
|
+
|
|
165
|
+
// 如果有配置验证器,使用它
|
|
166
|
+
try {
|
|
167
|
+
const { validateConfig } = require('../utils/configValidator');
|
|
168
|
+
return validateConfig(this.mergedConfig);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
// 如果配置验证器不存在或出错,只进行基础验证
|
|
171
|
+
if (process.env.AKFUN_DEBUG) {
|
|
172
|
+
console.log(chalk.gray(' 使用基础配置验证'));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return this.mergedConfig;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error(chalk.red('\n❌ 配置验证失败:'));
|
|
179
|
+
console.error(chalk.red(` ${error.message}\n`));
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 基础配置验证
|
|
186
|
+
* @private
|
|
187
|
+
*/
|
|
188
|
+
_validateBasicConfig() {
|
|
189
|
+
const config = this.mergedConfig;
|
|
190
|
+
|
|
191
|
+
// 验证必需的配置项
|
|
192
|
+
if (config.dev && typeof config.dev !== 'object') {
|
|
193
|
+
throw new Error('dev 配置格式错误');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (config.build && typeof config.build !== 'object') {
|
|
197
|
+
throw new Error('build 配置格式错误');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (config.webpack && typeof config.webpack !== 'object') {
|
|
201
|
+
throw new Error('webpack 配置格式错误');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// 验证端口号
|
|
205
|
+
if (config.dev && config.dev.port) {
|
|
206
|
+
const port = parseInt(config.dev.port, 10);
|
|
207
|
+
if (isNaN(port) || port < 1 || port > 65535) {
|
|
208
|
+
throw new Error('dev.port 必须是 1-65535 之间的数字');
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 验证路径
|
|
213
|
+
if (config.build && config.build.assetsRoot) {
|
|
214
|
+
const assetsRoot = config.build.assetsRoot;
|
|
215
|
+
if (typeof assetsRoot !== 'string') {
|
|
216
|
+
throw new Error('build.assetsRoot 必须是字符串类型的路径');
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 打印配置摘要
|
|
223
|
+
* @private
|
|
224
|
+
*/
|
|
225
|
+
printConfigSummary() {
|
|
226
|
+
console.log(chalk.cyan('\n📋 配置摘要:'));
|
|
227
|
+
|
|
228
|
+
const config = this.mergedConfig;
|
|
229
|
+
|
|
230
|
+
if (config.dev) {
|
|
231
|
+
console.log(chalk.gray(' 开发环境:'));
|
|
232
|
+
console.log(chalk.gray(` - 端口: ${config.dev.port}`));
|
|
233
|
+
console.log(chalk.gray(` - 自动打开浏览器: ${config.dev.autoOpenBrowser}`));
|
|
234
|
+
console.log(
|
|
235
|
+
chalk.gray(` - 代理配置: ${Object.keys(config.dev.proxyTable || {}).length} 个`)
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (config.build) {
|
|
240
|
+
console.log(chalk.gray(' 生产构建:'));
|
|
241
|
+
console.log(chalk.gray(` - 输出目录: ${config.build.assetsRoot}`));
|
|
242
|
+
console.log(chalk.gray(` - SourceMap: ${config.build.productionSourceMap}`));
|
|
243
|
+
console.log(chalk.gray(` - Gzip 压缩: ${config.build.productionGzip}`));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (config.webpack && config.webpack.resolve) {
|
|
247
|
+
console.log(chalk.gray(' Webpack 配置:'));
|
|
248
|
+
console.log(chalk.gray(` - 解析扩展名: ${config.webpack.resolve.extensions.length} 个`));
|
|
249
|
+
console.log(
|
|
250
|
+
chalk.gray(` - 别名配置: ${Object.keys(config.webpack.resolve.alias || {}).length} 个`)
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (config.settings) {
|
|
255
|
+
console.log(chalk.gray(' 代码检查:'));
|
|
256
|
+
console.log(chalk.gray(` - ESLint: ${config.settings.enableESLint}`));
|
|
257
|
+
console.log(chalk.gray(` - StyleLint: ${config.settings.enableStyleLint}`));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
console.log('');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 重置配置管理器
|
|
265
|
+
*/
|
|
266
|
+
reset() {
|
|
267
|
+
this.userConfig = null;
|
|
268
|
+
this.mergedConfig = null;
|
|
269
|
+
this.configPath = null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 获取默认配置
|
|
274
|
+
* @returns {Object}
|
|
275
|
+
*/
|
|
276
|
+
getDefaultConfig() {
|
|
277
|
+
return this.defaultConfig;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 获取用户配置
|
|
282
|
+
* @returns {Object|null}
|
|
283
|
+
*/
|
|
284
|
+
getUserConfig() {
|
|
285
|
+
return this.userConfig;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* 导出配置到文件(用于调试)
|
|
290
|
+
* @param {string} outputPath - 输出路径
|
|
291
|
+
*/
|
|
292
|
+
exportConfig(outputPath) {
|
|
293
|
+
if (!this.mergedConfig) {
|
|
294
|
+
this.mergeConfig();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const fs = require('fs');
|
|
298
|
+
const output = JSON.stringify(this.mergedConfig, null, 2);
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
fs.writeFileSync(outputPath, output, 'utf-8');
|
|
302
|
+
console.log(chalk.green(`✓ 配置已导出到: ${outputPath}`));
|
|
303
|
+
} catch (error) {
|
|
304
|
+
console.error(chalk.red(`❌ 配置导出失败: ${error.message}`));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// 导出单例实例
|
|
310
|
+
const configManager = new ConfigManager();
|
|
311
|
+
|
|
312
|
+
// 也导出类,方便测试
|
|
313
|
+
configManager.ConfigManager = ConfigManager;
|
|
314
|
+
|
|
315
|
+
module.exports = configManager;
|