Initial coomit for AAI-UI(sparky-fe)
[aai/sparky-fe.git] / gulpfile.js
1 /*
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 'use strict';
27
28 var localPath = require('path');
29 var gulp = require('gulp');
30 var gulpHelpers = require('gulp-helpers');
31 var taskMaker = gulpHelpers.taskMaker(gulp);
32 var runSequence = gulpHelpers.framework('run-sequence');
33 var gulpCssUsage = require('gulp-css-usage').default;
34 var webpack = require('webpack');
35 var WebpackDevServer = require('webpack-dev-server');
36 var devWebpackConfig = require('./webpack.devConfig.js');
37 var webpackConfig = require('./webpack.config');
38
39 let appName = 'aai';
40 let dist = 'dist/';
41
42 let path = {
43         output: dist,
44         aaiOutput: dist + '/aai/',
45         assets: './resources/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}',
46         json: './src/**/*.json',
47         aaiIndex: './src/index.html',
48         scss: './resources/scss/**/*.scss',
49         aaiCss: dist + '/css',
50         war: [dist + '**/*.html', dist + '**/*.js', dist + '**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '**/*.json', 'webapp/**'],
51         wardest: dist
52 };
53
54 taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
55 taskMaker.defineTask('copy', {taskName: 'copy-aai-index.html', src: path.aaiIndex, dest: path.output, rename: 'index.html'});
56
57 gulp.task('copy-dev-stuff', callback => {
58         return runSequence(['copy-aai-index.html'], callback);
59 });
60
61 gulp.task('copy-stuff', callback => {
62         return runSequence(['copy-aai-index.html'], callback);
63 });
64
65 gulp.task('dev', callback => {
66         return runSequence('clean', 'copy-dev-stuff', 'webpack-dev-server', callback);
67 });
68
69 // Production build
70 gulp.task('build', callback => {
71         return runSequence('clean', ['copy-stuff'], 'prod', callback);
72 });
73
74 gulp.task('default', ['dev']);
75
76 gulp.task('prod', () => {
77
78         return new Promise((resolve, reject)=> {
79                 // configure webpack for production
80                 let webpackProductionConfig = Object.create(webpackConfig);
81
82                 for (let name in webpackProductionConfig.entry) {
83                         webpackProductionConfig.entry[name] = webpackProductionConfig.entry[name].filter(path => !path.startsWith('webpack'));
84                 }
85
86                 webpackProductionConfig.cache = true;
87                 webpackProductionConfig.output = {
88                         path: localPath.join(__dirname, 'dist'),
89                         publicPath: '/services/aai/webapp/',
90                         filename: '[name].js'
91                 };
92                 webpackProductionConfig.resolveLoader = {
93                         root: [localPath.resolve('.')],
94                         alias: {
95                                 'config-json-loader': 'tools/webpack/config-json-loader/index.js'
96                         }
97                 };
98
99                 // remove source maps
100                 webpackProductionConfig.devtool = undefined;
101                 webpackProductionConfig.module.preLoaders = webpackProductionConfig.module.preLoaders.filter(preLoader => preLoader.loader != 'source-map-loader');
102                 webpackProductionConfig.module.loaders.forEach(loader => {
103                         if (loader.loaders && loader.loaders[0] === 'style') {
104                                 loader.loaders = loader.loaders.map(loaderName => loaderName.replace('?sourceMap', ''));
105                         }
106                 });
107
108                 webpackProductionConfig.module.loaders.push({test: /config.json$/, loaders: ['config-json-loader']});
109                 webpackProductionConfig.eslint = {
110                         configFile: './.eslintrc',
111                         failOnError: true
112                 };
113                 webpackProductionConfig.plugins = [
114                         new webpack.DefinePlugin({
115                                 'process.env': {
116                                         // This has effect on the react lib size
117                                         'NODE_ENV': JSON.stringify('production')
118                                 },
119                                 DEBUG: false,
120                                 DEV: false
121                         }),
122                         new webpack.optimize.DedupePlugin(),
123                         new webpack.optimize.UglifyJsPlugin()
124                 ];
125
126                 // run production build
127                 webpack(webpackProductionConfig, function (err, stats) {
128                         console.log('[webpack:build]', stats.toString());
129                         if (err || stats.hasErrors()) {
130                                 console.log('bundleJS : Failure!!');
131                                 reject();
132                         }
133                         else {
134                                 console.log('bundleJS : Done');
135                                 resolve();
136                         }
137                 });
138         });
139
140 });
141
142 gulp.task('webpack-dev-server', () => {
143         let myConfig = Object.create(devWebpackConfig);
144
145         // Start a webpack-dev-server
146         let server = new WebpackDevServer(webpack(myConfig), myConfig.devServer);
147         server.listen(myConfig.devServer.port, '0.0.0.0', err => {
148                 if (err) {
149                         throw new Error('webpack-dev-server' + err);
150                 }
151         });
152 });