Skip to content

Commit 7da6119

Browse files
committed
新增 gen webpack 命令,新增gen 可配置参数 --out-dir --installed
1 parent 1c5118c commit 7da6119

36 files changed

+663
-148
lines changed

PLAN.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ show prompt list 多选择
1010
-- typescript
1111
-- readme
1212
-- lerna
13+
-- react
14+
-- vue
1315

1416
cli gen eslint 内联
1517
cli gen eslint,mocha,babel,rollup 内联多选
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

assets/webpack/config/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title><%= htmlWebpackPlugin.options.title %></title>
6+
</head>
7+
<body>
8+
<div id="root"></div>
9+
</body>
10+
</html>

assets/webpack/config/webpack.base.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const path = require('path');
2+
const HTMLWebpackPlugin = require('html-webpack-plugin');
3+
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
4+
const ManifestWebpackPlugin = require('webpack-manifest-plugin');
5+
module.exports = {
6+
entry: path.resolve(__dirname, '../src/index.js'),
7+
output: {
8+
path: path.resolve(__dirname, '../dist')
9+
},
10+
optimization: {
11+
runtimeChunk: 'single',
12+
splitChunks: {
13+
chunks: 'all',
14+
name: false,
15+
},
16+
},
17+
module: {
18+
rules: [
19+
<%_ if(locals.typescript){ _%>
20+
{
21+
test: /\.(ts|tsx)/,
22+
use: 'ts-loader',
23+
include: [
24+
path.resolve(__dirname, '../src')
25+
],
26+
},
27+
<%_ } _%>
28+
{
29+
test: /\.css$/,
30+
use: [
31+
'style-loader',
32+
'css-loader',
33+
]
34+
},
35+
{
36+
test: /\.(jpg|png|svg|gif)$/,
37+
use: 'file-loader'
38+
},
39+
{
40+
test: /\.(woff|woff2|eot|ttf|otf)$/,
41+
use: 'file-loader'
42+
},
43+
]
44+
},
45+
46+
plugins: [
47+
new CleanWebpackPlugin(),
48+
new HTMLWebpackPlugin({
49+
title: Math.random(),
50+
template: path.resolve(__dirname, './index.html'),
51+
hash: true
52+
}),
53+
new ManifestWebpackPlugin(),
54+
]
55+
}

assets/webpack/config/webpack.dev.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const merge = require('webpack-merge');
2+
const base = require('./webpack.base');
3+
const path = require('path');
4+
const WebpackDevConfig = merge(base, {
5+
mode: 'development',
6+
output: {
7+
filename: '[name].js',
8+
},
9+
devtool: 'source-map',
10+
devServer: {
11+
contentBase: path.resolve(__dirname, 'dist'),
12+
port: 9000,
13+
publicPath: '/',
14+
hot: true
15+
},
16+
})
17+
18+
// console.log(WebpackDevConfig);
19+
20+
module.exports = WebpackDevConfig;

assets/webpack/config/webpack.prod.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const merge = require('webpack-merge');
2+
const base = require('./webpack.base');
3+
const path = require('path');
4+
const WorkboxPlugin = require('workbox-webpack-plugin');
5+
const WebpackProdConfig = merge(base, {
6+
mode: 'production',
7+
output: {
8+
filename: '[name].[contenthash].js',
9+
},
10+
optimization: {
11+
moduleIds: 'hashed',
12+
},
13+
module: {
14+
rules: [
15+
{
16+
test: /\.(js|jsx)/,
17+
use: [
18+
{
19+
loader: 'babel-loader',
20+
options: {
21+
presets: ['@babel/env'],
22+
plugins: [
23+
[
24+
'@babel/plugin-transform-runtime',
25+
{
26+
corejs: 3
27+
}
28+
]
29+
],
30+
ignore: ['node_modules/**']
31+
}
32+
}
33+
],
34+
include: [
35+
path.resolve(__dirname, '../src')
36+
],
37+
},
38+
]
39+
},
40+
plugins: [
41+
// new WorkboxPlugin.GenerateSW({
42+
// cacheId: 'seed-cache',
43+
// importWorkboxFrom: 'cdn', // 可填`cdn`,`local`,`disabled`,
44+
// // importScripts: '/scripts-build/commseed/workboxswMain.js',
45+
// skipWaiting: true, //跳过waiting状态
46+
// clientsClaim: true, //通知让新的sw立即在页面上取得控制权
47+
// cleanupOutdatedCaches: true,//删除过时、老版本的缓存
48+
49+
// //最终生成的service worker地址,这个地址和webpack的output地址有关
50+
// swDest: './sw.js',
51+
// // include: [
52+
// // ],
53+
// //缓存规则,可用正则匹配请求,进行缓存
54+
// //这里将js、css、还有图片资源分开缓存,可以区分缓存时间(虽然这里没做区分。。)
55+
// //由于种子农场此站点较长时间不更新,所以缓存时间可以稍微长一些
56+
// runtimeCaching: [
57+
// {
58+
// urlPattern: /.*\.js.*/i,
59+
// handler: 'CacheFirst',
60+
// options: {
61+
// cacheName: 'seed-js',
62+
// expiration: {
63+
// maxEntries: 20, //最多缓存20个,超过的按照LRU原则删除
64+
// maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
65+
// },
66+
// },
67+
// },
68+
// {
69+
// urlPattern: /.*css.*/,
70+
// handler: 'CacheFirst',
71+
// options: {
72+
// cacheName: 'seed-css',
73+
// expiration: {
74+
// maxEntries: 30, //最多缓存30个,超过的按照LRU原则删除
75+
// maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
76+
// },
77+
// },
78+
// },
79+
// {
80+
// urlPattern: /.*(png|svga).*/,
81+
// handler: 'CacheFirst',
82+
// options: {
83+
// cacheName: 'seed-image',
84+
// expiration: {
85+
// maxEntries: 30, //最多缓存30个,超过的按照LRU原则删除
86+
// maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
87+
// },
88+
// },
89+
// }
90+
// ]
91+
// })
92+
]
93+
})
94+
95+
// console.log(WebpackProdConfig);
96+
module.exports = WebpackProdConfig;

assets/webpack/webpack.config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const path = require('path');
2+
const merge = require('webpack-merge');
3+
const dev = require('./config/webpack.dev');
4+
const prod = require('./config/webpack.prod');
5+
const webpack = require('webpack');
6+
module.exports = env => {
7+
const isProd = env && env.NODE_ENV === 'prod';
8+
const InjectConfig = {
9+
module: {
10+
rules: [
11+
12+
]
13+
},
14+
plugins: [
15+
16+
]
17+
}
18+
return merge(isProd ? prod : dev, InjectConfig);
19+
}

ddd/.eslintignore2

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.git
2+
/.vscode
3+
/dist
4+
node_modules

ddd/.eslintrc2.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3+
parserOptions: {
4+
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
5+
sourceType: 'module', // Allows for the use of imports
6+
ecmaFeatures: {
7+
jsx: true
8+
},
9+
},
10+
extends: [
11+
'airbnb', // Uses airbnb, it including the react rule(eslint-plugin-react/eslint-plugin-jsx-a11y)
12+
'plugin:@typescript-eslint/recommended', // Optional enable, will more stricter
13+
'plugin:import/typescript', // Use prettier/react to pretty react syntax
14+
'plugin:promise/recommended',
15+
// 'plugin:prettier/recommended',
16+
// 'prettier/@typescript-eslint'
17+
],
18+
settings: {
19+
'import/parsers': {
20+
'@typescript-eslint/parser': ['.ts', '.tsx']
21+
},
22+
'import/resolver': {
23+
// use <root>/path/to/folder/tsconfig.json
24+
typescript: {
25+
directory: './tsconfig.json'
26+
},
27+
webpack: {
28+
config: './webpack.config.js'
29+
}
30+
}
31+
},
32+
env: {
33+
browser: true // enable all browser global variables
34+
},
35+
plugins: ['@typescript-eslint', 'promise'],
36+
rules: {
37+
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
38+
// e.g. '@typescript-eslint/explicit-function-return-type': 'off',
39+
'no-useless-constructor': 0,
40+
'no-console': 0,
41+
'class-methods-use-this': 0,
42+
43+
44+
/**
45+
* @description rules of @typescript-eslint
46+
*/
47+
'@typescript-eslint/prefer-interface': 'off', // also want to use 'type'
48+
'@typescript-eslint/explicit-function-return-type': 'off', // annoying to force return type
49+
'@typescript-eslint/indent': 'off' // avoid conflict with airbn
50+
51+
52+
/**
53+
* @description rules of eslint-plugin-prettier
54+
*/
55+
// 'prettier/prettier': [
56+
// error, {
57+
// singleQuote: true,
58+
// semi: false
59+
// }
60+
// ]
61+
},
62+
};
63+

lib/command/generate/action.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const SortObjectByKey = (obj) => {
2727
return prev;
2828
}, {});
2929
};
30-
const GenPlugin = (pluginName, usedMemory) => __awaiter(void 0, void 0, void 0, function* () {
30+
const GenPlugin = (pluginName, usedMemory, context) => __awaiter(void 0, void 0, void 0, function* () {
3131
if (plugins_1.PluginList.every(plugin => plugin.pluginName !== pluginName)) {
3232
throw new Error(`cant't resolve ${pluginName} plugin!`);
3333
}
@@ -37,8 +37,8 @@ const GenPlugin = (pluginName, usedMemory) => __awaiter(void 0, void 0, void 0,
3737
if (plugin.pluginName !== pluginName)
3838
continue;
3939
try {
40-
const packageInfo = yield plugin.install(usedMemory);
41-
ld.succeed(`generate ${pluginName}'s config files succeed!`);
40+
const packageInfo = yield plugin.install(usedMemory, context);
41+
ld.succeed(`resolved ${pluginName}'s config files!`);
4242
packageInfo && EP.emit('update_package', packageInfo);
4343
}
4444
catch (error) {
@@ -78,6 +78,21 @@ const GenUsedMemory = (pluginList) => {
7878
});
7979
return UsedMemory;
8080
};
81+
const BuildGenerateContext = (cmd) => {
82+
const outDir = cmd.outDir || '';
83+
const rootPath = process.cwd();
84+
let targetPath;
85+
if (path_1.default.isAbsolute(outDir)) {
86+
targetPath = outDir;
87+
}
88+
else {
89+
targetPath = path_1.default.resolve(rootPath, outDir);
90+
}
91+
return {
92+
rootPath,
93+
targetPath
94+
};
95+
};
8196
const GenAction = (pluginName, cmd) => __awaiter(void 0, void 0, void 0, function* () {
8297
pluginName = pluginName.trim().toLowerCase();
8398
let pluginList = [];
@@ -88,11 +103,14 @@ const GenAction = (pluginName, cmd) => __awaiter(void 0, void 0, void 0, functio
88103
else {
89104
pluginList = pluginName.split(',').filter(v => v.trim().length > 0);
90105
}
106+
const installed = cmd.installed || '';
107+
const installedList = installed.split(',').filter(v => v.trim().length > 0);
91108
GenPackage();
92-
const usedMemory = GenUsedMemory(pluginList);
109+
const usedMemory = GenUsedMemory(pluginList.concat(installedList));
110+
const context = BuildGenerateContext(cmd);
93111
for (let i = 0; i < pluginList.length; i++) {
94112
const plugin = pluginList[i];
95-
yield GenPlugin(plugin, usedMemory);
113+
yield GenPlugin(plugin, usedMemory, context);
96114
}
97115
EP.emit('on_generate_finish');
98116
});

lib/command/generate/gen/babel copy/index.js

-10
This file was deleted.

lib/command/generate/gen/babel/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ const BuildBabelPackageInfo = (usedMemory) => {
2222
};
2323
return packageInfo;
2424
};
25-
const GenBabelConfig = (usedMemory) => {
26-
parseRender_1.default(path_1.default.resolve(__dirname, '../../../../../assets/babel.config.js.bak'), path_1.default.resolve(process.cwd(), `babel.config2.js`), usedMemory);
27-
return BuildBabelPackageInfo(usedMemory);
25+
const GenBabelConfig = (usedMemory, context) => {
26+
return new Promise(resolve => {
27+
parseRender_1.default(path_1.default.resolve(__dirname, '../../../../../assets/babel.config.js'), path_1.default.resolve(context.targetPath, `babel.config2.js`), usedMemory, () => {
28+
resolve(BuildBabelPackageInfo(usedMemory));
29+
});
30+
});
2831
};
2932
exports.default = GenBabelConfig;

lib/command/generate/gen/eslint/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ const BuildESLintPackageInfo = (usedMemory) => {
7474
}
7575
return packageInfo;
7676
};
77-
const GenESLintConfig = (usedMemory) => {
78-
parseRender_1.default(path_1.default.resolve(__dirname, '../../../../../assets/eslintrc.js.bak'), path_1.default.resolve(process.cwd(), './.eslintrc2.js'), usedMemory);
79-
parseRender_1.default(path_1.default.resolve(__dirname, '../../../../../assets/eslintignore.bak'), path_1.default.resolve(process.cwd(), './.eslintignore2'), usedMemory);
80-
return BuildESLintPackageInfo(usedMemory);
77+
const GenESLintConfig = (usedMemory, context) => {
78+
return new Promise(resolve => {
79+
let count = 2;
80+
const onTaskEnd = () => {
81+
count--;
82+
count === 0 && resolve(BuildESLintPackageInfo(usedMemory));
83+
};
84+
parseRender_1.default(path_1.default.resolve(__dirname, '../../../../../assets/.eslintrc.js'), path_1.default.resolve(context.targetPath, './.eslintrc2.js'), usedMemory, onTaskEnd);
85+
parseRender_1.default(path_1.default.resolve(__dirname, '../../../../../assets/.eslintignore'), path_1.default.resolve(context.targetPath, './.eslintignore2'), usedMemory, onTaskEnd);
86+
});
8187
};
8288
exports.default = GenESLintConfig;

0 commit comments

Comments
 (0)