-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-cli.js
More file actions
457 lines (399 loc) · 10.8 KB
/
Copy pathreact-cli.js
File metadata and controls
457 lines (399 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// 引入node自带的相关模块
const fs = require('fs')
const path = require('path')
const project_name = process.argv[2] || "project";
const dev_path = process.cwd() + '/' + project_name;
const dir_arr = [
'config/',
'src/',
]
//文件配置
const file_arr = [
'.babelrc',
'.gitignore',
'index.html',
'package.json',
'README.md',
'config/webpack.base.conf.js',
'config/webpack.dev.conf.js',
'config/webpack.prod.conf.js',
'src/App.js',
'src/main.js',
'src/main.css'
];
//递归清空目录
function delAll(path) {
let files_list = [];
files_list = fs.readdirSync(path);
files_list.forEach(function (file, index) {
let next = path + '/' + file;
if (fs.statSync(next).isDirectory()) {
delAll(next);
} else {
fs.unlinkSync(next);
}
})
fs.rmdirSync(path);
}
//设置相关目录
function createDir() {
//检查是否已创建
let check_exis = fs.existsSync(dev_path);
if (check_exis) {
console.log('清空残留开发文档');
delAll(dev_path);
}
//创建开发目录
fs.mkdirSync(dev_path);
for (let i = 0; i < dir_arr.length; i++) {
fs.mkdirSync(dev_path + '/' + dir_arr[i]);
}
console.log('项目文件夹创建完毕');
}
//创建开发文件
function createFile() {
for (let i = 0; i < file_arr.length; i++) {
fs.openSync(dev_path + '/' + file_arr[i], 'w');
}
console.log('项目文件创建完毕');
}
function fillText(filled){
switch(filled){
case 'config/webpack.base.conf.js':
var write_text = `
/*
* @Description: 公用的配置
* @Author: sunmingming
* @Email: sun_mingming@foxmail.com
* @Date: 2019-03-18 16:03:01
*/
'use strict'
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry:{
app:'./src/main.js'
},
output:{
path:path.resolve(__dirname, '../dist'),
filename:"[name].js"
},
resolve:{
extensions:['.ts','.tsx','.js','.json']
},
//loader
module:{
rules:[
{
test:/\.js|jsx$/,
exclude:/(node_modules|bower_components)/,//屏蔽不需要处理的文件
loader: 'babel-loader'
},{
test:/\.css$/,
use:[
{
loader:'style-loader'
},{
loader:'css-loader',
options:{
modules:true, //指定启用css模块化
localIdentName:'[name]_[local]_[hash:base64:5]' //指定css类型格式
}
}
]
}
]
},
//插件
plugins:[
new HtmlWebpackPlugin({
filename:'index.html',
template:'index.html',
inject:'body'
})
]
}
`;
return write_text;
break;
case 'config/webpack.dev.conf.js':
var write_text = `
/*
* @Description: 开发环境配置
* @Author: sunmingming
* @Email: sun_mingming@foxmail.com
* @Date: 2019-03-18 16:04:00
*/
'use strict'
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const OpenBrewserPlugin = require('open-browser-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
module.exports = merge(baseWebpackConfig, {
// 模式
mode: "development",
// 调试工具
devtool: 'inline-source-map',
// 开发服务器
devServer: {
contentBase: false, // 默认webpack-dev-server会为根文件夹提供本地服务器,如果想为另外一个目录下的文件提供本地服务器,应该在这里设置其所在目录
historyApiFallback: true, // 在开发单页应用时非常有用,它依赖于HTML5 history API,如果设置为true,所有的跳转将指向index.html
compress: true, // 启用gzip压缩
inline: true, // 设置为true,当源文件改变时会自动刷新页面
hot: true, // 模块热更新,取决于HotModuleReplacementPlugin
host: '127.0.0.1', // 设置默认监听域名,如果省略,默认为“localhost”
port: 8080 // 设置默认监听端口,如果省略,默认为“8080”
},
// 插件
plugins: [
// 热更新相关
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new OpenBrewserPlugin({
url:'http://localhost:8080'
})
],
optimization: {
nodeEnv: 'development',
}
});
`;
return write_text;
break;
case 'config/webpack.prod.conf.js':
var write_text = `
/*
* @Description: 生产环境配置
* @Author: sunmingming
* @Email: sun_mingming@foxmail.com
* @Date: 2019-03-18 16:04:14
*/
'use strict'
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
module.exports = merge(baseWebpackConfig, {
// 模式
mode: "production",
// 调试工具
devtool: '#source-map',
// 输出
output: {
path: path.resolve(__dirname, '../dist'),
filename: "js/[name].[chunkhash].js",
},
// 插件
plugins: [
new CleanWebpackPlugin(['dist', 'build'], {
root: path.resolve(__dirname, '../'),
}),
new webpack.HashedModuleIdsPlugin(),
],
// 代码分离相关
optimization: {
nodeEnv: 'production',
minimizer: [new UglifyJSPlugin()],
runtimeChunk: {
name: 'manifest'
},
splitChunks: {
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
name: false,
cacheGroups: {
vendor: {
test: /[\\\\/]node_modules[\\\\/]/,
name: 'vendor',
chunks: 'initial',
}
}
}
}
});
`;
return write_text;
break;
case 'src/App.js':
var write_text = `
/*
* @Description: React根组件
* @Author: sunmingming
* @Email: sun_mingming@foxmail.com
* @Date: 2019-03-18 17:46:14
*/
import React from 'react';
import ReactDOM from 'react-dom';
function App() {
return (
<div style={{ textAlign: 'center', fontSize: '24px' }}>
<a href="https://github.com/sun-nocat/react-cli">如果觉得这个脚手还不错,感谢给我一个star</a>
</div>
);
}
export default App;
`
return write_text;
break;
case 'src/main.js':
var write_text = `
/*
* @Description: 项目入口文件
* @Author: sunmingming
* @Email: sun_mingming@foxmail.com
* @Date: 2019-03-18 16:05:42
*/
import React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter, Route } from 'react-router-dom';
import App from './App';
import './main.css';
function Main() {
return (
<HashRouter>
<Route path="/" component={App} />
</HashRouter>
);
}
ReactDOM.render(<Main />, document.getElementById('app'));
`;
return write_text;
break;
case '.babelrc':
var write_text = `
{
"presets": ["latest","react","stage-2"],
"plugins": []
}
`;
return write_text;
break;
case '.gitignore':
var write_text = `
/node_modules
/dist
`;
return write_text;
break;
case 'index.html':
var write_text = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
`;
return write_text;
break;
case 'package.json':
var write_text = `
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"dependencies": {
"@babel/core": "^7.3.4",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-import": "^1.11.0",
"babel-preset-latest": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"clean-webpack-plugin": "^1.0.0",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.11.0",
"open-browser-webpack-plugin": "0.0.5",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-router-dom": "^4.0.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"uglifyjs-webpack-plugin": "^2.1.2",
"url-loader": "^1.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1",
"webpack-merge": "^4.2.1"
},
"devDependencies": {
"css-loader": "^2.1.1",
"style-loader": "^0.23.1"
},
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1",
"dev": "webpack-dev-server --hot --inline --grogress --colors --config config/webpack.dev.conf.js",
"start": "npm run dev",
"build": "webpack --progress --colors --config config/webpack.prod.conf.js"
},
"author": "sunmingming",
"license": "ISC"
}
`;
return write_text;
break;
case 'src/main.css':
var write_text = `
/*
* @Description: 全局的样式
* @Author: sunmingming
* @Email: sun_mingming@foxmail.com
* @Date: 2019-03-19 18:04:34
*/
html body{
margin: 0;
padding: 0;
}
`
return write_text;
break;
case 'README.md':
var write_text = `
## 使用方法
安装依赖 npm install
启动开发环境 npm start
线上打包 npm run build
`
return write_text;
break;
default:
//异常
try{
throw "配置文件出错:" + filled
}
catch (err){
console.log(err)
}
}
}
//填充开发文件
function fillFile() {
//填充package.json
for (let i = 0; i < file_arr.length; i++) {
fs.writeFileSync(dev_path + '/' + file_arr[i], fillText(file_arr[i]));
}
console.log('项目文件配置完毕');
}
(function() {
console.log('初始化脚本');
createDir();
createFile();
fillFile();
console.log('搭建完毕,enjoy it!');
})()