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

© 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