Advertisement
Guest User

Untitled

a guest
May 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. var gulp = require('gulp'); // Base gulp package
  2. var babelify = require('babelify'); // Used to convert ES6 & JSX to ES5
  3. var browserify = require('browserify'); // Providers "require" support, CommonJS
  4. var notify = require('gulp-notify'); // Provides notification to both the console and Growel
  5. var rename = require('gulp-rename'); // Rename sources
  6. var sourcemaps = require('gulp-sourcemaps'); // Provide external sourcemap files
  7. var livereload = require('gulp-livereload'); // Livereload support for the browser
  8. var gutil = require('gulp-util'); // Provides gulp utilities, including logging and beep
  9. var chalk = require('chalk'); // Allows for coloring for logging
  10. var source = require('vinyl-source-stream'); // Vinyl stream support
  11. var buffer = require('vinyl-buffer'); // Vinyl stream support
  12. var watchify = require('watchify'); // Watchify for source changes
  13. var merge = require('utils-merge'); // Object merge tool
  14. var duration = require('gulp-duration'); // Time aspects of your gulp process
  15.  
  16. // Configuration for Gulp
  17. var config = {
  18. js: {
  19. src: './resources/assets/react/main.js',
  20. watch: './resources/assets/**/*',
  21. outputDir: './public/build/',
  22. outputFile: 'build.js',
  23. },
  24. };
  25.  
  26. // Error reporting function
  27. function mapError(err) {
  28. if (err.fileName) {
  29. // Regular error
  30. gutil.log(chalk.red(err.name)
  31. + ': ' + chalk.yellow(err.fileName.replace(__dirname + '/src/js/', ''))
  32. + ': ' + 'Line ' + chalk.magenta(err.lineNumber)
  33. + ' & ' + 'Column ' + chalk.magenta(err.columnNumber || err.column)
  34. + ': ' + chalk.blue(err.description));
  35. } else {
  36. // Browserify error..
  37. gutil.log(chalk.red(err.name)
  38. + ': '
  39. + chalk.yellow(err.message));
  40. }
  41. }
  42.  
  43. // Completes the final file outputs
  44. function bundle(bundler) {
  45. var bundleTimer = duration('Javascript bundle time');
  46. bundler
  47. .bundle()
  48. .on('error', mapError) // Map error reporting
  49. .pipe(source('main.jsx')) // Set source name
  50. .pipe(buffer()) // Convert to gulp pipeline
  51. .pipe(rename(config.js.outputFile)) // Rename the output file
  52. .pipe(sourcemaps.init({loadMaps: true})) // Extract the inline sourcemaps
  53. .pipe(sourcemaps.write('./map')) // Set folder for sourcemaps to output to
  54. .pipe(gulp.dest(config.js.outputDir)) // Set the output folder
  55. .pipe(notify({
  56. message: 'Generated file: <%= file.relative %>',
  57. })) // Output the file being created
  58. .pipe(bundleTimer) // Output time timing of the file creation
  59. .pipe(livereload()); // Reload the view in the browser
  60. }
  61.  
  62. // Gulp task for build
  63. gulp.task('default', function() {
  64. livereload.listen(); // Start livereload server
  65. var args = merge(watchify.args, { debug: true }); // Merge in default watchify args with browserify arguments
  66.  
  67. var bundler = browserify(config.js.src, args) // Browserify
  68. .plugin(watchify, {ignoreWatch: ['**/node_modules/**', '**/bower_components/**']}) // Watchify to watch source file changes
  69. .transform(babelify, {presets: ['es2015', 'react']}); // Babel tranforms
  70.  
  71. bundle(bundler); // Run the bundle the first time (required for Watchify to kick in)
  72.  
  73. bundler.on('update', function() {
  74. bundle(bundler); // Re-run bundle on source updates
  75. });
  76. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement