Fixing jenkins release build
[aai/sparky-fe.git] / gulpfile.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 'use strict';
25
26 var localPath = require('path');
27 var gulp = require('gulp');
28 var gulpHelpers = require('gulp-helpers');
29 var taskMaker = gulpHelpers.taskMaker(gulp);
30 var runSequence = gulpHelpers.framework('run-sequence');
31 var gulpCssUsage = require('gulp-css-usage').default;
32 var webpack = require('webpack');
33 var WebpackDevServer = require('webpack-dev-server');
34 var devWebpackConfig = require('./webpack.devConfig.js');
35 var webpackConfig = require('./webpack.config');
36
37 let appName = 'aai';
38 let dist = 'dist/';
39
40 let path = {
41         output: dist,
42         aaiOutput: dist + '/aai/',
43         assets: './resources/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}',
44         json: './src/**/*.json',
45         aaiIndex: './src/index.html',
46         scss: './resources/scss/**/*.scss',
47         aaiCss: dist + '/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
55 gulp.task('copy-dev-stuff', callback => {
56         return runSequence(['copy-aai-index.html'], callback);
57 });
58
59 gulp.task('copy-stuff', callback => {
60         return runSequence(['copy-aai-index.html'], callback);
61 });
62
63 gulp.task('dev', callback => {
64         return runSequence('clean', 'copy-dev-stuff', 'webpack-dev-server', callback);
65 });
66
67 // Production build
68 gulp.task('build', callback => {
69         return runSequence('clean', ['copy-stuff'], 'prod', callback);
70 });
71
72 gulp.task('default', ['dev']);
73
74 gulp.task('prod', () => {
75
76         return new Promise((resolve, reject)=> {
77                 // configure webpack for production
78                 let webpackProductionConfig = Object.create(webpackConfig);
79
80                 for (let name in webpackProductionConfig.entry) {
81                         webpackProductionConfig.entry[name] = webpackProductionConfig.entry[name].filter(path => !path.startsWith('webpack'));
82                 }
83
84                 webpackProductionConfig.cache = true;
85                 webpackProductionConfig.output = {
86                         path: localPath.join(__dirname, 'dist'),
87                         publicPath: '/services/aai/webapp/',
88                         filename: '[name].js'
89                 };
90                 webpackProductionConfig.resolveLoader = {
91                         root: [localPath.resolve('.')],
92                         alias: {
93                                 'config-json-loader': 'tools/webpack/config-json-loader/index.js'
94                         }
95                 };
96
97                 // remove source maps
98                 webpackProductionConfig.devtool = undefined;
99                 webpackProductionConfig.module.preLoaders = webpackProductionConfig.module.preLoaders.filter(preLoader => preLoader.loader != 'source-map-loader');
100                 webpackProductionConfig.module.loaders.forEach(loader => {
101                         if (loader.loaders && loader.loaders[0] === 'style') {
102                                 loader.loaders = loader.loaders.map(loaderName => loaderName.replace('?sourceMap', ''));
103                         }
104                 });
105
106                 webpackProductionConfig.module.loaders.push({test: /config.json$/, loaders: ['config-json-loader']});
107                 webpackProductionConfig.eslint = {
108                         configFile: './.eslintrc',
109                         failOnError: true
110                 };
111                 webpackProductionConfig.plugins = [
112                         new webpack.DefinePlugin({
113                                 'process.env': {
114                                         // This has effect on the react lib size
115                                         'NODE_ENV': JSON.stringify('production')
116                                 },
117                                 DEBUG: false,
118                                 DEV: false
119                         }),
120                         new webpack.optimize.DedupePlugin(),
121                         new webpack.optimize.UglifyJsPlugin()
122                 ];
123
124                 // run production build
125                 webpack(webpackProductionConfig, function (err, stats) {
126                         console.log('[webpack:build]', stats.toString());
127                         if (err || stats.hasErrors()) {
128                                 console.log('bundleJS : Failure!!');
129                                 reject();
130                         }
131                         else {
132                                 console.log('bundleJS : Done');
133                                 resolve();
134                         }
135                 });
136         });
137
138 });
139
140 gulp.task('webpack-dev-server', () => {
141         let myConfig = Object.create(devWebpackConfig);
142
143         // Start a webpack-dev-server
144         let server = new WebpackDevServer(webpack(myConfig), myConfig.devServer);
145         server.listen(myConfig.devServer.port, '0.0.0.0', err => {
146                 if (err) {
147                         throw new Error('webpack-dev-server' + err);
148                 }
149         });
150 });

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use