【发布时间】:2018-05-24 10:39:55
【问题描述】:
我正在开发一个具有 CSS 样式表的 WordPress 插件。我决定从 Gulp 迁移到 Webpack,因为我打算在未来开始使用 React.js 进行开发,并认为开始习惯 Webpack 是一件好事。
所以我正在成功处理、最小化和提取我的 .scss 文件。与我的 .js 文件相同。问题是我正在尝试使用 BrowserSync 在 .php 文件更改时重新加载页面,并在每次 Webpack 构建新的 dist/*.css 文件时注入我的 css 更改,但 BrowserSync 每次有 css 时都会重新加载整个页面改变。奇怪的是,它在完全重新加载之前注入了更改。我已经尝试查看 .css 文件并在更改时执行 bs.reload({stream: true}) ,但它仍然会完全重新加载。
有人知道会发生什么吗?我怀疑是 BrowserSync 配置问题,因为它在完全重新加载之前有效地注入了更改,但我无法弄清楚是什么设置导致了这个问题。
这是 webpack.config.js:
const path = require('path');
const UglifyJS = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractSass = new ExtractTextPlugin({
filename: "../dist/[name].min.css"
});
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
module.exports = {
entry: './src/js/main.js',
output: {
filename: 'custom.min.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new UglifyJS({sourceMap: true}),
extractSass,
new BrowserSyncPlugin({
host: 'localhost',
port: '3000',
proxy: 'http://domain.localdev',
open: false,
files: [{
match: ['**/*.php'],
fn: function(event, file) {
if (event === "change") {
const bs = require('browser-sync').get('bs-webpack-plugin');
bs.reload();
}
}
},
{
match: ['dist/*.css', 'dist/*.js'],
fn: function(event, file) {
if (event === "change") {
const bs = require('browser-sync').get('bs-webpack-plugin');
bs.stream();
}
}
}],
injectChanges: true,
notify: true
})
],
externals: {
jquery: "jQuery"
},
devtool: "source-map",
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['babel-preset-env'],
sourceMap: true
}
}
},
{
test: /\.scss$/,
use: extractSass.extract({
use: [{
loader: "css-loader",
options: {
minimize: true,
sourceMap: true
}
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}],
fallback: "style-loader"
})
},
{
test: /\.css$/,
loader: 'style-loader',
},
{
test: /\.css$/,
loader: 'css-loader',
options: {
minimize: true
}
}
]
}
}
【问题讨论】:
标签: webpack sass browser-sync webpack-style-loader