2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 let gulp, replace, Promise, webpack, webpackProductionConfig,cloneDeep, tap;
22 Runs the webpack build.
23 Will first seach for the resource bundles to see how many languages are supported and then run a build per langauage
25 function buildWebPackForLanguage(prodConfig, lang) {
26 return new Promise(function (resolve, reject) {
27 webpack(prodConfig, function (err, stats) {
28 console.log('[webpack:build ' + prodConfig.output.filename + ']', stats.toString());
29 if (err || stats.hasErrors()) {
30 console.log('webpack:build : Failure!! ' + prodConfig.output.filename + ']');
31 reject(err || stats.toJson().errors);
34 console.log('webpack:build : Done ' + prodConfig.output.filename + ']');
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
44 function 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;
54 .pipe(gulp.dest(options.outDir))
55 .on('end', function () {
58 .on('error', function (e) {
59 console.log('getLanguages : Failure!!');
67 * @param options.outFileName optional <default build>
69 function prodTask(options) {
70 gulp = require('gulp');
71 replace = require('gulp-replace');
72 Promise = require('bluebird');
73 webpack = require('webpack');
74 cloneDeep = require('lodash/cloneDeep');
75 tap = require('gulp-tap');
78 // updating webpack for the production build. no need for sourcemaps in this case.
79 webpackProductionConfig = require('../../../webpack.production');
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(() => {
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));
92 return Promise.all(promises);
97 module.exports = prodTask;