Building the front end with Gulp ================================ --- label: Gulp Document id: rhizome_front_end_gulp_doc categorySlug: front_end categoryLabel: Front End Document categoryRank: 1 documentRank: 4 # GULP Documentation ## Environment In this project, we use gulp to build frontend system. Gulp: use `npm install -g gulp` to install gulp. Gulp src directory: `/webapp/src` Gulp destination directory: `/webapp/public` We have two basic **GULP TASKS**, `gulp` and `gulp dev`, they are defined in `gulpfile.babel.js`. ``` gulp.task('dev', () => { gulp.config(gulp.DEV_MODE, true) gulp.start(['build']) }) gulp.task('default', ['build']) ``` * Use `gulp dev` in local development environment to build and watch frontend change. * Use `gulp build` in production environment for deployment. ## Tasks We have sevaral gulp tasks, `clean, copy, sass, standard, browserify and revision`. All the tasks are defined seperately in `/webapp/tasks` folder. ### Clean **Clean** task is used to remove the whole gulp destination directory `/webapp/public` to prepare for next step. ### Copy **Copy** task is used to copy all static assets from gulp src directory to gulp dest directory. ### Sass **Sass** task is used to compile all sass files to css files. ### Standard **Standard** task is used to check JavaScript code with the standard syntax. We're using [standard](https://github.com/feross/standard) with [babel-eslint](https://github.com/babel/babel-eslint) to format our Javascript code. Make sure run standard before committing any Javascript code. Install `standard` and `babel-eslint` by `npm install -g standard` and `npm install -g babel-eslint`. They must be installed globally. Run `standard` in `/webapp` folder to check Javascript format. Fix any format error before you commit. Standard will be added to CI pipeline to ensure the code format is strictly followed. Here's a quick view of standard rules. For more details, visit standard homepage or check standard error output. * 2 spaces for indentation. No tab is allowed. * Single quotes for strings. * No unused variables. * No semicolons. * Always use === instead of == except for `null`. * Space after keywords as `if` and function name. * React props must be defined by `propTypes`. ### Browserify and Watchify **Browserify** task is used to build a bundle which can be served up to browser in a single ` [**RevReplace**](https://github.com/sindresorhus/gulp-rev/blob/master/integration.md) task will replace the ` And `base.html` in **production env** will use revisional files. //This is base.html in production env //static css files //static js files ### Lint **Lint** task is used to identify and report on patterns found in ECMAScript/JavaScript code. ### Mocha [**Mocha**](https://github.com/knpwrs/gulp-spawn-mocha) task is used to run Mocha tests in a separate process from the gulp process. We can use `gulp mocha` to run the tests separately from other tasks. And `gulp mocha` will also generate a coverage report for frontend code. The report is in `/webapp/coverage/lcov-report/` folder and you can view the report through `/webapp/coverage/lcov-report/index.html` file. If you do not want to test the coverage, you can change the config in `/tasks/config/mocha.js`: ``` export default { src: [ `${gulp.config('base.src')}/**/__tests__/*.spec.js{,x}` ], options: { r: 'src/helpers/jsdom.js', R: 'dot', compilers: '.:babel/register', istanbul: true // Will test coverage and generate report. istanbul: false // Will not test coverage and generate report. } } ``` ### Package **Package** task is used to compress frontend code into a zip package.