[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / tools / gulp / tasks / prod.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 'use strict';
17
18 let gulp, replace, Promise, webpack, webpackProductionConfig;
19
20 const supportedLanguages = ['en'];
21
22 function start(options) {
23
24         let promises = [buildIndex(options)];
25         supportedLanguages.forEach(function (lang) {
26                 promises.push(bundleJS(options, lang));
27         });
28         return Promise.all(promises);
29 }
30
31 function bundleJS(options, lang) {
32         return new Promise(function (resolve, reject) {
33                 let prodConfig = webpackProductionConfig;
34                 prodConfig.resolve.alias.i18nJson = options.outDir + '/i18n/' + lang + '/locale.json';
35                 prodConfig.output.filename = jsFileByLang(options.outFileName, lang);
36                 webpack(prodConfig, function (err, stats) {
37                         console.log('[webpack:build]', stats.toString());
38                         if (err || stats.hasErrors()) {
39                                 console.log('bundleJS : Failure!!', '\n         -language: ', lang);
40                                 reject(err || stats.toJson().errors);
41                         }
42                         else {
43                                 console.log('bundleJS : Done', '\n              -language: ', lang);
44                                 resolve();
45                         }
46                 });
47         });
48 }
49
50 function buildIndex(options) {
51
52         return new Promise(function (resolve, reject) {
53
54                 // gulp.src returns a stream object
55                 gulp.src(options.outDir + '/index.html')
56                         .pipe(replace(/\/\/<!--prod:delete-->(.|[\r\n])*?<!--\/prod:delete-->/g, ''))//in script occurrences.
57                         .pipe(replace(/<!--prod:delete-->(.|[\r\n])*?<!--\/prod:delete-->/g, ''))//out of script occurrences.
58                         .pipe(replace(/<!--prod:add(-->)?/g, ''))
59                         .pipe(replace(/\/\/<!--prod:supported-langs-->(.|[\r\n])*?<!--\/prod:supported-langs-->/g, supportedLanguages.map(function (val) {
60                                 return "'" + val + "'";
61                         }).toString()))
62                         .pipe(gulp.dest(options.outDir))
63                         .on('end', function () {
64                                 console.log('buildIndex : Done');
65                                 resolve();
66                         })
67                         .on('error', function (e) {
68                                 console.log('buildIndex : Failure!!');
69                                 reject(e);
70                         });
71         });
72
73 }
74
75 function jsFileByLang(fileName, lang) {
76         return fileName.replace(/.js$/, '_' + lang + '.js');
77 }
78
79 /**
80  * @param options
81  * @param options.outFileName optional <default build>
82  */
83 function prodTask(options) {
84         gulp = require('gulp');
85         replace = require('gulp-replace');
86         Promise = require('bluebird');
87         webpack = require('webpack');
88
89         webpackProductionConfig = require('../../../webpack.production');
90         webpackProductionConfig.module.rules = webpackProductionConfig.module.rules.filter(rule => ((rule.enforce !== 'pre') || (rule.enforce === 'pre' && rule.loader !== 'source-map-loader')));
91         webpackProductionConfig.module.rules.forEach(loader => {
92                 if (loader.use && loader.use[0].loader === 'style-loader') {
93                         loader.use = loader.use.map(loaderObj => loaderObj.loader.replace('?sourceMap', ''));
94                 }
95         });
96
97
98         webpackProductionConfig.module.rules.push({test: /config.json$/, use: [{loader:'config-json-loader'}]});
99
100         return start({
101                 outFileName: options.outFileName || '[name].js',
102                 outDir: options.outDir
103         });
104 }
105
106 module.exports = prodTask;