Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 1 | /*- |
| 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 | |
| 21 | var gulp, replace, Promise, webpack, webpackProductionConfig; |
| 22 | |
| 23 | var supportedLanguages = ['en']; |
| 24 | |
| 25 | function 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 | |
| 34 | function 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 | |
| 53 | function 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 | |
| 78 | function jsFileByLang(fileName, lang) { |
| 79 | return fileName.replace(/.js$/, '_' + lang + '.js'); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param options |
| 84 | * @param options.outFileName optional <default build> |
| 85 | */ |
| 86 | function 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 | |
| 100 | module.exports = prodTask; |