ab623507433bf2e2abdfbb569fa99cd8f2f1dec0
[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         wardest: dist
50 };
51
52 taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
53 taskMaker.defineTask('copy', {taskName: 'copy-aai-index.html', src: path.aaiIndex, dest: path.output, rename: 'index.html'});
54 taskMaker.defineTask('copy', {taskName: 'copy-sa-index.html', src: path.saIndex, dest: path.saOutput, rename: 'index.html'});
55 /** Uncomment the loine below to generate a .war file with a local build */
56 // taskMaker.defineTask('compress', {taskName: 'compress-war', src: path.war, filename: appName + '.war', dest: path.wardest})
57
58 gulp.task('copy-dev-stuff', callback => {
59         return runSequence(['copy-aai-index.html', 'copy-sa-index.html'], callback);
60 });
61
62 gulp.task('copy-stuff', callback => {
63         return runSequence(['copy-aai-index.html', 'copy-sa-index.html'], callback);
64 });
65
66 gulp.task('dev', callback => {
67         return runSequence('clean', 'copy-dev-stuff', 'webpack-dev-server', callback);
68 });
69
70 // Production build
71 gulp.task('build', callback => {
72         return runSequence('clean', ['copy-stuff'], 'prod', callback);
73         /** Uncomment the loine below to generate a .war file with a local build */
74         //return runSequence('clean', ['copy-stuff'], 'prod', 'compress-war', callback);
75 });
76
77
78 gulp.task('default', ['dev']);
79
80 gulp.task('prod', () => {
81
82         return new Promise((resolve, reject)=> {
83                 // configure webpack for production
84                 let webpackProductionConfig = Object.create(webpackConfig);
85
86                 for (let name in webpackProductionConfig.entry) {
87                         webpackProductionConfig.entry[name] = webpackProductionConfig.entry[name].filter(path => !path.startsWith('webpack'));
88                 }
89
90                 webpackProductionConfig.cache = true;
91                 webpackProductionConfig.output = {
92                         path: localPath.join(__dirname, 'dist'),
93                         publicPath: '',
94                         filename: '[name].js'
95                 };
96                 webpackProductionConfig.resolveLoader = {
97                         root: [localPath.resolve('.')],
98                         alias: {
99                                 'config-json-loader': 'tools/webpack/config-json-loader/index.js'
100                         }
101                 };
102
103                 // remove source maps
104                 webpackProductionConfig.devtool = undefined;
105                 webpackProductionConfig.module.preLoaders = webpackProductionConfig.module.preLoaders.filter(preLoader => preLoader.loader != 'source-map-loader');
106                 webpackProductionConfig.module.loaders.forEach(loader => {
107                         if (loader.loaders && loader.loaders[0] === 'style') {
108                                 loader.loaders = loader.loaders.map(loaderName => loaderName.replace('?sourceMap', ''));
109                         }
110                 });
111
112                 webpackProductionConfig.module.loaders.push({test: /config.json$/, loaders: ['config-json-loader']});
113                 webpackProductionConfig.eslint = {
114                         configFile: './.eslintrc',
115                         failOnError: true
116                 };
117                 webpackProductionConfig.plugins = [
118                         new webpack.DefinePlugin({
119                                 'process.env.NODE_ENV': JSON.stringify('production')
120                           }),
121                         new webpack.optimize.DedupePlugin(),
122                         new webpack.optimize.UglifyJsPlugin()
123                 ];
124
125                 // run production build
126                 webpack(webpackProductionConfig, function (err, stats) {
127                         console.log('[webpack:build]', stats.toString());
128                         if (err || stats.hasErrors()) {
129                                 console.log('bundleJS : Failure!!');
130                                 reject();
131                         }
132                         else {
133                                 console.log('bundleJS : Done');
134                                 resolve();
135                         }
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 });