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;
20 const supportedLanguages = ['en'];
22 function start(options) {
24 let promises = [buildIndex(options)];
25 supportedLanguages.forEach(function (lang) {
26 promises.push(bundleJS(options, lang));
28 return Promise.all(promises);
31 function bundleJS(options, lang) {
32 return new Promise(function (resolve, reject) {
33 let prodConfig = webpackProductionConfig;
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);
43 console.log('bundleJS : Done', '\n -language: ', lang);
50 function buildIndex(options) {
52 return new Promise(function (resolve, reject) {
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.
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 + "'";
62 .pipe(gulp.dest(options.outDir))
63 .on('end', function () {
64 console.log('buildIndex : Done');
67 .on('error', function (e) {
68 console.log('buildIndex : Failure!!');
75 function jsFileByLang(fileName, lang) {
76 return fileName.replace(/.js$/, '_' + lang + '.js');
81 * @param options.outFileName optional <default build>
83 function prodTask(options) {
84 gulp = require('gulp');
85 replace = require('gulp-replace');
86 Promise = require('bluebird');
87 webpack = require('webpack');
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', ''));
98 webpackProductionConfig.module.rules.push({test: /config.json$/, use: [{loader:'config-json-loader'}]});
101 outFileName: options.outFileName || '[name].js',
102 outDir: options.outDir
106 module.exports = prodTask;