AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 16 | 'use strict'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 18 | let gulp, replace, Promise, webpack, webpackProductionConfig,cloneDeep, tap; |
| 19 | let langs = []; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 21 | /* |
| 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 |
| 24 | */ |
| 25 | function buildWebPackForLanguage(prodConfig, lang) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 26 | return new Promise(function (resolve, reject) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | webpack(prodConfig, function (err, stats) { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 28 | console.log('[webpack:build ' + prodConfig.output.filename + ']', stats.toString()); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 29 | if (err || stats.hasErrors()) { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 30 | console.log('webpack:build : Failure!! ' + prodConfig.output.filename + ']'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 31 | reject(err || stats.toJson().errors); |
| 32 | } |
| 33 | else { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 34 | console.log('webpack:build : Done ' + prodConfig.output.filename + ']'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 35 | resolve(); |
| 36 | } |
| 37 | }); |
| 38 | }); |
| 39 | } |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 40 | /* |
| 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 | */ |
| 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; |
| 53 | })) |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 54 | .pipe(gulp.dest(options.outDir)) |
| 55 | .on('end', function () { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 56 | resolve(); |
| 57 | }) |
| 58 | .on('error', function (e) { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 59 | console.log('getLanguages : Failure!!'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 60 | reject(e); |
| 61 | }); |
| 62 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param options |
| 67 | * @param options.outFileName optional <default build> |
| 68 | */ |
| 69 | function prodTask(options) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 70 | gulp = require('gulp'); |
| 71 | replace = require('gulp-replace'); |
| 72 | Promise = require('bluebird'); |
| 73 | webpack = require('webpack'); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 74 | cloneDeep = require('lodash/cloneDeep'); |
| 75 | tap = require('gulp-tap'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 76 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 77 | |
| 78 | // updating webpack for the production build. no need for sourcemaps in this case. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 79 | webpackProductionConfig = require('../../../webpack.production'); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 80 | |
| 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)); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 91 | } |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 92 | return Promise.all(promises); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 93 | }); |
| 94 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | module.exports = prodTask; |