@@ -3,7 +3,7 @@ const path = require("path");
3
3
const fs = require ( "fs" ) ;
4
4
const util = require ( "gulp-util" ) ;
5
5
const concat = require ( "gulp-concat" ) ;
6
- const uglify = require ( "gulp-uglify" ) ;
6
+ const uglify = require ( "gulp-uglify-es" ) . default ;
7
7
const saveLicense = require ( "uglify-save-license" ) ;
8
8
const cleanCSS = require ( "gulp-clean-css" ) ;
9
9
const md5File = require ( 'md5-file' )
@@ -52,7 +52,7 @@ const bundles = [
52
52
53
53
var md5HashMap = { } ;
54
54
55
- gulp . task ( "bundle-and-minify" , ( ) => {
55
+ const bundleAndMinify = ( done ) => {
56
56
var isDebugMode = argv . debugMode !== undefined && argv . debugMode . toLowerCase ( ) . trim ( ) === "true" ;
57
57
var promises = [ ] ;
58
58
bundles . forEach ( bundle => {
@@ -63,29 +63,29 @@ gulp.task("bundle-and-minify", () => {
63
63
}
64
64
} ) ;
65
65
66
- return Promise . all ( promises ) . then ( function ( ) {
67
- gulp . start ( "generate-bundling-global-metadata" ) ;
68
- } ) ;
69
- } ) ;
66
+ return Promise . all ( promises ) . then ( generateBundlingGlobalMetadata ( done ) ) ;
67
+ }
70
68
71
- gulp . task ( "bundle-and-minify:watch" , [ "bundle-and-minify" , "generate-file-check-sum-map" ] , ( ) => {
69
+ const addWatcher = ( done ) => {
72
70
var allFiles = [ ] ;
73
71
bundles . forEach ( bundle => {
74
72
allFiles = allFiles . concat ( bundle . files ) ;
75
73
} ) ;
76
74
77
- gulp . watch ( [ allFiles ] , { cwd : baseFolder } ) . on ( ' change' , function ( file ) {
78
- var hash = md5File . sync ( file . path ) ;
79
- if ( md5HashMap [ file . path ] !== hash ) {
80
- md5HashMap [ file . path ] = hash ;
81
- gulp . start ( "bundle-and-minify" ) ;
75
+ gulp . watch ( allFiles , { cwd : baseFolder } , gulp . series ( bundleAndMinify ) ) . on ( " change" , function ( file ) {
76
+ var filePath = path . join ( ` ${ __dirname } \\ ${ baseFolder + file } ` ) ;
77
+ var hash = md5File . sync ( filePath ) ;
78
+ if ( md5HashMap [ filePath ] !== hash ) {
79
+ md5HashMap [ filePath ] = hash ;
82
80
}
81
+
83
82
} ) ;
84
- } ) ;
83
+ done ( ) ;
84
+ }
85
85
86
86
// Generating hash per each file in the bundles based on its content.
87
87
// It is used to generate a new bundle only if the content of a file is changed.
88
- gulp . task ( "generate-file-check-sum-map" , ( ) => {
88
+ const generateFileCheckSumMap = ( done ) => {
89
89
var allFiles = [ ] ;
90
90
bundles . forEach ( bundle => {
91
91
allFiles = allFiles . concat ( bundle . files ) ;
@@ -96,9 +96,10 @@ gulp.task("generate-file-check-sum-map", () => {
96
96
var hash = md5File . sync ( filePath ) ;
97
97
md5HashMap [ filePath ] = hash ;
98
98
} ) ;
99
- } )
99
+ done ( ) ;
100
+ }
100
101
101
- gulp . task ( "generate-bundling-global-metadata" , ( ) => {
102
+ const generateBundlingGlobalMetadata = ( done ) => {
102
103
var metadata = { } ;
103
104
// for general cache invalidation purposes
104
105
metadata [ "_timestamp" ] = new Date ( ) . getTime ( ) ;
@@ -108,7 +109,9 @@ gulp.task("generate-bundling-global-metadata", () => {
108
109
} ) ;
109
110
110
111
fs . writeFileSync ( path . join ( __dirname , "template" , "bundling.global.json" ) , JSON . stringify ( metadata ) ) ;
111
- } )
112
+
113
+ done ( ) ;
114
+ }
112
115
113
116
function bundleAndMinifyJS ( files , fileName , outputFolder , isDebugMode ) {
114
117
return new Promise ( function ( resolve , reject ) {
@@ -138,3 +141,6 @@ function bundleAndMinifyCSS(files, fileName, outputFolder, isDebugMode) {
138
141
. on ( 'end' , resolve ) ;
139
142
} ) ;
140
143
}
144
+
145
+ exports . bundleAndMinify = bundleAndMinify ;
146
+ exports . bundleAndMinifyWatch = gulp . series ( bundleAndMinify , generateFileCheckSumMap , addWatcher ) ;
0 commit comments