How to use gulp webpack-stream to generate a proper named file?

前端 未结 4 1369
刺人心
刺人心 2021-02-19 03:19

Currently we\'re using Webpack for our Module loader, and Gulp for everything else (sass -> css, and the dev/production build process)

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 03:57

    Rather than giving your javascript a fixed filename, a better solution would be to use gulp-inject and insert the generated hash filename into a script tag. This means you don't have to worry about cache expiry on the compiled javascript (which is why the hash filename is being used in the first place).

    const inject = require('gulp-inject');
    
    gulp.task('webpack', function() {
        const index = './src/index.html';
        const scripts = gulp.src('entry.js')
        .pipe(webpack( require('./webpack.config.js') ))
        .pipe(gulp.dest('dist/js'));
    
        return target
           .pipe(inject(scripts))
           .pipe(gulp.dest('dist/'));
    });
    

    and of course you need the inject section in your src/index.html:

    
    
    
      index page
    
    
    
      
      
    
    
    

提交回复
热议问题