初始化项目

This commit is contained in:
dengtongyu committed 2018-11-12 17:48:13 +08:00
commit 4ab6cf96c0
38 files changed
+8362

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');
const util = require('./util');
const webpackConfig = require('./webpack.react');
let _paths = {};
let _configJson = {};
exports.init = (type, paths, command) => {
_paths = paths;
let tplPath = path.join(_paths.rootPath, util.tplTypePath(type));
fs.copy(tplPath, _paths.targetPath)
.then(() => {
console.log(chalk.bold.green('生成模板成功!'));
_configJson = util.configJson(_paths.targetPath);
if(_configJson.template == 'react') {
webpackConfig.run(command);
}
})
.catch(err => {
console.error(err);
console.error(chalk.bold.red('生成模板失败!'));
});
}
exports.dev = (paths, command) => {
_paths = paths;
if(_configJson.template == 'react') {
webpackConfig.run(paths, command);
}
}
exports.pack = (paths, command) => {
_paths = paths;
_configJson = util.configJson(_paths.targetPath);
if(_configJson.template == 'react') {
webpackConfig.run(paths, command);
}
}
+64
View File
@@ -0,0 +1,64 @@
const path = require('path');
const fs = require('fs-extra');
//const inquirer = require('inquirer');
//随机路径名
exports.randomActName = () => {
let d = new Date();
let m = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1).toString();
let dd = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
let str = d.getFullYear() + m + dd + String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0)) + Math.floor(Math.random() * 10);
return str;
}
//模板路径名
exports.tplTypePath = (type) => {
let _path = '';
if(type) {
_path = path.join('template', type);
}else{
_path = path.join('template', 'react');
}
return _path;
}
//初始化后的路径信息
exports.paths = (pathName, rootPath) => {
return {
pathName:pathName,
rootPath:rootPath,
targetPath:path.join(rootPath, 'src', 'pages' , pathName)
};
}
//获取项目config
exports.configJson = (targetPath) => {
let jsonPath = path.join(targetPath, 'config.json');
return require(jsonPath);
}
//是否存在该目录
exports.pathExists = async (pathName, rootPath) => {
let targetPath = path.join(rootPath, 'src', 'pages' , pathName);
let exists = await fs.pathExists(targetPath);
return exists;
// if(exists) {
// return true;
// }else{
// return '没有该目录,请重新输入'
// }
}
//会话流
// exports.inquirer = (option, promps, cb) => {
// inquirer.prompt(promps).then(function (answers) {
// //console.log('\n',answers)
// option = Object.assign(answers,option);
// if(cb) cb(option);
// });
// }
+111
View File
@@ -0,0 +1,111 @@
const path = require('path');
const chalk = require('chalk');
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const util = require('./util');
let _webpackConfig = {
module:{
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-react',
'@babel/preset-env'
],
plugins: [
["@babel/plugin-proposal-function-bind"]
]
}
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins:[]
};
exports.run = (paths, command) => {
//入口文件
_webpackConfig.entry = path.join(paths.targetPath, 'src', 'index.js');
//输出路径
_webpackConfig.output = {
path: path.join(paths.targetPath, 'dist'),
filename: 'index.js'
}
_webpackConfig.plugins.push(
new HtmlWebPackPlugin({
template: path.join(paths.targetPath, 'src', 'index.html'),
filename: path.join(paths.targetPath, 'dist', 'index.html')
})
);
//console.log(_webpackConfig,paths);
let _configJson = util.configJson(paths.targetPath);
let compiler = null;
if(command == 'init' || command == 'dev') {
_webpackConfig.mode = 'development';
let devServer = {
contentBase: path.join(paths.targetPath, "dist"),
compress: true,
host: 'localhost',
port: 8088,
hot: true,
//open: true,
stats: {colors: true}
}
if(_configJson.proxy) {
devServer.proxy = _configJson.proxy;
}
//node api 启动热更新
_webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
webpackDevServer.addDevServerEntrypoints(_webpackConfig, devServer);
compiler = webpack(_webpackConfig);
//node api 启动本地服务
let server = new webpackDevServer(compiler, devServer);
console.log(_webpackConfig);
server.listen(devServer.port, 'localhost', (err) => {
if(err) {
console.log(err);
}else{
console.log(chalk.bold.green('服务启动: '+ devServer.contentBase +' : http://localhost:' + devServer.port));
}
});
}
if(command == 'pack') {
_webpackConfig.mode = 'production';
compiler = webpack(_webpackConfig, (err, stats) => {
if (err || stats.hasErrors()) {
//处理错误
console.error(err);
}
//处理完成
console.log(chalk.bold.green('打包编译完成!'));
});
}
}