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