webpack.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /*
  15. this task will be considered done after initial compile
  16. */
  17. gulp.task('webpack:watch', function(done) {
  18. createStream(true, true, done)
  19. })
  20. const jsFilter = filter([ '**/*.js' ], { restore: true })
  21. const localeFilter = filter([ '**/locale-all.js', '**/locale/*.js' ], { restore: true })
  22. function createStream(isDev, isWatch, doneCallback) {
  23. let doneCallbackCalled = false
  24. let stream = gulp.src([]) // don't pass in any files. webpack handles that
  25. .pipe(
  26. webpack(Object.assign({}, webpackConfig, {
  27. devtool: isDev ? 'source-map' : false, // also 'inline-source-map'
  28. watch: isWatch || false
  29. }))
  30. )
  31. .pipe(
  32. // don't write bogus .css.js(.map) files webpack created for standalone css outputs
  33. filter([ '**', '!**/*.css.js*' ])
  34. )
  35. .pipe(
  36. // populate <%= %> variables in source code
  37. modify(function(content) {
  38. return content.replace(
  39. /<%=\s*(\w+)\s*%>/g,
  40. function(match, p1) {
  41. return packageConfig[p1]
  42. }
  43. )
  44. })
  45. )
  46. .pipe(jsFilter)
  47. .pipe(modify(function(content, path, file) {
  48. // for modules that plug into the core, webpack produces files that overwrite
  49. // the `FullCalendar` browser global each time. strip it out.
  50. if (file.relative !== 'dist/fullcalendar.js') {
  51. content = content.replace(/(root|exports)\[['"]FullCalendar['"]\]\s*=\s*/g, '')
  52. }
  53. // strip out "use strict", which moment and webpack harmony generates.
  54. content = content.replace(/['"]use strict['"]/g, '')
  55. return content
  56. }))
  57. .pipe(jsFilter.restore)
  58. if (!isDev) {
  59. stream = stream
  60. .pipe(localeFilter)
  61. .pipe(uglify()) // uglify only the locale files, then bring back other files to stream
  62. .pipe(localeFilter.restore)
  63. }
  64. return stream.pipe(
  65. gulp.dest(webpackConfig.output.path)
  66. ).on('data', function() {
  67. if (doneCallback && !doneCallbackCalled) {
  68. doneCallbackCalled = true
  69. setTimeout(doneCallback, 100) // HACK: for some reason files not written an this point, so wait
  70. }
  71. })
  72. }