qmzreact 1.0.14 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,10 +11,11 @@ var fs = require('fs');
11
11
  var entry = [];
12
12
  var entryFile = ['index.js', 'index.jsx', 'index.ts', 'index.tsx'];
13
13
  for (var i = 0; i < entryFile.length; i++) {
14
+ var entryPath = utils.rootPath(processConfig.compileDir, entryFile[i]);
14
15
  try {
15
- const stats = fs.statSync(utils.rootPath(processConfig.compileDir, entryFile[i]));
16
+ const stats = fs.statSync(entryPath);
16
17
  if (stats.isFile()) {
17
- entry = [utils.rootPath(entryFile[i])];
18
+ entry = [entryPath];
18
19
  }
19
20
  break;
20
21
  } catch (err) {
@@ -41,6 +41,7 @@ webpack(dllConf, function (err, stats) {
41
41
  children: false,
42
42
  chunks: false,
43
43
  chunkModules: false
44
- }) + '\n')
44
+ }) + '\n');
45
+ process.exit(0); // 退出进程
45
46
  })
46
47
  }
@@ -25,14 +25,18 @@ var hotMiddleware = require('webpack-hot-middleware')(compiler)
25
25
  // // force page reload when html-webpack-plugin template changes
26
26
  compiler.hooks.make.tap('compilation', function (compilation) {
27
27
  compilation.hooks.finishModules.tap('html-webpack-plugin-after-emit', function (modules) {
28
+ process.env.NODE_STATUS = "running";
28
29
  hotMiddleware.publish({ action: 'reload' })
29
30
  })
30
31
  })
31
32
  // print compiler finished info
32
33
  compiler.hooks.done.tap('AfterCompiler', function(stats) {
33
34
  setTimeout(() => {
35
+ if (process.env.NODE_STATUS === 'rebuild') {
36
+ hotMiddleware.publish({ action: 'reload' })
37
+ }
34
38
  console.log('webpack \x1b[1mcompiled \x1B[32mfinished \x1B[37min',new Date().toLocaleString(),'\x1b[0m')
35
- }, 1000)
39
+ }, 2000)
36
40
  })
37
41
 
38
42
 
@@ -79,7 +83,9 @@ if (processConfig.isHttps) {
79
83
  }
80
84
  var uri = 'https://localhost:' + port // 直接显示页面
81
85
  console.log('Listening at ' + uri + '\n')
82
- opn(uri)
86
+ if (process.env.NODE_STATUS === 'build') {
87
+ opn(uri)
88
+ }
83
89
  })
84
90
  } else {
85
91
  service = app.listen(port, function (err) {
@@ -89,7 +95,9 @@ if (processConfig.isHttps) {
89
95
  }
90
96
  var uri = 'http://localhost:' + port // 直接显示页面
91
97
  console.log('Listening at ' + uri + '\n')
92
- opn(uri)
98
+ if (process.env.NODE_STATUS === 'build') {
99
+ opn(uri)
100
+ }
93
101
 
94
102
  })
95
103
  }
package/conf/options.js CHANGED
@@ -17,7 +17,7 @@ var {
17
17
  exports.utils = utils;
18
18
 
19
19
  exports.processConfig = {
20
- compileDir: typeof compileDir === 'string' ? compileDir : 'src',
20
+ compileDir: (compileDir && typeof compileDir === 'string') ? compileDir : 'src',
21
21
  watch: Array.isArray(watch) ? watch : watch ? [watch] : [],
22
22
  port: argvPort || port || 8888,
23
23
  isHttps,
package/conf/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  var path = require('path')
2
2
  var fs = require('fs')
3
+ var crypto = require('crypto')
3
4
  var { cosmiconfigSync } = require('cosmiconfig');
4
5
 
5
6
  var configFilePath = 'build.config.js';
@@ -58,8 +59,29 @@ function getWatchFileRestartService(pathname, restartService) {
58
59
  fs.watch(pathname, (eventType, filename) => {
59
60
  // 如果是文件,直接重启服务
60
61
  if (stats.isFile()) {
61
- // 如果是文件直接重启服务
62
- reStart();
62
+ // 只在文件内容实际修改(mtime 变化)时重启
63
+ fs.stat(pathname, (err2, newStats) => {
64
+ if (err2) return;
65
+ if (eventType == 'rename') {
66
+ reStart();
67
+ }
68
+ if (eventType == 'change') {
69
+ fs.readFile(pathname, (err2, data) => {
70
+ if (err2) return;
71
+ // 计算内容哈希,首次读取时保存哈希,之后比较
72
+ var hash = crypto.createHash('sha256').update(data).digest('hex');
73
+ if (!stats._contentHash) {
74
+ stats._contentHash = hash;
75
+ return;
76
+ }
77
+ // 内容未变则不重启
78
+ if (stats._contentHash === hash) return;
79
+ // 更新保存的状态与哈希,避免重复触发
80
+ stats._contentHash = hash;
81
+ reStart();
82
+ });
83
+ }
84
+ });
63
85
  } else if (stats.isDirectory()) {
64
86
  // 如果是文件夹,则判断是否删除或者新增
65
87
  if (eventType == 'rename') {
@@ -19,7 +19,7 @@ watchFiles.forEach(dirname => {
19
19
  /**
20
20
  * 重启服务
21
21
  */
22
- startServer();
22
+ startServer('rebuild');
23
23
  });
24
24
  // 杀掉进程
25
25
  childProcess.kill('SIGTERM');
@@ -31,7 +31,7 @@ watchFiles.forEach(dirname => {
31
31
  /**
32
32
  * 启动服务
33
33
  */
34
- module.exports = function startServer() {
34
+ function startServer(status) {
35
35
  var spinner = ora('Starting dev server... \n');
36
36
  spinner.start();
37
37
  childProcess = spawn(
@@ -40,7 +40,7 @@ module.exports = function startServer() {
40
40
  {
41
41
  env: Object.assign({}, process.env, {
42
42
  NODE_ENV: 'development', // 根据需要修改或添加其它环境变量
43
- // EXAMPLE_VAR: 'value'
43
+ NODE_STATUS: status || 'build'
44
44
  }),
45
45
  stdio: 'pipe'
46
46
  }
@@ -82,3 +82,6 @@ module.exports = function startServer() {
82
82
  console.log(`======= Service Closed (code: ${exitCode}) ======`);
83
83
  });
84
84
  }
85
+
86
+
87
+ module.exports = startServer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qmzreact",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "前端react本地启动服务和打包工具",
5
5
  "author": {
6
6
  "name": "qmz",