blob: cb6e2514305478488a308ce9b968c05987c5a27a [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
AviZi280f8012017-06-09 02:39:56 +030018let gulp, replace, Promise, webpack, webpackProductionConfig;
Michael Landoefa037d2017-02-19 12:57:33 +020019
AviZi280f8012017-06-09 02:39:56 +030020const supportedLanguages = ['en'];
Michael Landoefa037d2017-02-19 12:57:33 +020021
22function start(options) {
23
AviZi280f8012017-06-09 02:39:56 +030024 let promises = [buildIndex(options)];
Michael Landoefa037d2017-02-19 12:57:33 +020025 supportedLanguages.forEach(function (lang) {
26 promises.push(bundleJS(options, lang));
27 });
28 return Promise.all(promises);
29}
30
31function bundleJS(options, lang) {
32 return new Promise(function (resolve, reject) {
AviZi280f8012017-06-09 02:39:56 +030033 let prodConfig = webpackProductionConfig;
Michael Landoefa037d2017-02-19 12:57:33 +020034 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
50function buildIndex(options) {
51
52 return new Promise(function (resolve, reject) {
53
AviZi280f8012017-06-09 02:39:56 +030054 // 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 Landoefa037d2017-02-19 12:57:33 +020057 .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
75function jsFileByLang(fileName, lang) {
76 return fileName.replace(/.js$/, '_' + lang + '.js');
77}
78
79/**
80 * @param options
81 * @param options.outFileName optional <default build>
82 */
83function prodTask(options) {
Michael Landoefa037d2017-02-19 12:57:33 +020084 gulp = require('gulp');
85 replace = require('gulp-replace');
86 Promise = require('bluebird');
87 webpack = require('webpack');
AviZi280f8012017-06-09 02:39:56 +030088
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 Landoefa037d2017-02-19 12:57:33 +020099
100 return start({
101 outFileName: options.outFileName || '[name].js',
102 outDir: options.outDir
103 });
104}
105
106module.exports = prodTask;