UNPKG

6.14 kBJavaScriptView Raw
1var program = require('commander');
2var pkg = require('./package.json');
3var webpack = require('webpack');
4var WebpackDevServer = require('webpack-dev-server');
5var defaultConfig = require('./lib/defaultConfig');
6var express = require('express');
7var inquirer = require('inquirer');
8var fs = require('fs');
9var path = require('path');
10var ejs = require('ejs');
11var WebpackConfig = require('./config/WebpackConfig');
12var rm = require('rimraf');
13
14let statOptions = {
15 colors: true,
16 modules: false,
17 children: false,
18 chunks: false,
19 chunkModules: false,
20};
21
22program
23 .version(pkg.version);
24
25
26function removeDirectory(callback) {
27 rm(path.join(process.cwd(), './dist/*'), err => {
28 if (err){
29 console.log(`删除${path.join(process.cwd(), './dist')}失败`);
30 return;
31 };
32 console.log(`删除${path.join(process.cwd(), './dist')}成功`);
33 callback();
34 })
35}
36
37
38function getWebpackConfig(webpackConfigFilePath, env) {
39 var config
40 try {
41 config = require(path.join(process.cwd(), webpackConfigFilePath))
42 } catch (e) {
43 console.log('读取 webpack.config.js 失败')
44 config = {}
45 }
46 config = Object.assign(defaultConfig.get(), config)
47 config.webpackConfig = config.webpack(WebpackConfig[env]())
48 return config;
49}
50
51program
52 .command('dev')
53 .option('-c --config [config]', 'webpack 配置文件', './webpack.config.js')
54 .action(function (cmd) {
55 var webpackConfig = getWebpackConfig(cmd.config, 'development');
56 WebpackConfig.postDevelopment(webpackConfig.webpackConfig, webpackConfig.devServer,webpackConfig)
57 let host = webpackConfig.devServer.host || '0.0.0.0';
58 removeDirectory(()=> {
59 webpack(webpackConfig.webpackConfig, function (err, stat) {
60 if (err) {
61 console.log(err)
62 } else {
63 console.log(`DevServer on http://${host}:${webpackConfig.devServer.port}`);
64 var server = new WebpackDevServer(webpack(webpackConfig.webpackConfig), {
65 stats: statOptions,
66 hot: webpackConfig.devServer.hot,
67 publicPath: webpackConfig.devServer.publicPath,
68 host: host,
69 headers: {
70 'Access-Control-Allow-Origin': '*'
71 },
72 disableHostCheck: true,
73 before: function (app) {
74 app.use(express.static(path.join(__dirname, `.${path.sep}node_modules${path.sep}`)))
75 app.use(express.static(path.join(__dirname, `..${path.sep}..${path.sep}..${path.sep}node_modules${path.sep}`)))
76 app.use('/'+webpackConfig.favicon.split('/').pop(),express.static(path.join(process.cwd(), webpackConfig.favicon)));
77 webpackConfig.devServer.before && webpackConfig.devServer.before(app);
78 }
79 })
80 server.listen(webpackConfig.devServer.port);
81 }
82 })
83 });
84 })
85
86program
87 .command('pro')
88 .option('-c --config [config]', 'webpack 配置文件', './webpack.config.js')
89 .action(function (cmd) {
90 var webpackConfig = getWebpackConfig(cmd.config, 'production')
91 WebpackConfig.postProduction(webpackConfig.webpackConfig, null, webpackConfig);
92 webpack(webpackConfig.webpackConfig, function (err, stat) {
93 if (err) {
94 console.log(err)
95 } else {
96 console.log(stat.toString(statOptions));
97 }
98 })
99 });
100
101
102program
103 .command('ssr')
104 .option('-c --config [config]', 'webpack 配置文件', './webpack.config.js')
105 .action(function (cmd) {
106 var webpackConfig = getWebpackConfig(cmd.config, 'NodeSSR')
107 var ssrConfig = WebpackConfig.postNodeSSR(webpackConfig.webpackConfig, null, webpackConfig);
108 webpack(ssrConfig, function (err, stat) {
109 if (err) {
110 console.log(err)
111 } else {
112 console.log(stat.toString(statOptions));
113 }
114 })
115 })
116
117program
118 .command('rename')
119 .action(function (cmd) {
120 const dist_ejs = path.join(process.cwd(), './dist_ejs');
121 const dist_ejs_temp = path.join(process.cwd(), './dist_ejs_temp')
122 const exist_dist_ejs_temp = fs.existsSync(dist_ejs_temp)
123 if(!exist_dist_ejs_temp)return false;
124 const exist_dist_ejs = fs.existsSync(dist_ejs);
125 if(exist_dist_ejs){
126 fs.renameSync(dist_ejs, `${dist_ejs}_old`)
127 }
128 fs.renameSync(dist_ejs_temp, dist_ejs)
129 rm(`${dist_ejs}_old`, err => {
130 if (err){
131 console.log(`静态资源目录切换失败`);
132 return;
133 };
134 console.log(`静态资源目录切换成功`);
135 })
136 })
137
138
139program
140 .command('init')
141 .action(function (cmd) {
142 inquirer.prompt([{
143 type: 'list',
144 name: 'type',
145 message: 'Which type do you want?',
146 choices: ['html-spa','node-spa','node-ssr']
147 }, {
148 type: 'list',
149 name: 'preset',
150 message: 'Need to support ie8?',
151 choices: ['ie8','chrome']
152 }, {
153 type: 'input',
154 name: 'devServerPort',
155 message: 'Dev Server Port?',
156 default: 8080
157 }, {
158 type: 'input',
159 name: 'entryJsPath',
160 message: 'What is the entry js path?',
161 default: './src/index.js'
162 }, {
163 type: 'input',
164 name: 'entryName',
165 message: 'Give the entry a name?',
166 default: 'main'
167 }]).then(function (answers) {
168 fs.writeFileSync(path.join(process.cwd(), './webpack.config.js'), ejs.render(fs.readFileSync(path.join(__dirname, './template/create/webpack.config.js.ejs'), 'utf8'), answers))
169 console.log(' \x1b[36mcreate\x1b[0m : webpack.config.js');
170 console.log('run \x1b[36migame dev\x1b[0m to begin')
171 }).catch(function (err) {
172 console.log(err)
173 });
174 })
175
176program.parse(process.argv);