webpack.js 3.3 KB

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