Updating to the optimized version
[aai/sparky-fe.git] / gulpfile.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 'use strict';
22
23 var localPath = require('path');
24 var gulp = require('gulp');
25 var gulpHelpers = require('gulp-helpers');
26 var taskMaker = gulpHelpers.taskMaker(gulp);
27 var runSequence = gulpHelpers.framework('run-sequence');
28 var gulpCssUsage = require('gulp-css-usage').default;
29 var webpack = require('webpack');
30 var WebpackDevServer = require('webpack-dev-server');
31 var devWebpackConfig = require('./webpack.devConfig.js');
32 var webpackConfig = require('./webpack.config');
33
34 let appName = 'aai';
35 let dist = 'dist/';
36
37 let path = {
38         output: dist,
39         aaiOutput: dist + '/aai/',
40         saOutput: dist + '/editAttributes/',
41         assets: './resources/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}',
42         json: './src/**/*.json',
43         aaiIndex: './src/index.html',
44         saIndex: './src/editAttributes/index.html',
45         scss: './resources/scss/**/*.scss',
46         aaiCss: dist + '/css',
47         saCss: dist + '/editAttributes/css',
48         war: [dist + '**/*.html', dist + '**/*.js', dist + '**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '**/*.json', 'webapp/**'],
49         bundleSrc:[dist + '**/*.map'],
50         wardest: dist
51 };
52
53 taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
54 taskMaker.defineTask('copy', {taskName: 'copy-aai-index.html', src: path.aaiIndex, dest: path.output, rename: 'index.html'});
55 taskMaker.defineTask('copy', {taskName: 'copy-sa-index.html', src: path.saIndex, dest: path.saOutput, rename: 'index.html'});
56 taskMaker.defineTask('copy', {taskName: 'copy-sa-index.html', src: path.saIndex, dest: path.saOutput, rename: 'index.html'});
57 taskMaker.defineTask('copy', {taskName: 'copy-map-file', src: path.bundleSrc, dest: path.output, rename: 'mappingFile'});
58 taskMaker.defineTask('clean', {taskName: 'clean-map-file', src: path.bundleSrc});
59 /** Uncomment the loine below to generate a .war file with a local build */
60 // taskMaker.defineTask('compress', {taskName: 'compress-war', src: path.war, filename: appName + '.war', dest: path.wardest})
61
62 gulp.task('copy-dev-stuff', callback => {
63         return runSequence(['copy-aai-index.html', 'copy-sa-index.html'], callback);
64 });
65
66 gulp.task('copy-stuff', callback => {
67         return runSequence(['copy-aai-index.html', 'copy-sa-index.html'], callback);
68 });
69
70 gulp.task('dev', callback => {
71         return runSequence('clean', 'copy-dev-stuff', 'webpack-dev-server', callback);
72 });
73
74 // Production build
75 gulp.task('build', callback => {
76         return runSequence('clean', ['copy-stuff'], 'prod', 'copy-map-file', 'clean-map-file', callback);
77         /** Uncomment the loine below to generate a .war file with a local build */
78         //return runSequence('clean', ['copy-stuff'], 'prod', 'compress-war', callback);
79 });
80
81
82 gulp.task('default', ['dev']);
83
84 gulp.task('prod', () => {
85
86         return new Promise((resolve, reject)=> {
87                 // configure webpack for production
88                 let webpackProductionConfig = webpackConfig;
89                 webpack(webpackProductionConfig, function (err, stats) {
90                         console.log('[webpack:build]', stats.toString());
91                         if (err || stats.hasErrors()) {
92                                 console.log('bundleJS : Failure!!');
93                                 reject();
94                         }
95                         else {
96                                 console.log('bundleJS : Done');
97                                 resolve();
98                         }
99                 });
100         });
101
102 });
103
104 gulp.task('webpack-dev-server', () => {
105         let myConfig = Object.create(devWebpackConfig);
106
107         // Start a webpack-dev-server
108         let server = new WebpackDevServer(webpack(myConfig), myConfig.devServer);
109         server.listen(myConfig.devServer.port, '0.0.0.0', err => {
110                 if (err) {
111                         throw new Error('webpack-dev-server' + err);
112                 }
113         });
114 });