webpack.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const gulp = require('gulp')
  2. const webpack = require('webpack-stream')
  3. const filter = require('gulp-filter')
  4. const modify = require('gulp-modify-file')
  5. const uglify = require('gulp-uglify')
  6. const packageConfig = require('../package.json')
  7. const webpackConfig = require('../webpack.config')
  8. gulp.task('webpack', function() {
  9. return createStream()
  10. })
  11. gulp.task('webpack:dev', function() {
  12. return createStream(true)
  13. })
  14. gulp.task('webpack:watch', function() {
  15. createStream(true, true)
  16. })
  17. const jsFilter = filter([ '**/*.js' ], { restore: true })
  18. const localeFilter = filter([ '**/locale-all.js', '**/locale/*.js' ], { restore: true })
  19. function createStream(enableDev, enableWatch) {
  20. let stream = gulp.src([]) // don't pass in any files. webpack handles that
  21. .pipe(
  22. webpack(Object.assign({}, webpackConfig, {
  23. devtool: enableDev ? 'source-map' : false, // also 'inline-source-map'
  24. watch: enableWatch || false,
  25. }))
  26. )
  27. .pipe(
  28. // don't write bogus .css.js(.map) files webpack created for standalone css outputs
  29. filter([ '**', '!**/*.css.js*' ])
  30. )
  31. .pipe(
  32. // populate <%= %> variables in source code
  33. modify(function(content) {
  34. return content.replace(
  35. /<%=\s*(\w+)\s*%>/g,
  36. function(match, p1) {
  37. return packageConfig[p1]
  38. }
  39. )
  40. })
  41. )
  42. .pipe(jsFilter)
  43. .pipe(modify(function(content, path, file) {
  44. // for modules that plug into the core, webpack produces files that overwrite
  45. // the `FullCalendar` browser global each time. strip it out.
  46. if (file.relative !== 'dist/fullcalendar.js') {
  47. content = content.replace(/(root|exports)\[['"]FullCalendar['"]\]\s*=\s*/g, '')
  48. }
  49. // strip out "use strict", which moment and webpack harmony generates.
  50. content = content.replace(/['"]use strict['"]/g, '');
  51. return content
  52. }))
  53. .pipe(jsFilter.restore);
  54. if (!enableDev) {
  55. stream = stream
  56. .pipe(localeFilter)
  57. .pipe(uglify()) // uglify only the locale files, then bring back other files to stream
  58. .pipe(localeFilter.restore)
  59. }
  60. return stream.pipe(
  61. gulp.dest(webpackConfig.output.path)
  62. );
  63. }