关于 gulp 的基本使用方法前面有介绍。
Gulp使用指南 
昨天,朋友问我一个问题。gulp打包给文件添加 hash 之后,怎么替换其他文件中引用的js(或者css),要替换成打包以后的带有 hash 的那一个文件。
其实主要用到的就是 gulp-rev  和 gulp-replace  这两个插件。
有三个js文件
a.js  
b.js 1 2 3 4 import  a from  'a' ;console .log(a)export  default  'b' ;
 
main.js 1 2 3 4 5 import  a from  'a' ;import  b from  'b' ;console .log(a)console .log(b)
 
三个js文件都需要打包并添加hash值。如果默认没有replace操作。那么打包后的main.js中的 import 的内容依旧是原来的。比如 import b from 'b'; 打包后还是 import b from 'b'; 而不是 import b from 'b-XXXXXXXX'; 这样带有hash的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 const  gulp = require ('gulp' );const  rev = require ('gulp-rev' );const  del = require ('del' );const  replace = require ('gulp-replace' );gulp.task('c' , function  (cb )  {   del(     [              'dist/*' ,     ],     cb   ); }); gulp.task(   'js' ,   () =>     gulp       .src(['./src/*js' ])       .pipe(gulp.dest('dist' ))        .pipe(rev())       .pipe(gulp.dest('dist' ))        .pipe(rev.manifest('js-rev.json' ))        .pipe(gulp.dest('dist' ))  ); gulp.task('r' , () =>  {   const  manifest = require ('./dist/js-rev.json' )   for  (const  key in  manifest) {     const  newKey = key.split('.' )[0 ];     manifest[newKey] = manifest[key].split('.' )[0 ]     delete  manifest[key];   }      return  (     gulp       .src('./dist/*.js' )       .pipe(replace(/import (\S+) from ('|")(\S+)('|")/g , (match, a, b, c ) =>  {         return  `import ${a}  from '${manifest[c]} '`        }))       .pipe(gulp.dest('dist' ))   ); }); 
 
大概的就是上面的配置,至于其他的,比如说要 babel 转译。直接添加相关的插件即可。
在线地址