blob: 509bec857abcd09e0bf33bea413b97435f27d785 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
AviZi280f8012017-06-09 02:39:56 +030016'use strict';
Michael Landoefa037d2017-02-19 12:57:33 +020017
Avi Zivb8e2faf2017-07-18 19:45:38 +030018let gulp, replace, Promise, webpack, webpackProductionConfig,cloneDeep, tap;
19let langs = [];
Michael Landoefa037d2017-02-19 12:57:33 +020020
Avi Zivb8e2faf2017-07-18 19:45:38 +030021/*
22Runs the webpack build.
23Will first seach for the resource bundles to see how many languages are supported and then run a build per langauage
24 */
25function buildWebPackForLanguage(prodConfig, lang) {
Michael Landoefa037d2017-02-19 12:57:33 +020026 return new Promise(function (resolve, reject) {
Michael Landoefa037d2017-02-19 12:57:33 +020027 webpack(prodConfig, function (err, stats) {
Avi Zivb8e2faf2017-07-18 19:45:38 +030028 console.log('[webpack:build ' + prodConfig.output.filename + ']', stats.toString());
Michael Landoefa037d2017-02-19 12:57:33 +020029 if (err || stats.hasErrors()) {
Avi Zivb8e2faf2017-07-18 19:45:38 +030030 console.log('webpack:build : Failure!! ' + prodConfig.output.filename + ']');
Michael Landoefa037d2017-02-19 12:57:33 +020031 reject(err || stats.toJson().errors);
32 }
33 else {
Avi Zivb8e2faf2017-07-18 19:45:38 +030034 console.log('webpack:build : Done ' + prodConfig.output.filename + ']');
Michael Landoefa037d2017-02-19 12:57:33 +020035 resolve();
36 }
37 });
38 });
39}
Avi Zivb8e2faf2017-07-18 19:45:38 +030040/*
41 // this will check in the src directory which language bundles we have and will
42 // create the array to that we can run a webpack build per language afterwards
43 */
44function getSupportedLanguages(options) {
45 return new Promise((resolve, reject) => {
46 gulp.src(options.i18nBundles)
47 .pipe(tap(function(file) {
48 let languageStartIndex = file.path.lastIndexOf('i18n') + 5;
49 let languageStr = file.path.indexOf('.json') - languageStartIndex;
50 let currentLang = file.path.substr(languageStartIndex, languageStr);
51 console.log('Found bundle ' + file.path + ' for [' + currentLang + ']');
52 langs[currentLang] = file.path;
53 }))
Michael Landoefa037d2017-02-19 12:57:33 +020054 .pipe(gulp.dest(options.outDir))
55 .on('end', function () {
Michael Landoefa037d2017-02-19 12:57:33 +020056 resolve();
57 })
58 .on('error', function (e) {
Avi Zivb8e2faf2017-07-18 19:45:38 +030059 console.log('getLanguages : Failure!!');
Michael Landoefa037d2017-02-19 12:57:33 +020060 reject(e);
61 });
62 });
Michael Landoefa037d2017-02-19 12:57:33 +020063}
64
65/**
66 * @param options
67 * @param options.outFileName optional <default build>
68 */
69function prodTask(options) {
Michael Landoefa037d2017-02-19 12:57:33 +020070 gulp = require('gulp');
71 replace = require('gulp-replace');
72 Promise = require('bluebird');
73 webpack = require('webpack');
Avi Zivb8e2faf2017-07-18 19:45:38 +030074 cloneDeep = require('lodash/cloneDeep');
75 tap = require('gulp-tap');
AviZi280f8012017-06-09 02:39:56 +030076
Avi Zivb8e2faf2017-07-18 19:45:38 +030077
78 // updating webpack for the production build. no need for sourcemaps in this case.
AviZi280f8012017-06-09 02:39:56 +030079 webpackProductionConfig = require('../../../webpack.production');
Avi Zivb8e2faf2017-07-18 19:45:38 +030080
81 // get the languages so that we can bulid per language with the correct bundle
82 let getLanguages =getSupportedLanguages(options);
83 // this will run a webpack build per language
84 return getLanguages.then(() => {
85 let promises = [];
86 for (var lang in langs) {
87 let prodConfig = cloneDeep(webpackProductionConfig);
88 prodConfig.resolve.alias.i18nJson = langs[lang];
89 prodConfig.output.filename = (options.outFileName || '[name].js').replace(/.js$/, '_' + lang + '.js');
90 promises.push(buildWebPackForLanguage(prodConfig, lang));
AviZi280f8012017-06-09 02:39:56 +030091 }
Avi Zivb8e2faf2017-07-18 19:45:38 +030092 return Promise.all(promises);
AviZi280f8012017-06-09 02:39:56 +030093 });
94
Michael Landoefa037d2017-02-19 12:57:33 +020095}
96
97module.exports = prodTask;