webpack.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const gulp = require('gulp')
  2. const gutil = require('gulp-util')
  3. const filter = require('gulp-filter')
  4. const modify = require('gulp-modify-file')
  5. const uglify = require('gulp-uglify')
  6. const webpack = require('webpack-stream')
  7. const packageConfig = require('../package.json')
  8. const webpackConfig = require('../webpack.config')
  9. gulp.task('webpack', function() {
  10. return createStream()
  11. })
  12. gulp.task('webpack:dev', function() {
  13. return createStream(true)
  14. })
  15. /*
  16. this task will be considered done after initial compile
  17. */
  18. gulp.task('webpack:watch', function() {
  19. createStream(true, true)
  20. })
  21. const jsFilter = filter([ '**/*.js' ], { restore: true })
  22. const localeFilter = filter([ '**/locale-all.js', '**/locale/*.js' ], { restore: true })
  23. function createStream(isDev, isWatch) {
  24. let stream = gulp.src([]) // don't pass in any files. webpack handles that
  25. .pipe(
  26. webpack(
  27. Object.assign({}, webpackConfig, {
  28. devtool: isDev ? 'source-map' : false, // also 'inline-source-map'
  29. watch: isWatch || false
  30. }),
  31. null,
  32. reporterFunc
  33. )
  34. )
  35. .pipe(
  36. // don't write bogus .css.js(.map) files webpack created for standalone css outputs
  37. filter([ '**', '!**/*.css.js*' ])
  38. )
  39. .pipe(
  40. // populate <%= %> variables in source code
  41. modify(function(content) {
  42. return content.replace(
  43. /<%=\s*(\w+)\s*%>/g,
  44. function(match, p1) {
  45. return packageConfig[p1]
  46. }
  47. )
  48. })
  49. )
  50. .pipe(jsFilter)
  51. .pipe(modify(function(content, path, file) {
  52. // for modules that plug into the core, webpack produces files that overwrite
  53. // the `FullCalendar` browser global each time. strip it out.
  54. if (file.relative !== 'dist/fullcalendar.js') {
  55. content = content.replace(/(root|exports)\[['"]FullCalendar['"]\]\s*=\s*/g, '')
  56. }
  57. // strip out "use strict", which moment and webpack harmony generates.
  58. content = content.replace(/['"]use strict['"]/g, '')
  59. return content
  60. }))
  61. .pipe(jsFilter.restore)
  62. if (!isDev) {
  63. stream = stream
  64. .pipe(localeFilter)
  65. .pipe(uglify()) // uglify only the locale files, then bring back other files to stream
  66. .pipe(localeFilter.restore)
  67. }
  68. return stream.pipe(
  69. gulp.dest(webpackConfig.output.path)
  70. )
  71. }
  72. /*
  73. Purpose is to filter out the poorly formatted stats for locale files
  74. */
  75. function reporterFunc(err, stats) {
  76. if (err) {
  77. gutil.log('Problem compiling!')
  78. } else {
  79. let assets = stats.compilation.assets
  80. let filteredAssets = {}
  81. let localeModuleCnt = 0
  82. for (let moduleName in assets) {
  83. if (moduleName.match(/^dist\/locale/)) {
  84. localeModuleCnt++
  85. } else {
  86. filteredAssets[moduleName] = assets[moduleName]
  87. }
  88. }
  89. stats.compilation.assets = filteredAssets
  90. gutil.log(
  91. stats.toString({
  92. // from https://github.com/shama/webpack-stream/blob/v4.0.0/index.js#L12
  93. colors: true,
  94. hash: false,
  95. timings: false,
  96. chunks: false,
  97. chunkModules: false,
  98. modules: false,
  99. children: true,
  100. version: true,
  101. cached: false,
  102. cachedAssets: false,
  103. reasons: false,
  104. source: false,
  105. errorDetails: false
  106. })
  107. )
  108. if (localeModuleCnt) {
  109. gutil.log(`Also compiled ${localeModuleCnt} locale-related files.`)
  110. }
  111. }
  112. }