Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | let gulp = require('gulp'); |
| 4 | let gulpHelpers = require('gulp-helpers'); |
| 5 | let replace = require('gulp-replace'); |
| 6 | let taskMaker = gulpHelpers.taskMaker(gulp); |
| 7 | let runSequence = gulpHelpers.framework('run-sequence'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 8 | let gulpCssUsage = require('gulp-css-usage').default; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 9 | |
| 10 | let prodTask = require('./tools/gulp/tasks/prod'); |
| 11 | let i18nTask = require('./tools/gulp/tasks/i18n.js'); |
| 12 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 13 | let jsonConfig = { |
| 14 | "appContextPath" : "/onboarding" |
| 15 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | try { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | jsonConfig = require('./src/sdc-app/config/config.json'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | } catch (e) { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 20 | console.log('could not load config. using default value instead'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | } |
| 22 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | const appName = 'onboarding'; |
| 24 | const dist = 'dist'; |
| 25 | |
| 26 | const path = { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 27 | // inputs |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 28 | json: './src/**/*.json', |
| 29 | index: './src/index.html', |
| 30 | heat: './src/heat.html', |
| 31 | scss: './resources/scss/**/*.scss', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 32 | i18nBundles: './src/nfvo-utils/i18n/*.json', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 33 | svgSrc: './resources/images/svg/*.svg', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 34 | appinf: './webapp-onboarding/**/*.*', |
| 35 | jetty: './webapp-onboarding/WEB-INF/jetty-web.xml', |
| 36 | srcDir: './src/', |
| 37 | // output |
| 38 | output: dist, |
| 39 | css: dist + '/css', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 40 | svg: dist + '/resources/images/svg', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 41 | appinf_output: dist + '/webapp-onboarding', |
| 42 | // war |
| 43 | war: [dist + '/index.html', dist + '/punch-outs*.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', dist + '/webapp-onboarding/**'], |
| 44 | heatWar: [dist + '/heat.html', dist + '/heat-validation_en.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', 'webapp-heat-validation/**'], |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 45 | wardest: dist, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 46 | // storybook |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 47 | storybookFonts: './.storybook/fonts/*', |
| 48 | storybookDist: './.storybook-dist', |
| 49 | storybookResources: './.storybook/resources/onboarding/resources/images/svg', |
| 50 | storybookDistResources: './.storybook-dist/onboarding/resources/images/svg' |
| 51 | }; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 52 | // cleans up the output directory |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 53 | taskMaker.defineTask('clean', {taskName: 'clean', src: path.output}); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 54 | // copies for all relevant files to the output directory |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 55 | taskMaker.defineTask('copy', {taskName: 'copy-json', src: path.json, dest: path.output, changed: {extension: '.json'}}); |
| 56 | taskMaker.defineTask('copy', {taskName: 'copy-index.html', src: path.index, dest: path.output, rename: 'index.html'}); |
| 57 | taskMaker.defineTask('copy', {taskName: 'copy-heat.html', src: path.heat, dest: path.output, rename: 'heat.html'}); |
| 58 | taskMaker.defineTask('copy', {taskName: 'copy-svg', src: path.svgSrc, dest: path.svg}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 59 | taskMaker.defineTask('copy', {taskName: 'copy-storybook-fonts', src: path.storybookFonts, dest: path.storybookDist}); |
| 60 | taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources', src: path.svgSrc, dest: path.storybookResources}); |
| 61 | taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources-prod', src: path.svgSrc, dest: path.storybookDistResources}); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 62 | // used for compressing war files |
| 63 | taskMaker.defineTask('compress', {taskName: 'compress-war', src: path.war, filename: appName + '.war', dest: path.wardest}); |
| 64 | taskMaker.defineTask('compress', {taskName: 'compress-heat-war', src: path.heatWar, filename: 'heat-validation.war', dest: path.wardest}); |
| 65 | // used for watching for changes for test |
| 66 | taskMaker.defineTask('watch', {taskName: 'watch-stuff', src: [path.json, path.index, path.heat], tasks: ['copy-stuff']}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 67 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 68 | |
| 69 | //TODO: delete this task after gulp-css-usage support for SCSS files |
| 70 | taskMaker.defineTask('sass', {taskName: 'sass', src: path.scss, dest: path.css, config: {outputStyle: 'compressed'}}); |
| 71 | |
| 72 | |
| 73 | // update the app-context for the web-xml file to the value from the config |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 74 | gulp.task('app-context', function(){ |
| 75 | gulp.src([path.appinf]) |
| 76 | .pipe(gulp.dest(path.appinf_output)) |
| 77 | .on('end', function () { |
| 78 | gulp.src([path.jetty]) |
| 79 | .pipe(replace(/<Set name="contextPath">.*<\/Set>/g, '<Set name="contextPath">'+jsonConfig.appContextPath+'</Set>')) |
| 80 | .pipe(gulp.dest(path.appinf_output + '/WEB-INF')); |
| 81 | }) |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 82 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 83 | // aggregates all copy tasks |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 84 | gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'copy-svg', 'app-context'], callback)); |
| 85 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 86 | // minimum build for dev |
| 87 | gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback)); |
| 88 | // build procedure for war file |
| 89 | gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'prod', ['compress-war', 'compress-heat-war'], callback)); |
| 90 | // default build is set to 'dev' |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 91 | gulp.task('default', ['dev']); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 92 | // creating the webpack tasks for the production build |
| 93 | gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles}) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 94 | .catch(err => { |
| 95 | if (err && err.stack) { |
| 96 | console.error(err, err.stack); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 97 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 98 | throw new Error('Webpack build FAILED'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | }) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 100 | ); |
| 101 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 102 | /*** |
| 103 | * T O O L S . N O T P A R T O F B U I L D |
| 104 | */ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 105 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 106 | // this is used to manually run on the sass files to check which classes are never used. not run as part of build. |
| 107 | // can be run as npm task |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 108 | gulp.task('gulp-css-usage', () => { |
| 109 | return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']})); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 110 | }); |
| 111 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 112 | gulp.task('css-usage', () => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 113 | runSequence('sass', 'gulp-css-usage'); |
| 114 | }); |
| 115 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 116 | |
| 117 | gulp.task('static-keys-bundle', () => i18nTask({outDir: path.output, srcDir: path.srcDir}) |
| 118 | .catch(err => { |
| 119 | throw new Error('static-keys-bundle FAILED'); |
| 120 | }) |
| 121 | ); |
| 122 | |
| 123 | gulp.task('static-keys-bundle-with-report', () => i18nTask({outDir: path.output, srcDir: path.srcDir, i18nBundles : path.i18nBundles }) |
| 124 | .catch(err => { |
| 125 | throw new Error('static-keys-bundle FAILED'); |
| 126 | }) |
| 127 | ); |