javascript - How to Hot Reload Sass Using Webpack 2? -
i'm working on setting react application uses webpack2, webpack-dev-middleware , hmr development. whenever make change on react component, updates in browser intended. issue running when modify .scss files, browser not update. happens instead, in console gives me following:
[hmr] bundle rebuilding client.js:207 [hmr] bundle rebuilt in 1567ms process-update.js:27 [hmr] checking updates on server... process-update.js:98 [hmr] nothing hot updated. process-update.js:107 [hmr] app date. after this, when refresh page, style changes appear. i'm not entirely sure what's going on or issue stems , clarification.below setup:
webpack.config.js
var webpack = require('webpack'); var path = require('path'); var autoprefixer = require('autoprefixer'); var dashboardplugin = require('webpack-dashboard/plugin'); var extracttextplugin = require('extract-text-webpack-plugin'); var argv = require('yargs').argv; const config = {}; // configured production if (argv.p) { config.entry = [ './src/client/scripts/index', './src/client/scripts/utils/index', './src/client/styles/index.scss' ] config.plugins = [ new dashboardplugin(), new extracttextplugin({ filename: 'bundle.css', allchunks: true }), ] } else { config.entry = [ 'react-hot-loader/patch', 'webpack-hot-middleware/client', './src/client/scripts/index', './src/client/scripts/utils/index', './src/client/styles/index.scss' ] config.plugins = [ new dashboardplugin(), new webpack.hotmodulereplacementplugin(), new webpack.noemitonerrorsplugin(), new webpack.namedmodulesplugin(), new extracttextplugin({ filename: 'bundle.css', allchunks: true }) ] } module.exports = { entry: config.entry, output: { path: path.join(__dirname, 'src', 'client', 'static'), filename: 'bundle.js', publicpath: '/static/' }, devtool: 'inline-source-map', devserver: { hot: true, contentbase: path.resolve(__dirname, 'src', 'client', 'static'), publicpath: (__dirname, 'src', 'client', 'static') }, plugins: config.plugins, module: { rules: [ { test: /\.js?$/, exclude: /(node_modules|bower_components)/, include: path.join(__dirname, 'src'), use: [ { loader: 'babel-loader', query: { presets: ['react', ['es2015', { 'modules': false }], 'stage-0'], plugins: ['react-hot-loader/babel', 'react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'], } } ] }, { test: /\.(png|woff|woff2|eot|ttf|svg)$/, use: [ { loader: 'url-loader?limit=100000' } ], }, { test: /\.scss$/, use: extracttextplugin.extract({ fallback: 'style-loader', use: ['css-loader', 'sass-loader'] }) } ] } }; server.js using webpack-dev-middleware
const router = router(); const clientdir = resolve(`${__dirname}/../../client`); if (isdev()) { const webpackdevmiddleware = require('webpack-dev-middleware') const webpack = require('webpack') const webpackconfig = require('../../../webpack.config') const webpackhotmiddleware = require('webpack-hot-middleware') const compiler = webpack(webpackconfig) // compiles our app using webpack router.use(webpackdevmiddleware(compiler, { publicpath: webpackconfig.output.publicpath, noinfo: true })) // connects our app hmr using middleware router.use(webpackhotmiddleware(compiler)) } router.use(express.static(clientdir)); export default router index.js on client side
import react 'react' import reactdom 'react-dom' import { appcontainer } 'react-hot-loader' import app './app' const root = document.queryselector('.root'); // wraps our app in appcontainer const render = (component) => { reactdom.render( <appcontainer> <component/> </appcontainer>, root ); }; // renders our application render(app); // checks if component has been updated // accepts changes , replaced module. // it's checking if js has been changed... // @todo - works js not css. // think issue webpack not // recognizing bundle.css dependency? if (module.hot) { module.hot.accept(); }
you're using extract-text-webpack-plugin , after webpack rebuilds bundle webpack-dev-middleware thinks nothing changed, because corresponding module in bundle representing css empty content has been extracted.
you need disable extract-text-webpack-plugin in development hmr. can use disable option , fallback style-loader, injects <style> tags.
new extracttextplugin({ filename: 'bundle.css', allchunks: true, disable: true }) instead of having define 2 versions of plugin can use environment variables node_env=production , use in plugin:
new extracttextplugin({ filename: 'bundle.css', allchunks: true, disable: process.env.node_env !== 'production' })
Comments
Post a Comment