wb-lib-tool 0.0.1-beta.3 → 0.0.1-beta.5
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 +17 -14
- package/bin/dev.js +3 -2
- package/bin/index.js +0 -15
- package/bin/lib.js +7 -7
- package/config/webpack.base.conf.js +2 -1
- package/config/webpack.lib.conf.js +3 -2
- package/package.json +12 -1
- package/utils/file.js +3 -14
- package/utils/{getTsContent.js → getFileContent.js} +3 -3
- package/utils/util.js +1 -1
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ wb-lib dev
|
|
|
36
36
|
| 属性 | 类型 | 说明 |
|
|
37
37
|
| ---- | --------------- | ---- |
|
|
38
38
|
**format** | Object|Array|string | 生成格式配置【可配置类型 ['module','commonjs']等】,默认值为 `module` |
|
|
39
|
-
**
|
|
39
|
+
**outputDir** | String | 输出存放文件夹,默认值为 `lib` |
|
|
40
40
|
**clean** | Boolean | 是否在打包前清除存放文件夹,默认值为 `true` |
|
|
41
41
|
**compileDir** | String | 编译目录,默认值为 `src/components` |
|
|
42
42
|
**webpackConf** | Function|Object | 自定义 webpack 配置项, 如传入函数,函数参数为 `function (webpackConf,env,format){ return webpackConf; }`,需要返回值为自定义的 webpack 配置项 `webpackConf(已有的webpack配置项)/env(当前运行模式[development|library])/format(编译格式[development模式下为null])` |
|
|
@@ -50,20 +50,20 @@ import { resolve } from 'path';
|
|
|
50
50
|
|
|
51
51
|
export default {
|
|
52
52
|
compileDir: 'src/components', // 默认编译目录
|
|
53
|
-
|
|
53
|
+
outputDir: 'lib', // 输出存放文件夹
|
|
54
54
|
clean: true, // 是否在打包前清除存放文件夹
|
|
55
55
|
|
|
56
56
|
//【format 使用方式1】
|
|
57
|
-
format: 'module', // 生成 module 格式的文件 同时相关代码文件放入到 [
|
|
57
|
+
format: 'module', // 生成 module 格式的文件 同时相关代码文件放入到 [outputDir]目录下【默认生成module格式】
|
|
58
58
|
|
|
59
59
|
//【format 使用方式2】
|
|
60
|
-
format: ['module', 'commonjs'], // 分别生成 module 和 commonjs 类型 同时相关代码文件放入到 [
|
|
60
|
+
format: ['module', 'commonjs'], // 分别生成 module 和 commonjs 类型 同时相关代码文件放入到 [outputDir]/(module|commonjs) 目录下
|
|
61
61
|
|
|
62
62
|
//【format 使用方式3】
|
|
63
63
|
format: {
|
|
64
|
-
// 生成 module 类型 同时相关代码文件放入到 [
|
|
64
|
+
// 生成 module 类型 同时相关代码文件放入到 [outputDir]/es 目录下
|
|
65
65
|
module: 'es',
|
|
66
|
-
// 生成 commonjs 类型 同时相关代码文件放入到 [
|
|
66
|
+
// 生成 commonjs 类型 同时相关代码文件放入到 [outputDir]/cjs 目录下
|
|
67
67
|
commonjs: 'cjs',
|
|
68
68
|
},
|
|
69
69
|
|
|
@@ -91,22 +91,25 @@ export default {
|
|
|
91
91
|
|
|
92
92
|
###### 使用lib.config.js
|
|
93
93
|
```
|
|
94
|
+
const path = require('path');
|
|
95
|
+
const { merge } = require('webpack-merge');
|
|
96
|
+
|
|
94
97
|
module.exports = {
|
|
95
98
|
compileDir: 'src/components', // 默认编译目录
|
|
96
|
-
|
|
99
|
+
outputDir: 'lib', // 输出存放文件夹
|
|
97
100
|
clean: true, // 是否在打包前清除存放文件夹
|
|
98
101
|
|
|
99
102
|
//【format 使用方式1】
|
|
100
|
-
format: 'module', // 生成 module 格式的文件 同时相关代码文件放入到 [
|
|
103
|
+
format: 'module', // 生成 module 格式的文件 同时相关代码文件放入到 [outputDir]目录下【默认生成module格式】
|
|
101
104
|
|
|
102
105
|
//【format 使用方式2】
|
|
103
|
-
format: ['module', 'commonjs'], // 分别生成 module 和 commonjs 类型 同时相关代码文件放入到 [
|
|
106
|
+
format: ['module', 'commonjs'], // 分别生成 module 和 commonjs 类型 同时相关代码文件放入到 [outputDir]/(module|commonjs) 目录下
|
|
104
107
|
|
|
105
108
|
//【format 使用方式3】
|
|
106
109
|
format: {
|
|
107
|
-
// 生成 module 类型 同时相关代码文件放入到 [
|
|
110
|
+
// 生成 module 类型 同时相关代码文件放入到 [outputDir]/es 目录下
|
|
108
111
|
module: 'es',
|
|
109
|
-
// 生成 commonjs 类型 同时相关代码文件放入到 [
|
|
112
|
+
// 生成 commonjs 类型 同时相关代码文件放入到 [outputDir]/cjs 目录下
|
|
110
113
|
commonjs: 'cjs',
|
|
111
114
|
},
|
|
112
115
|
|
|
@@ -154,7 +157,7 @@ module.exports = {
|
|
|
154
157
|
**<font size="2">format 使用方式1</font>**
|
|
155
158
|
```
|
|
156
159
|
├── ... # 其他文件
|
|
157
|
-
├── [
|
|
160
|
+
├── [outputDir]/ # 输出存放文件夹
|
|
158
161
|
│ ├── button/ # 组件目录
|
|
159
162
|
│ │ ├── index.js # 组件入口文件
|
|
160
163
|
│ │ ├── style/ # 组件样式目录
|
|
@@ -167,7 +170,7 @@ module.exports = {
|
|
|
167
170
|
**<font size="2">format 使用方式2</font>**
|
|
168
171
|
```
|
|
169
172
|
├── ... # 其他文件
|
|
170
|
-
├── [
|
|
173
|
+
├── [outputDir]/ # 输出存放文件夹
|
|
171
174
|
│ ├── module/ # module 格式代码目录
|
|
172
175
|
│ │ ├── button/ # 组件目录
|
|
173
176
|
│ │ │ ├── index.js # 组件入口文件
|
|
@@ -189,7 +192,7 @@ module.exports = {
|
|
|
189
192
|
**<font size="2">format 使用方式3</font>**
|
|
190
193
|
```
|
|
191
194
|
├── ... # 其他文件
|
|
192
|
-
├── [
|
|
195
|
+
├── [outputDir]/ # 输出存放文件夹
|
|
193
196
|
│ ├── es/ # module 格式代码目录
|
|
194
197
|
│ │ ├── button/ # 组件目录
|
|
195
198
|
│ │ │ ├── index.js # 组件入口文件
|
package/bin/dev.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
const webpackDevServerPath = require.resolve('webpack-dev-server/bin/webpack-dev-server');
|
|
1
2
|
const { spawnPromise } = require('../utils/util.js');
|
|
2
3
|
const { getResolvePath } = require('../utils/file.js');
|
|
3
4
|
|
|
4
5
|
const spawnDev = async () => {
|
|
5
|
-
await spawnPromise(
|
|
6
|
+
await spawnPromise(webpackDevServerPath, ['--config',
|
|
6
7
|
`${getResolvePath('config/webpack.dev.conf.js')}`,
|
|
7
8
|
'--progress',
|
|
8
9
|
...process.argv.slice(3),
|
|
9
|
-
]
|
|
10
|
+
]);
|
|
10
11
|
}
|
|
11
12
|
spawnDev();
|
package/bin/index.js
CHANGED
|
@@ -6,7 +6,6 @@ const packageJson = require('../package.json');
|
|
|
6
6
|
|
|
7
7
|
const program = new Command(packageJson.name);
|
|
8
8
|
|
|
9
|
-
|
|
10
9
|
program.version(packageJson.version);
|
|
11
10
|
program.command('lib').action(() => {
|
|
12
11
|
require('./lib.js');
|
|
@@ -19,16 +18,6 @@ program.command('clean').option('-n, --name <name>', '删除项目内的文件
|
|
|
19
18
|
});
|
|
20
19
|
program.parse();
|
|
21
20
|
|
|
22
|
-
// 处理程序退出
|
|
23
|
-
// const handleExit = (exitStatus = 0) => {
|
|
24
|
-
// console.error(chalk.red(
|
|
25
|
-
// `进程异常退出.\n`
|
|
26
|
-
// ));
|
|
27
|
-
// // 确保exitStatus是数字类型
|
|
28
|
-
// const numericExitStatus = typeof exitStatus === 'number' ? exitStatus : 1;
|
|
29
|
-
// process.exit(numericExitStatus);
|
|
30
|
-
// }
|
|
31
|
-
|
|
32
21
|
// 处理打印错误信息
|
|
33
22
|
const handleError = (error, _exitStatus = 1) => {
|
|
34
23
|
console.log('')
|
|
@@ -36,10 +25,6 @@ const handleError = (error, _exitStatus = 1) => {
|
|
|
36
25
|
`进程出错! \n` +
|
|
37
26
|
`${error ? (error.message || error) : ''}`
|
|
38
27
|
));
|
|
39
|
-
// handleExit(exitStatus);
|
|
40
28
|
}
|
|
41
29
|
|
|
42
|
-
// process.on('SIGINT', handleExit); // 终止监听
|
|
43
|
-
// process.on('SIGTERM', handleExit); // 终止监听
|
|
44
30
|
process.on('uncaughtException', (error) => handleError(error, 1)); // 异常监听
|
|
45
|
-
// process.on('unhandledRejection', (reason, _promise) => handleError(reason, 1)); // 异常监听
|
package/bin/lib.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
|
+
const webpackPath = require.resolve('webpack/bin/webpack');
|
|
2
3
|
const { timestampToHMS, DateTimeDiffS, spawnPromise, isObject } = require('../utils/util.js');
|
|
3
4
|
const { getResolvePath, getCustomConfig, removeFile } = require('../utils/file.js');
|
|
4
5
|
const { OUTPUT } = require('../config/PATH.js');
|
|
5
|
-
|
|
6
6
|
const customConfig = getCustomConfig();
|
|
7
7
|
|
|
8
8
|
const output = customConfig && customConfig.outputDir ? customConfig.outputDir : OUTPUT;
|
|
@@ -17,7 +17,7 @@ const spawnBuildLib = async () => {
|
|
|
17
17
|
console.log(' ');
|
|
18
18
|
if (Array.isArray(format)) {
|
|
19
19
|
isRun = true;
|
|
20
|
-
await Promise.all(format.map(type => spawnPromise(
|
|
20
|
+
await Promise.all(format.map(type => spawnPromise(webpackPath, [
|
|
21
21
|
'--config',
|
|
22
22
|
`${getResolvePath('config/webpack.lib.conf.js')}`,
|
|
23
23
|
'--progress', '--color',
|
|
@@ -25,10 +25,10 @@ const spawnBuildLib = async () => {
|
|
|
25
25
|
`format=${type}`,
|
|
26
26
|
`output=${output}`,
|
|
27
27
|
...process.argv.slice(3),
|
|
28
|
-
]
|
|
28
|
+
])));
|
|
29
29
|
} else if (isObject(format)) {
|
|
30
30
|
isRun = true;
|
|
31
|
-
await Promise.all(Object.keys(format).map(key => spawnPromise(
|
|
31
|
+
await Promise.all(Object.keys(format).map(key => spawnPromise(webpackPath, [
|
|
32
32
|
'--config',
|
|
33
33
|
`${getResolvePath('config/webpack.lib.conf.js')}`,
|
|
34
34
|
'--progress', '--color',
|
|
@@ -37,17 +37,17 @@ const spawnBuildLib = async () => {
|
|
|
37
37
|
`libName=${format[key]}`,
|
|
38
38
|
`output=${output}`,
|
|
39
39
|
...process.argv.slice(3),
|
|
40
|
-
]
|
|
40
|
+
])));
|
|
41
41
|
}
|
|
42
42
|
if (!isRun || isMerageAll) {
|
|
43
|
-
await spawnPromise(
|
|
43
|
+
await spawnPromise(webpackPath, ['--config',
|
|
44
44
|
`${getResolvePath('config/webpack.lib.conf.js')}`,
|
|
45
45
|
'--progress', '--color',
|
|
46
46
|
'--env', 'formatType=String',
|
|
47
47
|
format && `format=${format}`,
|
|
48
48
|
`output=${output}`,
|
|
49
49
|
...process.argv.slice(3),
|
|
50
|
-
]
|
|
50
|
+
]);
|
|
51
51
|
}
|
|
52
52
|
console.log(' ');
|
|
53
53
|
console.log(chalk.green(`执行结束: ${timestampToHMS(Date.now())}, 耗时: ${DateTimeDiffS(Date.now(), startTime)}`));
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { getResolvePath } = require('../utils/file');
|
|
2
2
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
3
|
+
const babelLoader = require.resolve('babel-loader');
|
|
3
4
|
|
|
4
5
|
const baseConfig = (env) => {
|
|
5
6
|
const isDev = env === 'development';
|
|
@@ -81,7 +82,7 @@ const baseConfig = (env) => {
|
|
|
81
82
|
exclude: /node_modules/,
|
|
82
83
|
use: [
|
|
83
84
|
{
|
|
84
|
-
loader:
|
|
85
|
+
loader: babelLoader,
|
|
85
86
|
options: {
|
|
86
87
|
configFile: getResolvePath('babel.config.json', __dirname),
|
|
87
88
|
},
|
|
@@ -4,7 +4,7 @@ const { BannerPlugin } = require('webpack');
|
|
|
4
4
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
5
5
|
const TerserPlugin = require('terser-webpack-plugin');
|
|
6
6
|
const { OUTPUT, DEFAULT_FORMAT, ROOT, COMPILE_DIR } = require('./PATH.js');
|
|
7
|
-
const { getCustomConfig, checkFileExist } = require('../utils/file.js');
|
|
7
|
+
const { getCustomConfig, checkFileExist, getResolvePath } = require('../utils/file.js');
|
|
8
8
|
const getComponentExternals = require('../utils/parserExportFile.js');
|
|
9
9
|
const baseConfig = require('./webpack.base.conf.js');
|
|
10
10
|
const multiOutputConfig = require('./output.multi.js');
|
|
@@ -40,7 +40,7 @@ const getLibConfig = function ({
|
|
|
40
40
|
'react-router-dom': 'react-router-dom',
|
|
41
41
|
},
|
|
42
42
|
optimization: {
|
|
43
|
-
minimize:
|
|
43
|
+
minimize: true, // 是否开启压缩
|
|
44
44
|
moduleIds: 'deterministic',
|
|
45
45
|
minimizer: [
|
|
46
46
|
new TerserPlugin({
|
|
@@ -235,6 +235,7 @@ const getLibConfig = function ({
|
|
|
235
235
|
}
|
|
236
236
|
}
|
|
237
237
|
const lastConfig = merge(outputConfig, buildConfig, { entry: defaultEntry, externals: getcompoentExternals });
|
|
238
|
+
if (lastConfig.output.path != getResolvePath(output, ROOT)) lastConfig.output.path = getResolvePath(output, ROOT);
|
|
238
239
|
return lastConfig;
|
|
239
240
|
};
|
|
240
241
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wb-lib-tool",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.5",
|
|
4
4
|
"description": "基于webpack库模式编译生成组件库",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"readme": "README.ch.md",
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"sass": "^1.93.2",
|
|
64
64
|
"sass-loader": "^16.0.6",
|
|
65
65
|
"style-loader": "^4.0.0",
|
|
66
|
+
"terser-webpack-plugin": "^5.3.16",
|
|
66
67
|
"ts-node": "^10.9.2",
|
|
67
68
|
"typescript": "^5.9.3",
|
|
68
69
|
"webpack": "^5.102.1",
|
|
@@ -86,5 +87,15 @@
|
|
|
86
87
|
"overrides": {
|
|
87
88
|
"react": "$react",
|
|
88
89
|
"react-dom": "$react-dom"
|
|
90
|
+
},
|
|
91
|
+
"pnpm": {
|
|
92
|
+
"onlyBuiltDependencies": [
|
|
93
|
+
"@parcel/watcher",
|
|
94
|
+
"@swc/core",
|
|
95
|
+
"core-js",
|
|
96
|
+
"core-js",
|
|
97
|
+
"esbuild",
|
|
98
|
+
"less"
|
|
99
|
+
]
|
|
89
100
|
}
|
|
90
101
|
}
|
package/utils/file.js
CHANGED
|
@@ -7,7 +7,7 @@ const {
|
|
|
7
7
|
} = require('path');
|
|
8
8
|
const { readdirSync, statSync, existsSync } = require('fs');
|
|
9
9
|
const { rimraf } = require('rimraf');
|
|
10
|
-
const {
|
|
10
|
+
const { getFileExportContent } = require('./getFileContent.js');
|
|
11
11
|
const { ROOT, BUILD_FILE_NAME } = require('../config/PATH.js');
|
|
12
12
|
|
|
13
13
|
// 查询将要使用的文件是否存在
|
|
@@ -124,21 +124,10 @@ const getCustomConfig = () => {
|
|
|
124
124
|
if (!checkFileExist(buildConfigPath)) continue;
|
|
125
125
|
let conf = null;
|
|
126
126
|
try {
|
|
127
|
-
|
|
128
|
-
// 清除缓存,确保每次都重新加载文件
|
|
129
|
-
if (require.cache[require.resolve(buildConfigPath)]) {
|
|
130
|
-
delete require.cache[require.resolve(buildConfigPath)];
|
|
131
|
-
}
|
|
132
|
-
// 直接使用require加载配置文件
|
|
133
|
-
conf = require(buildConfigPath);
|
|
134
|
-
// 如果是ES模块导出,提取default属性
|
|
135
|
-
conf = conf.default || conf;
|
|
136
|
-
} else {
|
|
137
|
-
conf = getTsFileExportContent(buildConfigPath);
|
|
138
|
-
}
|
|
127
|
+
conf = getFileExportContent(buildConfigPath);
|
|
139
128
|
return conf;
|
|
140
129
|
} catch (error) {
|
|
141
|
-
console.error(' 配置文件加载失败 => \n', error);
|
|
130
|
+
console.error(' 配置文件加载失败 => \n', error, '\n');
|
|
142
131
|
continue;
|
|
143
132
|
}
|
|
144
133
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const getFileExportContent = (filePath) => {
|
|
2
2
|
if (!filePath) return null;
|
|
3
3
|
let exports = null;
|
|
4
4
|
require('ts-node').register({
|
|
@@ -11,10 +11,10 @@ const getTsFileExportContent = (filePath) => {
|
|
|
11
11
|
}
|
|
12
12
|
});
|
|
13
13
|
// 直接加载原文件,自动处理所有依赖
|
|
14
|
-
exports = require(filePath).default; // 取 export default
|
|
14
|
+
exports = require(filePath).default || require(filePath); // 取 export default
|
|
15
15
|
return exports;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
module.exports = {
|
|
19
|
-
|
|
19
|
+
getFileExportContent
|
|
20
20
|
}
|
package/utils/util.js
CHANGED
|
@@ -22,7 +22,7 @@ const DateTimeDiffS = (endTime, startTime) => {
|
|
|
22
22
|
// spawn promise 封装
|
|
23
23
|
const spawnPromise = (command, args, options = {}) => {
|
|
24
24
|
return new Promise((resolve, reject) => {
|
|
25
|
-
const cross = spawn(command, args, options);
|
|
25
|
+
const cross = spawn(command, args, Object.assign({ stdio: 'inherit' }, options));
|
|
26
26
|
cross.on('close', (code) => {
|
|
27
27
|
if (code != 0) {
|
|
28
28
|
return reject(new Error(`进程[${command}]命令执行失败:退出码 ${code}`));
|