1 |
|
2 | 'use strict';
|
3 | var processPath = process.cwd().replace(/\\/g, "/")
|
4 | , fsExtra = require('fs-extra')
|
5 | , del = require('del');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | fsExtra.copySync('node_modules/@omnia/foundation/tools/TaskRunner/external/node_modules/bin', processPath + '/node_modules/.bin');
|
14 | del.sync('node_modules/@types/node');
|
15 | del.sync('node_modules/@omnia/foundation/node_modules');
|
16 |
|
17 | var gulp = require('gulp')
|
18 | , glob = require('glob')
|
19 | , chokidar = require('chokidar')
|
20 | , webpack = require("webpack")
|
21 | , namedModulesPlugin = require('webpack/lib/NamedModulesPlugin')
|
22 | , gutil = require("gulp-util")
|
23 | , merge = require('webpack-merge')
|
24 | , exec = require('child_process').exec
|
25 | , fs = require('fs')
|
26 | , argv = require('yargs').argv
|
27 | , $ = require('gulp-load-plugins')({
|
28 | pattern: [
|
29 | 'gulp-*'
|
30 | ],
|
31 | rename: {
|
32 | }
|
33 | })
|
34 | , jsonfile = require('jsonfile')
|
35 | , extend = require('deep-extend')
|
36 | , timestamp = require('console-timestamp')
|
37 | , path = require('path')
|
38 | , exec = require('child_process').exec
|
39 | , omniaPlugins = require('./TaskRunner/omf.webpack.plugins.js')
|
40 | , utils = require('./TaskRunner/omf.utils.js');
|
41 |
|
42 | var appConfig = {};
|
43 |
|
44 | gulp.task('fix-build', function (cb) {
|
45 | fsExtra.copySync('node_modules/@omnia/foundation/tools/TaskRunner/external/node_modules/bin', processPath + '/node_modules/.bin');
|
46 | del.sync('node_modules/@types/node');
|
47 | del.sync('node_modules/@omnia/foundation/node_modules');
|
48 | });
|
49 |
|
50 | gulp.task('build', function (cb) {
|
51 | mergeConfig()
|
52 | .then(aotCompile)
|
53 | .then(webpackBundle)
|
54 | .then(function () { console.log(timestamp('[hh:mm:ss]') + ' Build finished'); })
|
55 | });
|
56 |
|
57 | gulp.task('aot-compile', function (cb) {
|
58 | mergeConfig()
|
59 | .then(aotCompile)
|
60 | .then(cb);
|
61 | })
|
62 |
|
63 | gulp.task('webpack-bundle', function (cb) {
|
64 | mergeConfig()
|
65 | .then(webpackBundle)
|
66 | .then(cb);
|
67 | });
|
68 |
|
69 | gulp.task('less', function (cb) {
|
70 | mergeConfig()
|
71 | .then(compileAllLess)
|
72 | .then(cb);
|
73 | });
|
74 |
|
75 | gulp.task('watch-less', function () {
|
76 | process.on('uncaughtException', function (err) {
|
77 | console.log(err);
|
78 | });
|
79 |
|
80 | var watchLessTenantResource = chokidar.watch(appConfig.less.watch, {
|
81 | ignored: /[\/\\]\./,
|
82 | persistent: true
|
83 | });
|
84 |
|
85 | watchLessTenantResource.on('ready', function () {
|
86 | watchLessTenantResource
|
87 | .on('change', function (path) {
|
88 | console.log(utils.getCurrentDateTime() + " : compiling > " + path);
|
89 | compileAllLess();
|
90 | console.log(utils.getCurrentDateTime() + " : done");
|
91 | });
|
92 | });
|
93 | });
|
94 |
|
95 | gulp.task('webpack-watch', function () {
|
96 | var listFilesBundling = [];
|
97 | process.on('uncaughtException', function (err) {
|
98 | console.log(err);
|
99 | });
|
100 |
|
101 | var watchAngular = chokidar.watch(appConfig.webpack.watch, {
|
102 | ignored: /[\/\\]\./,
|
103 | persistent: true
|
104 | });
|
105 |
|
106 | watchAngular.on('ready', function () {
|
107 | watchAngular
|
108 | .on('change', function (filePath) {
|
109 | if (listFilesBundling.indexOf(filePath) === -1) {
|
110 | listFilesBundling.push(filePath);
|
111 | webpackCompile(appConfig.webpack.basePath, appConfig.webpack.flatModules, appConfig.webpack.ignoreFiles, filePath, function () {
|
112 | if (listFilesBundling.indexOf(filePath) > -1)
|
113 | listFilesBundling.splice(listFilesBundling.indexOf(filePath), 1)
|
114 | }, true);
|
115 | }
|
116 | });
|
117 | });
|
118 | });
|
119 |
|
120 | function aotCompile() {
|
121 | return new Promise(function (resolve, reject) {
|
122 | console.log(timestamp('[hh:mm:ss]') + ' Aot compile running...');
|
123 |
|
124 | if (!enableRunAot() && (process.argv.length === 0 || process.argv[process.argv.length - 1] !== '--force-aot')) {
|
125 | console.warn('Aot compile disabled in environment.json');
|
126 | resolve();
|
127 | return;
|
128 | }
|
129 | if (appConfig.aot.cleanBefore && appConfig.aot.cleanBefore.length > 0) {
|
130 | del.sync(appConfig.aot.cleanBefore);
|
131 | }
|
132 | jsonfile.writeFileSync('./omf.tsconfig.aot.json', appConfig.aot.ngc);
|
133 |
|
134 | exec('"node_modules/.bin/ngc" -p ./omf.tsconfig.aot.json', function (err, stdout, stderr) {
|
135 | if (stdout)
|
136 | console.log(stdout);
|
137 | if (stderr)
|
138 | console.log(stderr);
|
139 | if (err !== null) {
|
140 | resolve(err);
|
141 | return;
|
142 | }
|
143 |
|
144 | else {
|
145 | if (appConfig.aot.compileAotTsFiles && appConfig.aot.compileAotTsFiles.length > 0) {
|
146 | del.sync('node_modules/@types/node');
|
147 | gulp.src(appConfig.aot.typesForCompileAotTsFiles.concat(appConfig.aot.compileAotTsFiles))
|
148 | .pipe($.tsc({
|
149 | "target": "es5",
|
150 | "module": "commonjs",
|
151 | "moduleResolution": "node",
|
152 | "declaration": false,
|
153 | "sourceMap": false,
|
154 | "removeComments": false,
|
155 | "noImplicitAny": false,
|
156 | "emitDecoratorMetadata": true,
|
157 | "experimentalDecorators": true,
|
158 | "types": []
|
159 | }))
|
160 | .pipe(gulp.dest('.'))
|
161 | .on('end', function () {
|
162 | doWebpackCompile(resolve);
|
163 | });
|
164 | }
|
165 | else {
|
166 | doWebpackCompile(resolve);
|
167 | }
|
168 | }
|
169 | });
|
170 | })
|
171 |
|
172 | function doWebpackCompile(resolve) {
|
173 | if (appConfig.aot.webpackCompileFiles && appConfig.aot.webpackCompileFiles.length > 0) {
|
174 | var totalFiles = 0;
|
175 | var countLoop = 0;
|
176 | for (var i = 0; i < appConfig.aot.webpackCompileFiles.length; i++) {
|
177 | glob(appConfig.aot.webpackCompileFiles[i], function (err, files) {
|
178 | countLoop++;
|
179 | totalFiles = totalFiles + files.length;
|
180 | if (totalFiles === 0 && countLoop === appConfig.aot.webpackCompileFiles.length) {
|
181 | console.log(timestamp('[hh:mm:ss]') + ' Aot compile finished');
|
182 | resolve();
|
183 | }
|
184 | for (var k = 0; k < files.length; k++) {
|
185 | webpackCompile(appConfig.webpack.basePaths, appConfig.webpack.flatModules, appConfig.webpack.ignoreFiles, files[k], function () {
|
186 | totalFiles = totalFiles - 1;
|
187 | if (totalFiles === 0) {
|
188 | if (appConfig.aot.copyOutputAotFilesToTenantResources && appConfig.aot.copyOutputAotFilesToTenantResources.length > 0) {
|
189 | var count = 0;
|
190 | for (var j = 0; j < appConfig.aot.copyOutputAotFilesToTenantResources.length; j++) {
|
191 | gulp.src(appConfig.aot.copyOutputAotFilesToTenantResources[j].source)
|
192 | .pipe(gulp.dest(appConfig.aot.copyOutputAotFilesToTenantResources[j].target))
|
193 | .on('end', function () {
|
194 | count++;
|
195 | if (count === appConfig.aot.copyOutputAotFilesToTenantResources.length) {
|
196 | if (appConfig.aot.cleanAfter && appConfig.aot.cleanAfter.length > 0) {
|
197 | del.sync(appConfig.aot.cleanAfter);
|
198 | }
|
199 | console.log(timestamp('[hh:mm:ss]') + ' Aot compile finished');
|
200 | resolve();
|
201 | }
|
202 | });
|
203 | }
|
204 | }
|
205 | else {
|
206 | if (appConfig.aot.cleanAfter && appConfig.aot.cleanAfter.length > 0) {
|
207 | del.sync(appConfig.aot.cleanAfter);
|
208 | }
|
209 | console.log(timestamp('[hh:mm:ss]') + ' Aot compile finished');
|
210 | resolve();
|
211 | }
|
212 | }
|
213 | });
|
214 | }
|
215 | });
|
216 | }
|
217 | }
|
218 | else {
|
219 | if (appConfig.aot.cleanAfter && appConfig.aot.cleanAfter.length > 0) {
|
220 | del.sync(appConfig.aot.cleanAfter);
|
221 | }
|
222 | console.log(timestamp('[hh:mm:ss]') + ' Aot compile finished');
|
223 | resolve();
|
224 | }
|
225 | }
|
226 | }
|
227 |
|
228 | function webpackBundle(allowLog) {
|
229 | return new Promise(function (resolve, reject) {
|
230 | console.log(timestamp('[hh:mm:ss]') + ' Webpack bundle running...');
|
231 | var srcfiles = appConfig.webpack.compileFiles;
|
232 | var totalFiles = 0;
|
233 | var countLoop = 0;
|
234 |
|
235 | for (var i = 0; i < srcfiles.length; i++) {
|
236 | glob(srcfiles[i], function (err, files) {
|
237 | countLoop++;
|
238 | totalFiles = totalFiles + files.length;
|
239 | for (var k = 0; k < files.length; k++) {
|
240 | webpackCompile(appConfig.webpack.basePaths, appConfig.webpack.flatModules, appConfig.webpack.ignoreFiles, files[k], function () {
|
241 | totalFiles = totalFiles - 1;
|
242 | if (totalFiles === 0 && countLoop === srcfiles.length) {
|
243 | console.log(timestamp('[hh:mm:ss]') + ' Webpack bundle finished');
|
244 | resolve();
|
245 | }
|
246 | }, allowLog);
|
247 | }
|
248 | });
|
249 | }
|
250 | if (srcfiles.length === 0) {
|
251 | console.log(timestamp('[hh:mm:ss]') + ' Webpack bundle finished');
|
252 | resolve();
|
253 | }
|
254 | })
|
255 | }
|
256 |
|
257 | function compileLess(filePath, callBack) {
|
258 | var filePathFOrmatOS = filePath.replace(/\//g, "\\");
|
259 |
|
260 | gulp.src(filePath)
|
261 | .pipe($.sourcemaps.init())
|
262 | .pipe($.less({}))
|
263 | .pipe($.sourcemaps.write('.'))
|
264 | .pipe(gulp.dest(filePath.substring(0, filePath.lastIndexOf('/'))))
|
265 | .on('end', function () {
|
266 | if (callBack !== undefined)
|
267 | callBack();
|
268 | });
|
269 |
|
270 | }
|
271 |
|
272 | function compileAllLess() {
|
273 | return new Promise(function (resolve, reject) {
|
274 | console.log(timestamp('[hh:mm:ss]') + ' Compile less running...');
|
275 |
|
276 | var totalFiles = 0;
|
277 | var countLoop = 0;
|
278 |
|
279 | for (var i = 0; i < appConfig.less.compile.tenantResource.files.length; i++) {
|
280 | glob(appConfig.less.compile.tenantResource.files[i], function (err, files) {
|
281 | countLoop++;
|
282 | totalFiles = totalFiles + files.length;
|
283 |
|
284 | for (var k = 0; k < files.length; k++) {
|
285 | compileLess(files[k], function () {
|
286 | totalFiles = totalFiles - 1;
|
287 | if (totalFiles === 0 && countLoop === appConfig.less.compile.tenantResource.files.length) {
|
288 | console.log(timestamp('[hh:mm:ss]') + ' Compile less finished');
|
289 | resolve();
|
290 | }
|
291 | });
|
292 | }
|
293 | });
|
294 | }
|
295 | if (appConfig.less.compile.tenantResource.files.length.length === 0) {
|
296 | console.log(timestamp('[hh:mm:ss]') + ' Compile less finished');
|
297 | resolve();
|
298 | }
|
299 | });
|
300 | }
|
301 |
|
302 | function webpackCompile(basePaths, flatModules, ignoreFiles, destPath, callBack, allowLog) {
|
303 | var isSelfResolveModule = false;
|
304 | var isCompile = false;
|
305 | var flatModule = isFlatModule(flatModules, destPath)
|
306 | if (!isMatchIgnoreFiles(ignoreFiles, destPath) && destPath.indexOf('.ngsummary.js') === -1) {
|
307 | let stream = fs.readFileSync(destPath, 'utf8');
|
308 |
|
309 | isCompile = stream.indexOf('webpackJsonp([0],{') !== 0;
|
310 | if (isCompile) {
|
311 | isSelfResolveModule = stream.indexOf('.OmniaControl({') > -1 || stream.indexOf('.OmniaAdminControl({') > -1 || stream.indexOf('.NgModule({') > -1 || stream.indexOf('.Component({') > -1
|
312 | || stream.indexOf('Extensibility_1.TemplateId') > -1 || stream.indexOf('Extensibility_1.OmniaControl') > -1 || stream.indexOf('<<webpack-resolve-module>>') > -1
|
313 | }
|
314 | }
|
315 | if (isCompile) {
|
316 | var namedModulesReplacements = [];
|
317 | for (var i = 0; i < basePaths.length; i++) {
|
318 | namedModulesReplacements.push({
|
319 | pattern: new RegExp(basePaths[i].replace(/\//g, "\\/") + "\\/", 'g'),
|
320 | replace: ""
|
321 | });
|
322 | }
|
323 | namedModulesReplacements.push({
|
324 | pattern: /\/node_modules/g,
|
325 | replace: ""
|
326 | });
|
327 | fs.appendFileSync(destPath, '\r\n //webpack-eof');
|
328 | destPath = "./" + destPath.replace(/[/\\*]/g, "/");
|
329 | var fileName = destPath.substring(destPath.lastIndexOf('/'));
|
330 | webpack(merge(require("./TaskRunner/config/webpack.config.js"), {
|
331 | entry: {
|
332 | 'external': './TaskRunner/config/external.js',
|
333 | 'app': destPath
|
334 | },
|
335 | externals: [
|
336 | function (context, request, callback) {
|
337 | if (destPath.indexOf(request.replace("./", "")) === -1 || destPath.substring(destPath.indexOf(request.replace("./", "")), destPath.length) !== request.replace("./", "")) {
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | return callback(null, "__webpack_require__('" + request.toLowerCase() + "')");
|
344 |
|
345 | }
|
346 | else {
|
347 | callback();
|
348 | }
|
349 | }
|
350 | ],
|
351 |
|
352 | cache: false,
|
353 | output: {
|
354 |
|
355 | libraryTarget: flatModule ? "amd" : "var",
|
356 | pathinfo: true,
|
357 |
|
358 | filename: destPath.replace("./", ''),
|
359 | },
|
360 | plugins: [
|
361 | new webpack.optimize.CommonsChunkPlugin({ name: 'external', filename: 'TaskRunner/config/external.bundle.js' }),
|
362 | new omniaPlugins.NamedModulesPlugin({
|
363 | replacements: namedModulesReplacements
|
364 | }),
|
365 | new omniaPlugins.ReplaceBundlePlugin([
|
366 | {
|
367 | partten: /\/\/# sourceMappingURL[\s\S]+\/\/webpack-eof/g,
|
368 | replacement: function (match) {
|
369 | return '//webpack-eof'
|
370 | }
|
371 | },
|
372 | {
|
373 | partten: /\/\/webpack-eof[\s\S]+}\,\[(.*?)\]/g,
|
374 | replacement: function (match) {
|
375 | return '}},[]'
|
376 | }
|
377 | },
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | {
|
412 | partten: /"use strict";[\s\S]+"use strict";/g,
|
413 | replacement: '"use strict";'
|
414 | },
|
415 | {
|
416 | partten: /\/\* no static exports found[\s\S]+function\(module, exports, __webpack_require__\) {/i,
|
417 | replacement: function (match) { return "function(module, exports, __webpack_require__) {"; }
|
418 | },
|
419 | {
|
420 | partten: /\/\* no static exports found[\s\S]+function\(module, exports\) {/i,
|
421 | replacement: function (match) { return "function(module, exports) {"; }
|
422 | },
|
423 | {
|
424 | partten: /\/\* no static exports found[\s\S]+function\(module, exports\) {/i,
|
425 | replacement: function (match) { return "function(module, exports) {"; }
|
426 | },
|
427 | {
|
428 | partten: new RegExp("__webpack_require__\\(\/\\*(.*)\\)", 'g'),
|
429 | replacement: function (match) {
|
430 | return match
|
431 | .replace("/*!", "'")
|
432 | .replace(/\*\/(.*?)\){1}/i, "')")
|
433 | .replace(/ /g, "")
|
434 | .replace(/'(.*?)'/i, function (requireModulePath) {
|
435 | requireModulePath = requireModulePath.replace(/'/g, "");
|
436 | if (requireModulePath.indexOf(".") === 0) {
|
437 | var requireModulePath = path.resolve(path.dirname(destPath), requireModulePath);
|
438 | requireModulePath = requireModulePath.replace(/\\/g, "/");
|
439 | for (var i = 0; i < basePaths.length; i++) {
|
440 | requireModulePath = requireModulePath.replace(utils.root(basePaths[i]).replace(/\\/g, "/"), "");
|
441 | }
|
442 | requireModulePath = requireModulePath
|
443 | .replace(utils.root('TenantResources').replace(/\\/g, "/") + '/~/', "")
|
444 | .replace(utils.root('.').replace(/\\/g, "/") + '/~/', "")
|
445 | .replace(utils.root('.').replace(/\\/g, "/"), "")
|
446 | .trim("/");
|
447 | if (requireModulePath.indexOf("/") === 0)
|
448 | requireModulePath = requireModulePath.substring(1, requireModulePath.length);
|
449 | if (requireModulePath.indexOf("node_modules/") === 0)
|
450 | requireModulePath = requireModulePath.replace(/node_modules\//i, "");
|
451 | if (destPath.indexOf(requireModulePath + '.js') > -1) {
|
452 | console.log('have recursive import self : ' + destPath);
|
453 | }
|
454 | }
|
455 |
|
456 | return "'" + requireModulePath.toLowerCase().replace("@omnia/foundation/extensibility/typings/", "@omnia/foundation/extensibility/") + "'";
|
457 | });
|
458 | }
|
459 | },
|
460 | {
|
461 | skip: !isSelfResolveModule,
|
462 | partten: /webpackJsonp\(\[0\],{[\s\S]+":/g,
|
463 | replacement: function (match) {
|
464 | var moduleId = match.match(/"(.*?)"/)[1]
|
465 | return "omfExecuteModules.push('" + moduleId + "'); ";
|
466 |
|
467 |
|
468 |
|
469 | },
|
470 | insertToEOF: true
|
471 | },
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 | {
|
481 | partten: /;;/g,
|
482 | replacement: ';'
|
483 | },
|
484 | ])
|
485 | ]
|
486 | }), function (err, stats) {
|
487 | if (err) throw new gutil.PluginError("webpack", err);
|
488 | })
|
489 | .plugin("done", function () {
|
490 | if (allowLog)
|
491 | console.log('bundle completed - ' + destPath);
|
492 | if (callBack !== undefined)
|
493 | callBack();
|
494 | });
|
495 | }
|
496 | else {
|
497 | if (callBack !== undefined)
|
498 | callBack();
|
499 | }
|
500 | }
|
501 |
|
502 | function isMatchIgnoreFiles(ignoreFiles, filePath) {
|
503 | var isMatch = false
|
504 | for (var i = 0; i < ignoreFiles.length; i++) {
|
505 | if (filePath.indexOf(ignoreFiles[i]) > -1) {
|
506 | isMatch = true;
|
507 | return isMatch;
|
508 | }
|
509 | }
|
510 |
|
511 | return isMatch;
|
512 | }
|
513 |
|
514 | function isFlatModule(flatModules, filePath) {
|
515 | var isMatch = false
|
516 | for (var i = 0; i < flatModules.length; i++) {
|
517 | if (filePath.replace(/\\/g, "/").toLowerCase().indexOf(flatModules[i].replace(/\\/g, "/").toLowerCase()) > -1) {
|
518 | isMatch = true;
|
519 | return isMatch;
|
520 | }
|
521 | }
|
522 |
|
523 | return isMatch;
|
524 | }
|
525 |
|
526 | function enableRunAot() {
|
527 | var result = true;
|
528 | var processPath = process.cwd().replace(/\\/g, "/");
|
529 |
|
530 | if (fs.existsSync(processPath + '/environment.json')) {
|
531 | try {
|
532 | var config = require(processPath + '/environment.json');
|
533 | if (config.Angular && config.Angular && config.Angular.AOT && config.Angular.AOT.RunOnBuild === false)
|
534 | result = false
|
535 | }
|
536 | catch (err) {
|
537 | console.log(err);
|
538 | }
|
539 | }
|
540 |
|
541 | return result;
|
542 | }
|
543 |
|
544 | function mergeConfig() {
|
545 | return new Promise(function (resolve, reject) {
|
546 | var processPath = process.cwd().replace(/\\/g, "/");
|
547 | if (fs.existsSync(processPath + '/TaskRunner/omf.task.config.json')) {
|
548 | try {
|
549 | appConfig = require('./TaskRunner/config/app.json');
|
550 | var extendConfig = require(processPath + '/TaskRunner/omf.task.config.json');
|
551 | extend(appConfig, extendConfig);
|
552 |
|
553 | resolve();
|
554 | }
|
555 | catch (err) {
|
556 | console.log('have an exception in omf.task.config.json : ' + err);
|
557 | }
|
558 | }
|
559 | else {
|
560 | appConfig = require('./TaskRunner/config/app.json');
|
561 | resolve();
|
562 | }
|
563 | })
|
564 | }
|