Guest User

Untitled

a guest
Mar 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. /**
  2. * Created by tliang on 3/9/2018.
  3. */
  4.  
  5. var gulp = require('gulp');
  6. var concat = require('gulp-concat');
  7. var uglify = require('gulp-uglify');
  8. var react = require('gulp-react');
  9. var htmlreplace = require('gulp-html-replace');
  10. var babel = require('gulp-babel');
  11. var source = require('vinyl-source-stream');
  12. var browserify = require('browserify');
  13. var watchify = require('watchify');
  14. var reactify = require('reactify');
  15. var streamify = require('gulp-streamify');
  16. var babelify = require('babelify');
  17. var gutil = require('gulp-util');
  18. var chalk = require('chalk');
  19. var sourcemaps = require('gulp-sourcemaps');
  20.  
  21. function map_error(err) {
  22. if (err.fileName) {
  23. // regular error
  24. gutil.log(chalk.red(err.name)
  25. + ': '
  26. + chalk.yellow(err.fileName.replace(__dirname + '/src/', ''))
  27. + ': '
  28. + 'Line '
  29. + chalk.magenta(err.lineNumber)
  30. + ' & '
  31. + 'Column '
  32. + chalk.magenta(err.columnNumber || err.column)
  33. + ': '
  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. //this.end()
  43. }
  44.  
  45.  
  46. var path = {
  47. HTML: 'src/index.html',
  48. ALL: ['src/*.js','src/**/*.js','src/index.html'],
  49. JS: ['src/*.js','src/**/*.js'],
  50. MINIFIED_OUT: 'build.min.js',
  51. DEST_SRC: './dist/src',
  52. DEST_BUILD: './dist/build',
  53. DEST: 'dist',
  54. ENTRY_POINT: './src/index.js',
  55. OUT: 'build.js'
  56. };
  57.  
  58. gulp.task('build1',function(){
  59.  
  60. return gulp.src(path.JS)
  61. .pipe(babel({
  62. presets:['es2015','react']
  63. }))
  64. .pipe(concat(path.MINIFIED_OUT))
  65. .pipe(uglify())
  66. .pipe(gulp.dest(path.DEST_BUILD));
  67. });
  68.  
  69. gulp.task('build2', function(){
  70. browserify({
  71. entries: [path.ENTRY_POINT],
  72. debug:true
  73. }).transform ('babelify',{presets:['es2015','react']
  74. }).bundle()
  75. .pipe(source(path.MINIFIED_OUT))
  76. .pipe(streamify(uglify(path.MINIFIED_OUT)))
  77. .pipe(sourcemaps.write('./maps'))
  78. .pipe(gulp.dest(path.DEST_BUILD));
  79. });
  80.  
  81. function build() {
  82. return gulp.src(path.JS)
  83. .pipe(babel({
  84. presets:['es2015','react']
  85. }))
  86. .pipe(concat(path.MINIFIED_OUT))
  87. .pipe(uglify())
  88. .pipe(gulp.dest(path.DEST_BUILD));
  89. }
  90.  
  91. gulp.task('transform', function() {
  92. return gulp.src(path.JS)
  93. .pipe(babel({
  94. presets:['es2015','react']
  95. }))
  96. .pipe(gulp.dest(path.DEST_SRC));
  97. });
  98.  
  99.  
  100. gulp.task('copy',function() {
  101. return gulp.src(path.HTML)
  102. .pipe(gulp.dest(path.DEST));
  103. });
  104.  
  105. /*gulp.task('watch',function() {
  106. gulp.watch(path.ALL,'transform');
  107. gulp.watch(path.ALL,'copy');
  108. });*/
  109.  
  110. gulp.task('replaceHTML',function() {
  111. return gulp.src(path.HTML)
  112. .pipe(htmlreplace({
  113. 'js':'build/' + path.MINIFIED_OUT
  114. }))
  115. .pipe(gulp.dest(path.DEST));
  116. });
  117.  
  118. gulp.task('copy-to-app',['build'],function () {
  119. return gulp.src('./build/reactapptest.js')
  120. .pipe(gulp.dest('C:\\Users\\tliang\\Workspaces\\SpringBoot2\\src\\main\\webapp\\WEB-INF\\jsp\\'))
  121. });
  122.  
  123. gulp.task('watch',function () {
  124.  
  125. gulp.watch(path.HTML,['copy']);
  126.  
  127. var watcher = watchify(browserify({
  128.  
  129. entries: [path.ENTRY_POINT],
  130. transform: [reactify],
  131. debug: true,
  132. cache: {}, packageCache: {}, fullPaths: true
  133.  
  134. }));
  135.  
  136. return watcher.on('update',function () {
  137.  
  138. watcher.bundle()
  139. .pipe(source(path.OUT))
  140. .pipe(gulp.dest(path.DEST_SRC))
  141. console.log('Updated');
  142. })
  143. });
  144.  
  145. gulp.task('build',function () {
  146.  
  147. browserify({
  148. entries:[path.ENTRY_POINT],
  149. transform:['babelify',{presets:['es2015']}]
  150. })
  151. .bundle()
  152. .pipe(source(path.MINIFIED_OUT))
  153. .pipe(streamify(uglify(path.MINIFIED_OUT)))
  154. .pipe(gulp.dest(path.DEST_BUILD));
  155.  
  156. });
  157.  
  158. var b = watchify(browserify({
  159. entries: ['./src/index.js'],
  160. extensions: ['.js','.jsx'],
  161. debug: true,
  162. fullPaths: true,
  163. cache: {},
  164. packageCache: {}
  165. }),{poll:true});
  166.  
  167. b.transform("babelify",{"presets":["es2015","react"],
  168. "plugins": ["transform-object-rest-spread"]});
  169.  
  170. function bundle() {
  171. return b.bundle()
  172. .on('error',map_error)
  173. .pipe(source(path.MINIFIED_OUT))
  174. .pipe(gulp.dest(path.DEST_BUILD));
  175. }
  176.  
  177. gulp.task('babel',bundle);
  178.  
  179. gulp.task('default',['watch']);
  180.  
  181.  
  182.  
  183. /*gulp.task('default',['copy-to-app']);*/
  184.  
  185. gulp.task('production',['replaceHTML','build']);
Add Comment
Please, Sign In to add comment