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 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 18 | let gulp, replace, Promise, webpack, webpackProductionConfig; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 20 | const supportedLanguages = ['en']; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | |
| 22 | function start(options) { |
| 23 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 24 | let promises = [buildIndex(options)]; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 25 | supportedLanguages.forEach(function (lang) { |
| 26 | promises.push(bundleJS(options, lang)); |
| 27 | }); |
| 28 | return Promise.all(promises); |
| 29 | } |
| 30 | |
| 31 | function bundleJS(options, lang) { |
| 32 | return new Promise(function (resolve, reject) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 33 | let prodConfig = webpackProductionConfig; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 34 | prodConfig.resolve.alias.i18nJson = options.outDir + '/i18n/' + lang + '/locale.json'; |
| 35 | prodConfig.output.filename = jsFileByLang(options.outFileName, lang); |
| 36 | webpack(prodConfig, function (err, stats) { |
| 37 | console.log('[webpack:build]', stats.toString()); |
| 38 | if (err || stats.hasErrors()) { |
| 39 | console.log('bundleJS : Failure!!', '\n -language: ', lang); |
| 40 | reject(err || stats.toJson().errors); |
| 41 | } |
| 42 | else { |
| 43 | console.log('bundleJS : Done', '\n -language: ', lang); |
| 44 | resolve(); |
| 45 | } |
| 46 | }); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | function buildIndex(options) { |
| 51 | |
| 52 | return new Promise(function (resolve, reject) { |
| 53 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 54 | // gulp.src returns a stream object |
| 55 | gulp.src(options.outDir + '/index.html') |
| 56 | .pipe(replace(/\/\/<!--prod:delete-->(.|[\r\n])*?<!--\/prod:delete-->/g, ''))//in script occurrences. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 57 | .pipe(replace(/<!--prod:delete-->(.|[\r\n])*?<!--\/prod:delete-->/g, ''))//out of script occurrences. |
| 58 | .pipe(replace(/<!--prod:add(-->)?/g, '')) |
| 59 | .pipe(replace(/\/\/<!--prod:supported-langs-->(.|[\r\n])*?<!--\/prod:supported-langs-->/g, supportedLanguages.map(function (val) { |
| 60 | return "'" + val + "'"; |
| 61 | }).toString())) |
| 62 | .pipe(gulp.dest(options.outDir)) |
| 63 | .on('end', function () { |
| 64 | console.log('buildIndex : Done'); |
| 65 | resolve(); |
| 66 | }) |
| 67 | .on('error', function (e) { |
| 68 | console.log('buildIndex : Failure!!'); |
| 69 | reject(e); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | function jsFileByLang(fileName, lang) { |
| 76 | return fileName.replace(/.js$/, '_' + lang + '.js'); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param options |
| 81 | * @param options.outFileName optional <default build> |
| 82 | */ |
| 83 | function prodTask(options) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 84 | gulp = require('gulp'); |
| 85 | replace = require('gulp-replace'); |
| 86 | Promise = require('bluebird'); |
| 87 | webpack = require('webpack'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 88 | |
| 89 | webpackProductionConfig = require('../../../webpack.production'); |
| 90 | webpackProductionConfig.module.rules = webpackProductionConfig.module.rules.filter(rule => ((rule.enforce !== 'pre') || (rule.enforce === 'pre' && rule.loader !== 'source-map-loader'))); |
| 91 | webpackProductionConfig.module.rules.forEach(loader => { |
| 92 | if (loader.use && loader.use[0].loader === 'style-loader') { |
| 93 | loader.use = loader.use.map(loaderObj => loaderObj.loader.replace('?sourceMap', '')); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | |
| 98 | webpackProductionConfig.module.rules.push({test: /config.json$/, use: [{loader:'config-json-loader'}]}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | |
| 100 | return start({ |
| 101 | outFileName: options.outFileName || '[name].js', |
| 102 | outDir: options.outDir |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | module.exports = prodTask; |