akfun 5.1.21 → 5.2.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/module/index.js +18 -10
- package/module/main.js +2 -0
- package/package.json +1 -1
- package/src/build2esm.js +25 -67
- package/src/build2node.js +32 -0
- package/src/rollup/build.js +83 -0
- package/src/{config → rollup}/rollup.config.js +36 -22
- package/src/rollup/rollup.node.config.js +87 -0
- package/src/webpack/webpack.base.conf.js +1 -1
package/module/index.js
CHANGED
|
@@ -142,33 +142,41 @@ let argv = yargs
|
|
|
142
142
|
)
|
|
143
143
|
.command(
|
|
144
144
|
'build2lib',
|
|
145
|
-
'构建
|
|
145
|
+
'构建 Library 库(UMD 模块)',
|
|
146
146
|
(yargs) => {
|
|
147
147
|
yargs
|
|
148
148
|
.reset()
|
|
149
149
|
.usage(titleTip('Usage') + ': $0 build2lib')
|
|
150
150
|
.alias('h', 'help');
|
|
151
151
|
},
|
|
152
|
-
(
|
|
152
|
+
() => {
|
|
153
153
|
mainAction.build('lib'); // 构建library
|
|
154
154
|
},
|
|
155
155
|
)
|
|
156
156
|
.command(
|
|
157
157
|
'build2esm',
|
|
158
|
-
'构建
|
|
158
|
+
'构建 ESM 模块',
|
|
159
159
|
(yargs) => {
|
|
160
160
|
yargs
|
|
161
161
|
.reset()
|
|
162
162
|
.usage(titleTip('Usage') + ': $0 build2esm')
|
|
163
|
-
.option('fileName', {
|
|
164
|
-
alias: 'n',
|
|
165
|
-
describe: '输出的文件名',
|
|
166
|
-
default: '',
|
|
167
|
-
})
|
|
168
163
|
.alias('h', 'help');
|
|
169
164
|
},
|
|
170
|
-
(
|
|
171
|
-
mainAction.build2esm(
|
|
165
|
+
() => {
|
|
166
|
+
mainAction.build2esm();
|
|
167
|
+
},
|
|
168
|
+
)
|
|
169
|
+
.command(
|
|
170
|
+
'build2node',
|
|
171
|
+
'构建 Node 模块',
|
|
172
|
+
(yargs) => {
|
|
173
|
+
yargs
|
|
174
|
+
.reset()
|
|
175
|
+
.usage(titleTip('Usage') + ': $0 build2node')
|
|
176
|
+
.alias('h', 'help');
|
|
177
|
+
},
|
|
178
|
+
() => {
|
|
179
|
+
mainAction.build2node();
|
|
172
180
|
},
|
|
173
181
|
)
|
|
174
182
|
.command(
|
package/module/main.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const buildAction = require('../src/build.js'); // 构建脚本:生产环境
|
|
3
3
|
const devAction = require('../src/dev-server.js'); // 构建脚本:开发环境
|
|
4
4
|
const build2esm = require('../src/build2esm.js'); // 构建esm输出模块
|
|
5
|
+
const build2node = require('../src/build2node.js'); // 构建esm输出模块
|
|
5
6
|
const inspect = require('./inspect.js');
|
|
6
7
|
const gitClone = require('../src/utils/gitClone.js');
|
|
7
8
|
const createFile = require('../src/utils/createFile');
|
|
@@ -21,6 +22,7 @@ module.exports = {
|
|
|
21
22
|
dev: devAction,
|
|
22
23
|
build: buildAction,
|
|
23
24
|
build2esm,
|
|
25
|
+
build2node,
|
|
24
26
|
inspect,
|
|
25
27
|
|
|
26
28
|
// 工具方法
|
package/package.json
CHANGED
package/src/build2esm.js
CHANGED
|
@@ -1,80 +1,38 @@
|
|
|
1
|
-
const ora = require('ora');
|
|
2
|
-
const rollup = require('rollup');
|
|
3
|
-
const terser = require('@rollup/plugin-terser'); // 压缩
|
|
4
1
|
const getProjectConfig = require('./config/index'); // 用于获取当前项目配置文件
|
|
5
|
-
const rollupConfig = require('./config/rollup.config'); // rollup的配置文件
|
|
6
|
-
const { isArray, isObject } = require('./utils/typeof');
|
|
7
2
|
const { curConsoleTag } = require('./utils/akfunParams');
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
3
|
+
const rollupBuild = require('./rollup/build');
|
|
4
|
+
const rollupConfig = require('./rollup/rollup.config'); // rollup的配置文件
|
|
5
|
+
/**
|
|
6
|
+
* 用于构建 esm 模块
|
|
7
|
+
* @param {*} akfunConfig akfun 配置文件
|
|
8
|
+
* @param {*} _consoleTag 控制台输出标签
|
|
9
|
+
*
|
|
10
|
+
* * 支持的配置项目(akfunConfig.build2esm)
|
|
11
|
+
* - type: 项目构建类型(ts、js)
|
|
12
|
+
* - format: 构建格式(esm、cjs)
|
|
13
|
+
* - input: 构建入口文件
|
|
14
|
+
* - fileName: 构建输出文件名
|
|
15
|
+
* - outDir: 构建输出目录
|
|
16
|
+
* - excludeList: 构建排除列表
|
|
17
|
+
* - declaration: 是否生成声明文件
|
|
18
|
+
* - declarationDir: 声明文件输出目录
|
|
19
|
+
* - svgDir: svg文件输出目录
|
|
20
|
+
* - plugins: 自定义rollup插件
|
|
21
|
+
* - babelPlugins: 自定义babel插件
|
|
22
|
+
*/
|
|
28
23
|
|
|
29
24
|
// 构建脚本:一般用于构建生产环境的代码
|
|
30
|
-
module.exports = function (
|
|
25
|
+
module.exports = function (akfunConfig, _consoleTag) {
|
|
31
26
|
const consoleTag = _consoleTag || curConsoleTag;
|
|
32
27
|
// 获取项目配置文件
|
|
33
28
|
let config = getProjectConfig(akfunConfig);
|
|
34
|
-
|
|
35
|
-
const spinner = ora(`${consoleTag}开启esm模块构建能力...`).start();
|
|
36
|
-
const curRollupConfig = rollupConfig(fileName, config); // 默认配置
|
|
37
29
|
const build2esm = config.build2esm; // 用户的项目配置
|
|
38
|
-
const curWebpackConfig = config.webpack;
|
|
39
|
-
const compress = build2esm.compress ?? true; // 是否压缩代码
|
|
40
|
-
if (build2esm && build2esm.input) {
|
|
41
|
-
curRollupConfig.input = build2esm.input;
|
|
42
|
-
}
|
|
43
30
|
|
|
44
|
-
//
|
|
45
|
-
let externals = []; // rollup 的 externals 是数组格式
|
|
31
|
+
const curRollupConfig = rollupConfig(config, build2esm); // 默认配置
|
|
46
32
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
externals = externals.concat(webpackExternal);
|
|
50
|
-
} else if (webpackExternal && isObject(webpackExternal)) {
|
|
51
|
-
externals = externals.concat(Object.keys(webpackExternal));
|
|
33
|
+
if (build2esm && !build2esm.format) {
|
|
34
|
+
build2esm.format = 'esm'; // 默认构建格式为 esm
|
|
52
35
|
}
|
|
53
36
|
|
|
54
|
-
|
|
55
|
-
if (build2esmExternal && isArray(build2esmExternal)) {
|
|
56
|
-
externals = externals.concat(build2esmExternal);
|
|
57
|
-
} else if (build2esmExternal && isObject(build2esmExternal)) {
|
|
58
|
-
externals = externals.concat(Object.keys(build2esmExternal));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 添加到 rollup 配置中(rollup 使用 external 而不是 externals)
|
|
62
|
-
curRollupConfig.external = externals;
|
|
63
|
-
|
|
64
|
-
if (build2esm && build2esm.output) {
|
|
65
|
-
curRollupConfig.output = build2esm.output;
|
|
66
|
-
|
|
67
|
-
if (isArray(build2esm.output)) {
|
|
68
|
-
build2esm.output.map((outputItem) => {
|
|
69
|
-
if (!outputItem.plugins && compress) {
|
|
70
|
-
outputItem.plugins = [terser()];
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
} else if (isObject(build2esm.output) && !build2esm.output.plugins && compress) {
|
|
74
|
-
build2esm.output.plugins = [terser()];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
build2esmFunc(curRollupConfig, config).then(() => {
|
|
78
|
-
spinner.succeed(`${consoleTag}esm模块构建完成。`);
|
|
79
|
-
});
|
|
37
|
+
rollupBuild(config, build2esm, curRollupConfig, consoleTag);
|
|
80
38
|
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const getProjectConfig = require('./config/index'); // 用于获取当前项目配置文件
|
|
2
|
+
const { curConsoleTag } = require('./utils/akfunParams');
|
|
3
|
+
const rollupBuild = require('./rollup/build');
|
|
4
|
+
const rollupConfig = require('./rollup/rollup.node.config'); // rollup的配置文件
|
|
5
|
+
/**
|
|
6
|
+
* 用于构建 esm 模块
|
|
7
|
+
* @param {*} akfunConfig akfun 配置文件
|
|
8
|
+
* @param {*} _consoleTag 控制台输出标签
|
|
9
|
+
*
|
|
10
|
+
* 支持的配置项目(akfunConfig.build2node)
|
|
11
|
+
* - type: 项目构建类型(ts、js)
|
|
12
|
+
* - input: 构建入口文件
|
|
13
|
+
* - fileName: 构建输出文件名
|
|
14
|
+
* - outDir: 构建输出目录
|
|
15
|
+
* - excludeList: 构建排除列表
|
|
16
|
+
* - declaration: 是否生成声明文件
|
|
17
|
+
* - declarationDir: 声明文件输出目录
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// 构建脚本:一般用于构建生产环境的代码
|
|
21
|
+
module.exports = function (akfunConfig, _consoleTag) {
|
|
22
|
+
const consoleTag = _consoleTag || curConsoleTag;
|
|
23
|
+
// 获取项目配置文件
|
|
24
|
+
let config = getProjectConfig(akfunConfig);
|
|
25
|
+
const build2node = config.build2node; // 用户的项目配置
|
|
26
|
+
|
|
27
|
+
const curRollupConfig = rollupConfig(config, build2node); // 默认配置
|
|
28
|
+
|
|
29
|
+
build2node.format = 'cjs'; // 构建格式固定为 cjs,无需用户配置
|
|
30
|
+
|
|
31
|
+
rollupBuild(config, build2node, curRollupConfig, consoleTag);
|
|
32
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const ora = require('ora');
|
|
2
|
+
const rollup = require('rollup');
|
|
3
|
+
const terser = require('@rollup/plugin-terser'); // 压缩
|
|
4
|
+
const { isArray, isObject } = require('../utils/typeof');
|
|
5
|
+
const { curConsoleTag } = require('../utils/akfunParams');
|
|
6
|
+
|
|
7
|
+
async function rollupBuildFunc(envConfig, curConfig) {
|
|
8
|
+
// create a bundle
|
|
9
|
+
const bundle = await rollup.rollup({
|
|
10
|
+
input: envConfig.input,
|
|
11
|
+
external: envConfig.external || envConfig.externals, // 兼容新旧两种写法
|
|
12
|
+
plugins: envConfig.plugins
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
if (isArray(envConfig.output)) {
|
|
16
|
+
// 等待所有输出完成
|
|
17
|
+
await Promise.all(envConfig.output.map((outputItem) => bundle.write(outputItem)));
|
|
18
|
+
} else if (isObject(envConfig.output)) {
|
|
19
|
+
// or write the bundle to disk
|
|
20
|
+
await bundle.write(envConfig.output);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 关闭 bundle 以释放资源
|
|
24
|
+
await bundle.close();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 用于构建 node 模块
|
|
29
|
+
* @param {*} config: akfun 配置文件
|
|
30
|
+
* @param {*} envConfig: 当前环境配置
|
|
31
|
+
* @param {*} _rollupConfig: 自定义 rollup 配置
|
|
32
|
+
* @param {*} _consoleTag 控制台输出标签
|
|
33
|
+
*/
|
|
34
|
+
module.exports = function (config, envConfig, _rollupConfig, _consoleTag) {
|
|
35
|
+
const consoleTag = _consoleTag || curConsoleTag;
|
|
36
|
+
const curRollupConfig = _rollupConfig;
|
|
37
|
+
const curWebpackConfig = config.webpack;
|
|
38
|
+
const buildFormat = envConfig.format || 'esm';
|
|
39
|
+
const compress = envConfig.compress ?? true; // 是否压缩代码
|
|
40
|
+
if (envConfig && envConfig.input) {
|
|
41
|
+
curRollupConfig.input = envConfig.input;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 处理 externals,用户手动添加要剔除的依赖
|
|
45
|
+
let externals = []; // rollup 的 externals 是数组格式
|
|
46
|
+
|
|
47
|
+
const webpackExternals = curWebpackConfig.externals || curWebpackConfig.external;
|
|
48
|
+
if (webpackExternals && isArray(webpackExternals)) {
|
|
49
|
+
externals = externals.concat(webpackExternals);
|
|
50
|
+
} else if (webpackExternals && isObject(webpackExternals)) {
|
|
51
|
+
externals = externals.concat(Object.keys(webpackExternals));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const curExternal = envConfig.external;
|
|
55
|
+
if (curExternal && isArray(curExternal)) {
|
|
56
|
+
externals = externals.concat(curExternal);
|
|
57
|
+
} else if (curExternal && isObject(curExternal)) {
|
|
58
|
+
externals = externals.concat(Object.keys(curExternal));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 添加到 rollup 配置中(rollup 使用 external 而不是 externals)
|
|
62
|
+
curRollupConfig.external = externals;
|
|
63
|
+
|
|
64
|
+
if (envConfig && envConfig.output) {
|
|
65
|
+
curRollupConfig.output = envConfig.output;
|
|
66
|
+
|
|
67
|
+
if (compress &&isArray(envConfig.output)) {
|
|
68
|
+
envConfig.output.map((outputItem) => {
|
|
69
|
+
if (!outputItem.plugins) {
|
|
70
|
+
outputItem.plugins = [terser()];
|
|
71
|
+
} else {
|
|
72
|
+
outputItem.plugins.push(terser());
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
} else if (compress && isObject(envConfig.output) && !envConfig.output.plugins) {
|
|
76
|
+
envConfig.output.plugins = [terser()];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const spinner = ora(`${consoleTag}开启${buildFormat}模块构建模式...`).start();
|
|
80
|
+
rollupBuildFunc(curRollupConfig, config).then(() => {
|
|
81
|
+
spinner.succeed(`${consoleTag}${buildFormat}模块构建完成。`);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
@@ -23,24 +23,40 @@ const cssnano = require('cssnano');
|
|
|
23
23
|
const svgr = require('@svgr/rollup');
|
|
24
24
|
const { nodeExternals } = require('rollup-plugin-node-externals');
|
|
25
25
|
const { resolveToCurrentRoot, resolveToCurrentDist } = require('../utils/pathUtils'); // 统一路径解析
|
|
26
|
-
const babelConfig = require('
|
|
27
|
-
const projectConfig = require('./index'); // 引入当前项目配置文件
|
|
26
|
+
const babelConfig = require('../config/babel.config'); // Babel的配置文件
|
|
28
27
|
const { buildBanner } = require('../utils/akfunParams');
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
/**
|
|
30
|
+
* 用于生成 rollup 配置
|
|
31
|
+
* @param {*} curConfig 当前项目配置
|
|
32
|
+
* @param {*} curEnvConfig 当前环境配置
|
|
33
|
+
*
|
|
34
|
+
* 支持的配置项目(curEnvConfig)
|
|
35
|
+
* - type: 项目构建类型(ts、js)
|
|
36
|
+
* - format: 构建格式(esm、cjs)
|
|
37
|
+
* - input: 构建入口文件
|
|
38
|
+
* - fileName: 构建输出文件名
|
|
39
|
+
* - outDir: 构建输出目录
|
|
40
|
+
* - excludeList: 构建排除列表
|
|
41
|
+
* - declaration: 是否生成声明文件
|
|
42
|
+
* - declarationDir: 声明文件输出目录
|
|
43
|
+
* - svgDir: svg文件输出目录
|
|
44
|
+
* - plugins: 自定义rollup插件
|
|
45
|
+
* - babelPlugins: 自定义babel插件
|
|
46
|
+
*/
|
|
47
|
+
module.exports = function (curConfig, curEnvConfig) {
|
|
33
48
|
const curWebpackConfig = curConfig.webpack || {};
|
|
34
|
-
const buildType =
|
|
49
|
+
const buildType = curEnvConfig.type || 'ts';
|
|
50
|
+
const buildFormat = curEnvConfig.format || 'esm';
|
|
35
51
|
// 获取用户配置的构建入口文件
|
|
36
52
|
let rollupInput = resolveToCurrentRoot('src/main.js');
|
|
37
|
-
if (
|
|
38
|
-
rollupInput =
|
|
53
|
+
if (curEnvConfig.input) {
|
|
54
|
+
rollupInput = curEnvConfig.input;
|
|
39
55
|
}
|
|
40
|
-
let curFileName =
|
|
56
|
+
let curFileName = 'index'; // 默认以"index.esm.js"输出
|
|
41
57
|
// 获取用户配置的构建输出文件名
|
|
42
|
-
if (
|
|
43
|
-
curFileName =
|
|
58
|
+
if (curEnvConfig.fileName) {
|
|
59
|
+
curFileName = curEnvConfig.fileName;
|
|
44
60
|
}
|
|
45
61
|
// 增加babel配置
|
|
46
62
|
babelConfig.babelHelpers = 'runtime';
|
|
@@ -53,7 +69,6 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
53
69
|
|
|
54
70
|
return {
|
|
55
71
|
banner: buildBanner,
|
|
56
|
-
// format: build2esm.format || 'esm', // 生成包的格式
|
|
57
72
|
input: rollupInput,
|
|
58
73
|
plugins: [
|
|
59
74
|
alias({
|
|
@@ -69,7 +84,7 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
69
84
|
* 设置打包中应该排除的依赖
|
|
70
85
|
*/
|
|
71
86
|
nodeExternals({
|
|
72
|
-
include:
|
|
87
|
+
include: curEnvConfig.excludeList || []
|
|
73
88
|
// exclude: ['./**', '../**'], // 排除所有相对路径模块
|
|
74
89
|
// deps: true, // 只标记 node_modules 中的依赖
|
|
75
90
|
// devDeps: false, // 不标记 devDependencies
|
|
@@ -87,12 +102,11 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
87
102
|
buildType === 'ts'
|
|
88
103
|
? typescript({
|
|
89
104
|
// 是否生成声明文件(默认 false)
|
|
90
|
-
declaration:
|
|
91
|
-
declarationDir:
|
|
105
|
+
declaration: curEnvConfig.declaration !== undefined ? curEnvConfig.declaration : false,
|
|
106
|
+
declarationDir: curEnvConfig.declarationDir || './dist/types'
|
|
92
107
|
})
|
|
93
108
|
: undefined,
|
|
94
109
|
babel(babelConfig), // 备注,需要先babel()再commjs()
|
|
95
|
-
// jsx( {factory: 'React.createElement'} ),
|
|
96
110
|
buildType === 'ts' ? undefined : jsx({ factory: 'React.createElement' }),
|
|
97
111
|
vue(),
|
|
98
112
|
commonjs({
|
|
@@ -103,7 +117,7 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
103
117
|
postcss({
|
|
104
118
|
extensions: ['.css', '.scss', '.sass', '.styl', '.stylus', '.less'],
|
|
105
119
|
// Or with custom file name, it will generate file relative to bundle.js in v3
|
|
106
|
-
extract: resolveToCurrentDist(`${curFileName}.css`,
|
|
120
|
+
extract: resolveToCurrentDist(`${curFileName}.css`, curEnvConfig.outDir),
|
|
107
121
|
plugins: [
|
|
108
122
|
simplevars(),
|
|
109
123
|
nested(),
|
|
@@ -121,18 +135,18 @@ module.exports = function (fileName, akfunConfig) {
|
|
|
121
135
|
dimensions: false
|
|
122
136
|
}),
|
|
123
137
|
image({
|
|
124
|
-
exclude: [
|
|
138
|
+
exclude: [curEnvConfig.svgDir || 'src/icons/**']
|
|
125
139
|
}),
|
|
126
140
|
json()
|
|
127
141
|
],
|
|
128
142
|
output: [
|
|
129
143
|
{
|
|
130
|
-
file: resolveToCurrentDist(`${curFileName}.
|
|
131
|
-
format:
|
|
144
|
+
file: resolveToCurrentDist(`${curFileName}.${buildFormat}.js`, curEnvConfig.outDir),
|
|
145
|
+
format: buildFormat
|
|
132
146
|
},
|
|
133
147
|
{
|
|
134
|
-
file: resolveToCurrentDist(`${curFileName}.
|
|
135
|
-
format:
|
|
148
|
+
file: resolveToCurrentDist(`${curFileName}.${buildFormat}.min.js`, curEnvConfig.outDir),
|
|
149
|
+
format: buildFormat,
|
|
136
150
|
plugins: [terser()]
|
|
137
151
|
}
|
|
138
152
|
]
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// rollup.config.js
|
|
2
|
+
const { nodeResolve } = require('@rollup/plugin-node-resolve'); // 支持node中的文件导入
|
|
3
|
+
const typescript = require('@rollup/plugin-typescript'); // 支持ts
|
|
4
|
+
const commonjs = require('@rollup/plugin-commonjs'); // 识别cmd模块
|
|
5
|
+
const json = require('@rollup/plugin-json'); // 识别json类型文件
|
|
6
|
+
const alias = require('@rollup/plugin-alias'); // 简写配置
|
|
7
|
+
const { nodeExternals } = require('rollup-plugin-node-externals');
|
|
8
|
+
const { resolveToCurrentRoot, resolveToCurrentDist } = require('../utils/pathUtils'); // 统一路径解析
|
|
9
|
+
const { buildBanner } = require('../utils/akfunParams');
|
|
10
|
+
const { resolve } = require('../utils/pathUtils'); // 统一路径解析
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 用于生成构建 node 模块的 rollup 配置
|
|
14
|
+
* 备注:node 模块构建不需要使用 babel,也不需要处理css、图片、svg 等文件。
|
|
15
|
+
* @param {*} curConfig 当前项目配置
|
|
16
|
+
* @param {*} curEnvConfig 当前环境配置
|
|
17
|
+
*
|
|
18
|
+
* 支持的配置项目(curEnvConfig)
|
|
19
|
+
* - type: 项目构建类型(ts、js)
|
|
20
|
+
* - format: 构建格式(esm、cjs)
|
|
21
|
+
* - input: 构建入口文件
|
|
22
|
+
* - fileName: 构建输出文件名
|
|
23
|
+
* - outDir: 构建输出目录
|
|
24
|
+
* - excludeList: 构建排除列表
|
|
25
|
+
* - declaration: 是否生成声明文件
|
|
26
|
+
* - declarationDir: 声明文件输出目录
|
|
27
|
+
*/
|
|
28
|
+
module.exports = function (curConfig, curEnvConfig) {
|
|
29
|
+
const buildFormat = curEnvConfig.format || 'cjs';
|
|
30
|
+
const buildType = curEnvConfig.type || 'ts';
|
|
31
|
+
const curWebpackConfig = curConfig.webpack || {};
|
|
32
|
+
|
|
33
|
+
let rollupInput = resolveToCurrentRoot('src/main.js'); // 默认入口文件为 src/main.js
|
|
34
|
+
if (curEnvConfig.input) {
|
|
35
|
+
rollupInput = curEnvConfig.input;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let curFileName = 'index'; // 默认以"index.esm.js"输出
|
|
39
|
+
// 获取用户配置的构建输出文件名
|
|
40
|
+
if (curEnvConfig.fileName) {
|
|
41
|
+
curFileName = curEnvConfig.fileName;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
banner: buildBanner,
|
|
46
|
+
input: rollupInput,
|
|
47
|
+
plugins: [
|
|
48
|
+
alias({
|
|
49
|
+
entries: Object.entries(curWebpackConfig.resolve.alias || {}).map(
|
|
50
|
+
([find, replacement]) => ({
|
|
51
|
+
find,
|
|
52
|
+
replacement
|
|
53
|
+
})
|
|
54
|
+
)
|
|
55
|
+
}),
|
|
56
|
+
nodeExternals({
|
|
57
|
+
include: curEnvConfig.excludeList || []
|
|
58
|
+
}),
|
|
59
|
+
nodeResolve({
|
|
60
|
+
extensions: curWebpackConfig.resolve.extensions
|
|
61
|
+
}),
|
|
62
|
+
buildType === 'ts'
|
|
63
|
+
? typescript({
|
|
64
|
+
// 是否生成声明文件(默认 false)
|
|
65
|
+
declaration: curEnvConfig.declaration !== undefined ? curEnvConfig.declaration : false,
|
|
66
|
+
declarationDir: curEnvConfig.declarationDir || './dist/types'
|
|
67
|
+
})
|
|
68
|
+
: undefined,
|
|
69
|
+
commonjs({
|
|
70
|
+
transformMixedEsModules: true, // 转换混合的 ES 模块和 CommonJS 模块
|
|
71
|
+
strictRequires: true, // 严格模式处理 require,确保所有 require 都被正确处理
|
|
72
|
+
ignoreDynamicRequires: true // 忽略动态 require
|
|
73
|
+
}),
|
|
74
|
+
json()
|
|
75
|
+
],
|
|
76
|
+
output: {
|
|
77
|
+
dir: resolve('dist'),
|
|
78
|
+
format: buildFormat, // which can be one of "amd", "cjs", "system", "es", "iife" or "umd".
|
|
79
|
+
preserveModules: true, // 关键:保留原始模块结构,不合并文件
|
|
80
|
+
preserveModulesRoot: 'src', // 指定模块根目录
|
|
81
|
+
exports: 'auto', // 自动处理导出(适配 ES 模块的默认导出/命名导出)
|
|
82
|
+
generatedCode: 'es2015', // 使用现代 JavaScript 语法生成代码
|
|
83
|
+
entryFileNames: '[name].js',
|
|
84
|
+
// interop: 'auto' // 自动处理 CJS/ES 模块互操作(如 __esModule 标记)
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
@@ -250,7 +250,7 @@ module.exports = (_curEnvConfig, _akfunConfig, buildMode = 'build') => {
|
|
|
250
250
|
allowList = allowList.concat(curEnvConfig.allowList);
|
|
251
251
|
}
|
|
252
252
|
// 用户手动添加要剔除的依赖
|
|
253
|
-
let externals = curWebpackConfig.external || {};
|
|
253
|
+
let externals = curWebpackConfig.externals || curWebpackConfig.external || {};
|
|
254
254
|
if (curEnvConfig.externals && isObject(curEnvConfig.externals)) {
|
|
255
255
|
externals = Object.assign(externals, curEnvConfig.externals);
|
|
256
256
|
}
|