简体   繁体   English

为什么我有重新加载页面以便在运行服务器时显示脚本? (Gulp / Webpack / React)

[英]Why do I have reload page in order for script to display when I run the server? (Gulp/Webpack/React)

When I run gulp, everything is compiled into another dist folder. 当我运行gulp时,所有内容都会编译到另一个dist文件夹中。 The page automatically loads up but upon loading, the script does display until I reload the page once again. 页面会自动加载,但是加载后,脚本会一直显示,直到我再次重新加载页面为止。 Not sure what the issue is. 不知道是什么问题。

Gulpfile Gulp文件

var gulp = require('gulp');
var argv = require('yargs').argv;
var gulpIf = require('gulp-if');
var sourcemaps = require('gulp-sourcemaps');
var browserSync = require('browser-sync');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var csscomb = require('gulp-csscomb');
var cssnano = require('gulp-cssnano');
var bourbon = require('node-bourbon').includePaths;
var neat = require('node-neat').includePaths;
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var cache = require('gulp-cache');
var del = require('del');
var rename = require('gulp-rename');
var htmlreplace = require('gulp-html-replace');
var runSequence = require('run-sequence');
var sizereport = require('gulp-sizereport');
var htmlmin = require('gulp-html-minifier');
var webpack = require('webpack-stream');


gulp.task('browserSync', function(){
    browserSync({
        server: {
            baseDir: 'dist'
        },
        browser: "google chrome"
    });
});
/* --- WEBPACK --- */
gulp.task('webpack', function() {
  return gulp.src('app/assets/js/main.js')
.pipe(webpack( require('./webpack.config.js') ))
  .pipe(gulpIf(argv.production, uglify()))
  .pipe(gulpIf(argv.production, rename({suffix:'.min'})))
    .pipe(gulp.dest('dist/assets/js/'))
    .pipe(browserSync.reload({stream:true}))
});

/* --- STYLE TASKS--- */
gulp.task('styles',function(){
    return gulp.src('app/assets/scss/**/*.scss')
    .pipe(gulpIf(!argv.production, sourcemaps.init()))
        .pipe(sass({
            includePaths: bourbon,
            includePaths: neat
        }))
        .pipe(csscomb(csscomb.json))
        .pipe(autoprefixer({
            browsers: ['last 3 versions'],
            cascade: false
        }))
        .pipe(gulpIf(argv.production, rename({suffix:'.min'})))
        .pipe(gulpIf(argv.production, cssnano()))
        .pipe(gulpIf(argv.production, sizereport({
            gzip: true
        })))
    .pipe(sourcemaps.write('../maps'))
    .pipe(gulp.dest('dist/assets/css'))
    .pipe(browserSync.reload({stream:true}));
});
/*--- HTML ---*/
gulp.task('html', function () {
    return gulp.src('app/*.html')
        .pipe(gulpIf(argv.production, htmlreplace({
        'css': './assets/css/app.min.css',
        'js': './assets/js/main.min.js'
    })))
        .pipe(gulpIf(argv.production, htmlmin({collapseWhitespace: true})))
        .pipe(gulp.dest('dist'))
        .pipe(browserSync.reload({
            stream: true
        }));
});
/* --- Watch Task ---*/
gulp.task ('watch', function(){
    gulp.watch('app/assets/scss/**/*.scss', ['styles']);
    gulp.watch('app/*.html', ['html']);
    gulp.watch('app/assets/img/*', ['images']);
    gulp.watch('app/assets/js/**/*.js', ['webpack']);
});

/* --- IMAGE TASKS ---*/
gulp.task('images', function() {
  return gulp.src('app/assets/img/**/*.+(png|jpg|jpeg|gif|svg)')
    .pipe(cache(imagemin({
      interlaced: true,
      progressive: true
    })))
   .pipe(gulp.dest('./dist/assets/img'))
});
/* --- Font Tasks --- */    
gulp.task('fonts', function () {
    return gulp.src('app/assets/fonts/**/*')
        .pipe(gulp.dest('dist/assets/fonts'))
        .pipe(browserSync.reload({
            stream: true
        }));
});    
/*---- CLEAN TASKS ---*/
gulp.task('clean', function() {
  return del.sync('dist').then(function(cb) {
    return cache.clearAll(cb);
  });
});
gulp.task('clean:dist', function () {
    return del.sync(['dist/**/*', '!dist/images', '!dist/images/**/*']);
});
/*--- BUILD TASKS ---*/
gulp.task('default', function(cb) {
  runSequence('clean:dist', 
              ['styles', 'html', 'images', 'fonts', 'webpack', 'browserSync', 'watch'], cb )
});

webpack 网络包

module.exports = {

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015'],
            plugins: ['transform-class-properties']
        }
      }
    ]
  },
  output: {
    filename: "main.js"
  }
};

Im not sure why you are using gulp when you can use webpack. 我不确定为什么可以使用webpack时为什么要使用gulp。 I think what you are talking about is react-hot-loader . 我认为您正在谈论的是react-hot-loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么必须重新加载页面才能正确显示地图? - Why do I have to reload the page to display the map correctly? 为什么必须重新加载页面才能使JS事件正常工作? - Why do I have to reload the page for my JS event to work? 为什么我重新加载页面时 React 应用程序会失败 - Why does React app fail when I reload page Webpack开发服务器可以重新加载,但页面不会更改 - Webpack dev server DO reload, but page not change 为什么在使用 react-router 时必须手动刷新页面才能加载组件? - Why do I have to manually refresh the page for the component to load when using react-router? 我在本地服务器上运行时无法显示页面 - can't display page when i run on local server 运行“ gulp”时如何查看棉绒错误? - How do i view linting errors when i run 'gulp'? 为什么我必须在 gulp 中使用乙烯基源流? - Why do I have to use vinyl-source-stream with gulp? 如何在 webpack 开发服务器启动后自动运行 NPM 脚本? - How do I automatically run an NPM script after webpack dev server has been started? 当 webpack-dev-server 导致页面重新加载时,在 GUI 中重新运行 Cypress 测试 - Re-run Cypress tests in GUI when webpack-dev-server causes page reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM