Va1 / browser-sync-webpack-plugin

Easily use BrowserSync in your Webpack project.
MIT License
369 stars 40 forks source link

Browser CSS is always version behind #75

Closed Razunter closed 5 years ago

Razunter commented 5 years ago

Can't figure out why this is happening, but my browser is always displays previous version of BrowserSync's CSS, but HTML updates properly with new CSS file. Doesn't seem to be related to specific browser. I think I've had the same issue with Gulp before I've started using .stream() in BS, but I'm not sure if it can be used with Webpack.

const {
  themename,
  serverUrl,
  pathtothemefolder
} = require('./theme-constants');

const globImporter = require('node-sass-glob-importer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const imageminGuetzli = require('imagemin-guetzli');
const CopyWebpackPlugin = require('copy-webpack-plugin');

var path = require('path');

module.exports = {
  entry: './main.js',
  output: {
    path: path.resolve(__dirname, './'),
    filename: 'bundle.js',
    publicPath: './' + themename
  },
  module: {
    rules: [
      /*      {
              test: \/js-src/!*.js$/,
              exclude: /node_modules/,
              use: {
                loader: 'babel-loader',
                options: {
                  presets: [
                    [
                      '@babel/preset-env',
                      {
                        'useBuiltIns': 'entry'
                      }
                    ]
                  ]
                }
              }
            },*/
      {
        test: /\.(png|jpg|gif|svg|webp)$/,
        exclude: /node_modules/,
        loader: 'url-loader',
        options: {
          name: '[name].[ext]?[hash]',
          limit: 8192,
          publicPath: '../images/',
        }
      },
      {
        test: /\.(scss|sass)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          // {
          //   loader: 'style-loader' // creates style nodes from JS strings
          // },
          {
            loader: 'css-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              sourceMap: true
            }
            // }, {
            //   loader: 'resolve-url-loader',
            //   options: {
            //     sourceMap: true
            //   }
          },
          {
            loader: 'sass-loader',
            options: {
              importer: globImporter(),
              sourceMap: true,
            }
          }
        ]
      }
    ]
  },
  devtool: 'source-map',
  plugins: [
    // Copy the images folder and optimize all the images
    new CopyWebpackPlugin([{
      from: 'img-src/images',
      to: themename + '/images'
    }]),
    new ImageminPlugin({
      test: /\.(jpe?g|png|gif|svg)$/i,
      cacheFolder: './cache',
      svgo: {
        removeTitle: true
      },
      plugins: [
        imageminGuetzli({
          quality: 85
        })
      ]
    }),
    new FixStyleOnlyEntriesPlugin(),
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: themename + '/css/style.css',
      chunkFilename: themename + '/css/[id].css'
    }),
    new BrowserSyncPlugin({
        proxy: {
          target: serverUrl,
          ws: true
        },
        serveStatic: [{
          route: pathtothemefolder,
          dir: './' + themename
        }],
        files: [
          themename + '/css/*',
          themename + '/templates/*',
          themename + '/blueprints/*',
        ],
        injectCss: true,
        open: false,
        cors: true
      },
      { reload: false }
    )
  ],
};

Tried adding ReloadDelay in case it served the CSS file too soon, but it's not the case.

Razunter commented 5 years ago

OK, nvm, I've figured it out... serveStatic paths were wrong and I was getting server-side files instead of local (freshly updated) versions.