2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 var path = require('path');
24 var gulp = require('gulp');
25 var gulpHelpers = require('gulp-helpers');
26 var taskMaker = gulpHelpers.taskMaker(gulp);
27 var _ = gulpHelpers.framework('_');
28 var runSequence = gulpHelpers.framework('run-sequence');
29 var i18nTask = require('./tools/gulp/tasks/i18n');
30 var prodTask = require('./tools/gulp/tasks/prod');
31 var gulpCssUsage = require('gulp-css-usage').default;
32 var webpack = require('webpack');
33 var WebpackDevServer = require('webpack-dev-server');
35 var localDevConfig = {};
37 localDevConfig = require('./devConfig');
40 var devConfig = Object.assign({}, require('./devConfig.defaults'), localDevConfig);
41 var webpackConfig = require('./webpack.config');
43 function defineTasks(mode) {
44 let appName = 'onboarding';
45 let dist = 'dist/' + mode + '/';
49 jssource: 'src/**/*.js',
50 jsxsource: 'src/**/*.jsx',
53 assets: './resources/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}',
54 json: './src/**/*.json',
55 index: './src/index.html',
56 heat: './src/heat.html',
58 scss: './resources/scss/**/*.scss',
60 war: [dist + 'index.html', dist + 'punch-outs_en.js', dist + '**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '**/*(config.json|locale.json)', 'tools/gulp/deployment/**', 'webapp-onboarding/**'],
61 heatWar: [dist + 'heat.html', dist + 'heat-validation_en.js', dist + '**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '**/*(config.json|locale.json)', 'webapp-heat-validation/**'],
65 taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
66 taskMaker.defineTask('copy', {taskName: 'copy-assets', src: path.assets, dest: path.output});
67 taskMaker.defineTask('copy', {
68 taskName: 'copy-json',
71 changed: {extension: '.json'}
73 taskMaker.defineTask('copy', {
74 taskName: 'copy-index.html',
79 taskMaker.defineTask('copy', {
80 taskName: 'copy-heat.html',
85 taskMaker.defineTask('sass', {
89 config: {outputStyle: 'compressed'}
91 taskMaker.defineTask('compress', {
92 taskName: 'compress-war',
94 filename: appName + '.war',
97 taskMaker.defineTask('compress', {
98 taskName: 'compress-heat-war',
100 filename: 'heat-validation.war',
103 taskMaker.defineTask('watch', {
104 taskName: 'watch-stuff',
105 src: [path.assets, path.json, path.index, path.heat],
106 tasks: ['copy-stuff']
108 taskMaker.defineTask('watch', {taskName: 'watch-sass', src: path.scss, tasks: ['sass']});
110 gulp.task('copy-stuff', callback => {
111 return runSequence(['copy-assets', 'copy-json', 'copy-index.html', 'copy-heat.html'], callback);
114 gulp.task('i18n', () => {
116 outputPath: path.output,
117 localesPath: path.locales,
120 console.log('i18n Task : Error! ', err);
126 gulp.task('dependencies', () => {
132 gulp.task('dev', callback => {
134 return runSequence('clean', ['i18n', 'copy-stuff'], 'webpack-dev-server', ['watch-stuff'], callback);
138 gulp.task('build', callback => {
140 return runSequence('clean', ['copy-stuff', 'i18n'], 'prod', ['compress-war', 'compress-heat-war'], callback);
143 gulp.task('default', ['dev']);
145 gulp.task('prod', () => {
147 // configure webpack for production
148 let webpackProductionConfig = Object.create(webpackConfig);
150 for (let name in webpackProductionConfig.entry) {
151 webpackProductionConfig.entry[name] = webpackProductionConfig.entry[name].filter(path => !path.startsWith('webpack'));
154 webpackProductionConfig.cache = true;
155 webpackProductionConfig.output = {
156 path: path.join(__dirname, 'dist/prod'),
157 publicPath: '/onboarding/',
158 filename: '[name].js'
160 webpackProductionConfig.resolveLoader = {
161 root: [path.resolve('.')],
163 'config-json-loader': 'tools/webpack/config-json-loader/index.js'
167 // remove source maps
168 webpackProductionConfig.devtool = undefined;
169 webpackProductionConfig.module.preLoaders = webpackProductionConfig.module.preLoaders.filter(preLoader => preLoader.loader != 'source-map-loader');
170 webpackProductionConfig.module.loaders.forEach(loader => {
171 if (loader.loaders && loader.loaders[0] === 'style') {
172 loader.loaders = loader.loaders.map(loaderName => loaderName.replace('?sourceMap', ''));
176 webpackProductionConfig.module.loaders.push({test: /config.json$/, loaders: ['config-json-loader']});
177 webpackProductionConfig.eslint = {
178 configFile: './.eslintrc',
181 webpackProductionConfig.babel = {//TODO: remove this when UglifyJS will support user or
183 presets: ['es2015', 'stage-0', 'react']
185 webpackProductionConfig.plugins = [
186 new webpack.DefinePlugin({
188 // This has effect on the react lib size
189 'NODE_ENV': JSON.stringify('production')
194 new webpack.optimize.DedupePlugin(),
195 new webpack.optimize.UglifyJsPlugin()
198 // run production build
200 webpackProductionConfig,
206 if (err && err.stack) {
207 console.error(err, err.stack);
209 throw new Error('Webpack build FAILED');
213 gulp.task('webpack-dev-server', () => {
214 // modify some webpack config options for development
215 let myConfig = Object.create(webpackConfig);
217 myConfig.devServer.setup = server => {
218 let fixture = require('./fixture/fixture');
219 let proxy = require('http-proxy-middleware');
220 let proxyConfigDefaults = {
223 onProxyRes: (proxyRes, req, res) => {
224 let setCookie = proxyRes.headers['set-cookie'];
226 setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
232 (req, res, next) => {
233 let match = req.url.match(/^(.*)_en.js$/);
234 let newUrl = match && match[1] + '.js';
236 console.log(`REWRITING URL: ${req.url} -> ${newUrl}`);
242 enabled: devConfig.useFixture
246 // standalon back-end (proxyTarget) has higher priority, so it should be first
247 if (devConfig.proxyTarget) {
249 proxy(['/api', '/onboarding-api', '/sdc1/feProxy/onboarding-api'], Object.assign({}, proxyConfigDefaults, {
250 target: devConfig.proxyTarget,
252 '/sdc1/feProxy/onboarding-api': '/onboarding-api'
258 // Ecorp environment (proxyATTTarget) has lower priority, so it should be second
259 if (devConfig.proxyATTTarget) {
261 proxy(['/sdc1', '/onboarding-api'], Object.assign({}, proxyConfigDefaults, {
262 target: devConfig.proxyATTTarget,
264 // Workaround for some weird proxy issue
265 '/sdc1/feProxy/onboarding-api': '/sdc1/feProxy/onboarding-api',
266 '/onboarding-api': '/sdc1/feProxy/onboarding-api'
271 server.use(middlewares);
274 // Start a webpack-dev-server
275 let server = new WebpackDevServer(webpack(myConfig), myConfig.devServer);
276 server.listen(myConfig.devServer.port, '0.0.0.0', err => {
278 throw new Error('webpack-dev-server' + err);
284 gulp.task('gulp-css-usage', callback => {
285 return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: 'dist/dev/css/style.css', babylon: ['objectRestSpread']}));
288 gulp.task('css-usage', callback => {
290 runSequence('sass', 'gulp-css-usage');