forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.js
More file actions
214 lines (192 loc) · 5.57 KB
/
dev.js
File metadata and controls
214 lines (192 loc) · 5.57 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
/**
* 启动服务,根据传入的路径地址,按照 ICE page 的格则搜寻代码,并启动编译服务
* @param {String} cwd 项目目录
* @param {Object} options 命令行参数
*/
process.env.NODE_ENV = 'development';
const address = require('address');
const chalk = require('chalk');
const clearConsole = require('react-dev-utils/clearConsole');
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const getPaths = require('./config/paths');
const getEntries = require('./config/getEntry');
const getWebpackConfigDev = require('./config/webpack.config.dev');
// const npmUpdate = require('./helpers/npmUpdate');
const iceworksClient = require('./iceworksClient');
/* eslint no-console:off */
module.exports = function(args, subprocess) {
const cwd = process.cwd();
const HOST = args.host || '0.0.0.0';
const PORT = args.port || 3333;
const send = function(data) {
iceworksClient.send(data);
if (subprocess && typeof subprocess.send === 'function') {
subprocess.send(data);
}
};
const LOCAL_IP = address.ip();
const isInteractive = false; // process.stdout.isTTY;
const entries = getEntries(cwd, false);
const paths = getPaths(cwd);
const packageData = require(paths.appPackageJson);
// get ice config by package.ice
const webpackConfig = getWebpackConfigDev(
entries,
paths,
packageData.buildConfig || packageData.ice
);
if (iceworksClient.available) {
webpackConfig.plugins.push(
new webpack.ProgressPlugin((percentage, msg) => {
send({
action: 'update_project',
message: 'compiler_progress',
data: {
statusCompile: 'progress',
statusCompileProgress: percentage,
statusCompileProgressText: msg,
},
});
})
);
}
let isFirstCompile = true;
const compiler = webpack(webpackConfig);
const devServerConfig = require('./config/webpack.server.config')(
paths,
args
);
const devServer = new WebpackDevServer(compiler, devServerConfig);
compiler.plugin('done', (stats) => {
if (isInteractive) {
clearConsole();
}
if (isFirstCompile) {
send({
action: 'update_project',
message: 'server_finished',
data: {
statusDev: 'working',
serverUrl: `http://${LOCAL_IP}:${PORT}`,
},
});
isFirstCompile = false;
console.log(chalk.cyan('Starting the development server...'));
console.log(' ', chalk.yellow(`http://localhost:${PORT}`));
console.log(' ', chalk.yellow(`http://${LOCAL_IP}:${PORT}`));
}
console.log(
stats.toString({
colors: true,
chunks: false,
assets: true,
children: false,
modules: false,
})
);
const json = stats.toJson({}, true);
const messages = formatWebpackMessages(json);
const isSuccessful = !messages.errors.length && !messages.warnings.length;
if (isSuccessful) {
if (stats.stats) {
console.log(chalk.green('Compiled successfully'));
} else {
console.log(
chalk.green(
`Compiled successfully in ${(json.time / 1000).toFixed(1)}s!`
)
);
}
}
if (messages.errors.length) {
if (messages.errors.length > 1) {
messages.errors.length = 1;
}
console.log(chalk.red('Failed to compile.\n'));
console.log(messages.errors.join('\n\n'));
} else if (messages.warnings.length) {
console.log(chalk.yellow('Compiled with warnings.'));
console.log();
messages.warnings.forEach((message) => {
console.log(message);
console.log();
});
// Teach some ESLint tricks.
console.log('You may use special comments to disable some warnings.');
console.log(
`Use ${chalk.yellow(
'// eslint-disable-next-line'
)} to ignore the next line.`
);
console.log(
`Use ${chalk.yellow(
'/* eslint-disable */'
)} to ignore all warnings in a file.`
);
console.log();
}
if (isSuccessful) {
// 服务启动完成切没有任务错误与警告
send({
action: 'update_project',
message: 'compiler_success',
data: {
statusCompile: 'success',
serverUrl: `http://${LOCAL_IP}:${PORT}`,
},
});
} else {
// 服务启动完成切没有任务错误与警告
send({
action: 'update_project',
message: 'compiler_failed',
data: {
statusCompile: 'failed',
serverUrl: `http://${LOCAL_IP}:${PORT}`,
},
});
}
});
compiler.plugin('invalid', () => {
if (isInteractive) {
clearConsole();
}
console.log('Compiling...');
send({
action: 'update_project',
message: 'compiler_compiling',
data: {
statusCompile: 'compiling',
},
});
});
devServer.use(function(req, res, next) {
console.log('Time:', Date.now());
next();
});
devServer.listen(PORT, HOST, (err) => {
// 端口被占用,退出程序
if (err) {
send({
action: 'update_project',
message: 'server_failed',
data: {
statusDev: 'failed',
},
});
console.error(err);
process.exit(500);
} else {
send({
action: 'update_project',
message: 'server_success',
data: {
statusDev: 'working',
serverUrl: `http://${LOCAL_IP}:${PORT}`,
},
});
}
});
};