aiot-toolkit 1.0.20-importfile-dev.2 → 1.0.20

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.
@@ -1,2 +1,136 @@
1
- "use strict";const webpack=require("webpack"),{setCustomConfig:setCustomConfig,colorconsole:colorconsole}=require("@aiot-toolkit/shared-utils"),genWebpackConf=require("../../gen-webpack-conf"),{summaryErrors:summaryErrors,summaryWarnings:summaryWarnings}=require("./utils");let watching=null;function showVersion(){const o=require("../../package.json").version,r=require("@babel/core/package.json").version,e=require("webpack/package.json").version;colorconsole.info(`aiot-toolkit: ${o}; babel: ${r}; webpack: ${e};`)}showVersion(),module.exports.compile=function(o,r,e,s={}){const n=s.onerror;return new Promise(((c,t)=>{function a(o,r){if(o&&(n&&n(o),colorconsole.error(o)),r){if(r.hasErrors()||r.hasWarnings()){const o=summaryErrors(r),e=summaryWarnings(r);n&&n(o),colorconsole.error(o),colorconsole.warn(e)}r.hasErrors()&&(process.exitCode=1)}}colorconsole.attach(s.log),setCustomConfig(s.cwd),process.env.NODE_PLATFORM=o,process.env.NODE_PHASE=r;const i="prod"===r?"production":"development";try{const o=genWebpackConf(s,i);if(e){const r=webpack(o);watching=r.watch({aggregateTimeout:300},((o,r)=>{a(o,r),c({compileError:o,stats:r,watching:watching})}))}else webpack(o,((o,r)=>{a(o,r),c({compileError:o,stats:r})}))}catch(o){t(o)}}))},module.exports.stopWatch=function(){return new Promise((o=>{watching?watching.close((()=>{watching=null,o({stopWatchError:null})})):o({stopWatchError:"no watching"})}))};
1
+ "use strict";
2
+
3
+ /*
4
+ * Copyright (C) 2017, hapjs.org. All rights reserved.
5
+ */
6
+ const webpack = require('webpack');
7
+
8
+ const {
9
+ setCustomConfig,
10
+ colorconsole
11
+ } = require('@aiot-toolkit/shared-utils');
12
+
13
+ const genWebpackConf = require('../../gen-webpack-conf');
14
+
15
+ const {
16
+ summaryErrors,
17
+ summaryWarnings
18
+ } = require('./utils'); // webpack watch 模式返回的watching实例
19
+
20
+
21
+ let watching = null;
22
+
23
+ function showVersion() {
24
+ const toolkitVer = require('../../package.json').version;
25
+
26
+ const babelVer = require('@babel/core/package.json').version;
27
+
28
+ const webpackVer = require('webpack/package.json').version;
29
+
30
+ colorconsole.info(`aiot-toolkit: ${toolkitVer}; babel: ${babelVer}; webpack: ${webpackVer};`);
31
+ }
32
+
33
+ showVersion();
34
+ /**
35
+ * 调用 webpack 进行编译
36
+ *
37
+ * @module compile
38
+ * @param {String} platform - 目标平台: native
39
+ * @param {dev|prod} mode - 编译模式: dev、prod
40
+ * @param {Boolean} watch - 是否监听
41
+ * @param {Object} [options={}] - 动态生成 webpack 配置项的参数对象
42
+ * @param {String} [options.cwd] - 工作目录
43
+ * @param {Writable} [options.log] - 日志输出流
44
+ * @param {String} [options.originType] - 打包来源,ide|cmd
45
+ * @param {Function} [options.onerror] - 错误回调函数
46
+ * @param {String} [options.setPreviewPkgPath] - 预览包保存路径,由IDE传入
47
+ * @returns {Promise} - 返回成功与否的信息
48
+ */
49
+
50
+ module.exports.compile = function compile(platform, mode, watch, options = {}) {
51
+ const errCb = options.onerror;
52
+ return new Promise((resolve, reject) => {
53
+ colorconsole.attach(options.log);
54
+ setCustomConfig(options.cwd); // IMPORTANT: set env variables before generating webpack config
55
+
56
+ process.env.NODE_PLATFORM = platform;
57
+ process.env.NODE_PHASE = mode;
58
+
59
+ function compilationCallback(err, stats) {
60
+ if (err) {
61
+ errCb && errCb(err);
62
+ colorconsole.error(err);
63
+ }
64
+
65
+ if (stats) {
66
+ if (stats.hasErrors() || stats.hasWarnings()) {
67
+ const message = summaryErrors(stats);
68
+ const warningMsg = summaryWarnings(stats);
69
+ errCb && errCb(message);
70
+ colorconsole.error(message);
71
+ colorconsole.warn(warningMsg);
72
+ }
73
+
74
+ if (stats.hasErrors()) {
75
+ process.exitCode = 1;
76
+ }
77
+ }
78
+ }
79
+
80
+ const webpackMode = mode === 'prod' ? 'production' : 'development';
81
+
82
+ try {
83
+ const webpackConfig = genWebpackConf(options, webpackMode);
84
+
85
+ if (watch) {
86
+ const compiler = webpack(webpackConfig);
87
+ watching = compiler.watch({
88
+ aggregateTimeout: 300
89
+ }, (err, stats) => {
90
+ compilationCallback(err, stats);
91
+ resolve({
92
+ compileError: err,
93
+ stats,
94
+ watching
95
+ });
96
+ });
97
+ } else {
98
+ webpack(webpackConfig, (err, stats) => {
99
+ compilationCallback(err, stats);
100
+ resolve({
101
+ compileError: err,
102
+ stats
103
+ });
104
+ });
105
+ }
106
+ } catch (err) {
107
+ reject(err);
108
+ }
109
+ });
110
+ };
111
+ /**
112
+ * 停止 webpack watch监听
113
+ *
114
+ * @module stopWatch
115
+ * @returns {Promise} - 返回成功与否的信息
116
+ */
117
+
118
+
119
+ module.exports.stopWatch = function () {
120
+ return new Promise(resolve => {
121
+ if (watching) {
122
+ watching.close(() => {
123
+ watching = null;
124
+ resolve({
125
+ stopWatchError: null
126
+ });
127
+ });
128
+ return;
129
+ }
130
+
131
+ resolve({
132
+ stopWatchError: 'no watching'
133
+ });
134
+ });
135
+ };
2
136
  //# sourceMappingURL=compile.js.map