- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让 React-hot-loader 3 与 React-hot-loader 3、React-router 4 和 Webpack-hot-middleware(最新版本,2.18.2)一起工作。
这是我的server.js
:
const express = require('express');
const bodyParser = require('body-parser');
const cookiesMiddleware = require('universal-cookie-express');
/* eslint-disable import/no-extraneous-dependencies */
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackHotServerMiddleware = require('webpack-hot-server-middleware');
/* eslint-enable import/no-extraneous-dependencies */
const clientConfig = require('./webpack.config.dev.client');
const serverConfig = require('./webpack.config.dev.server');
const PORT_NUMBER = process.env.PORT || 3000;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookiesMiddleware());
app.use(express.static('public'));
const multiCompiler = webpack([clientConfig, serverConfig]);
const clientCompiler = multiCompiler.compilers[0];
app.use(webpackDevMiddleware(multiCompiler, {
publicPath: clientConfig.output.publicPath,
noInfo: true,
stats: { children: false },
}));
app.use(webpackHotMiddleware(clientCompiler));
app.use(webpackHotServerMiddleware(multiCompiler, {
serverRendererOptions: { outputPath: clientConfig.output.path },
}));
app.listen(PORT_NUMBER, () => {
// eslint-disable-next-line no-console
console.log(`Server listening at port ${PORT_NUMBER}`);
});
我的客户端入口点
:
import React from 'react';
import { render } from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import * as Bundles from './components/Bundles';
import App from './App';
const doRender = () => {
render(
<AppContainer>
<App type="client" />
</AppContainer>,
document.getElementById('content'),
);
};
const splitPoints = window.splitPoints || [];
Promise.all(splitPoints.map(chunk => Bundles[chunk].loadComponent()))
.then(doRender);
if (module.hot) {
module.hot.accept('./App', doRender);
}
.babelrc
:
{
"plugins": [
"transform-decorators-legacy",
"transform-object-rest-spread"
],
"presets": [
["es2015", { "modules": false }],
"react",
"stage-0"
],
"env": {
"development": {
"plugins": ["react-hot-loader/babel"]
},
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
看起来我遵循了 react-hot-loader's README 的每一步, 但每次我更改组件中的一些代码时,我都会在控制台中收到此消息:
[HMR] bundle rebuilding
client.js:207 [HMR] bundle rebuilt in 8218ms
process-update.js:27 [HMR] Checking for updates on the server...
process-update.js:81 [HMR] The following modules couldn't be hot updated: (Full reload needed)
This is usually because the modules which have changed (and their parents) do not know how to hot reload themselves. See http://webpack.github.io/docs/hot-module-replacement-with-webpack.html for more details.
有没有人偶然发现这个?提前致谢!
编辑:这是我的客户端 webpack 配置:
const path = require('path');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const notifier = require('node-notifier');
const configFileName = './.env.development.json';
let envConfig;
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
envConfig = require(configFileName);
} catch (e) {
envConfig = {};
}
const eslintSettings = {
extends: path.join(__dirname, '.eslintrc.js'),
configFile: path.join(__dirname, '.eslintrc.js'),
emitWarning: true,
cache: true,
};
const babelSettings = {
extends: path.join(__dirname, '.babelrc'),
cacheDirectory: true,
};
const excludes = [
/node_modules(?![/\\]@local-package[/\\])/,
];
const roots = [
path.join(__dirname, '../../node_modules'),
path.join(__dirname, 'node_modules'),
path.join(__dirname, 'client'),
];
const getCommonCSSLoaders = enableCSSModules => [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: enableCSSModules,
importLoaders: 1,
localIdentName: '[name]_[local]_[hash:base64:3]',
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
ident: 'postcss',
plugins: () => [
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
require('postcss-flexbugs-fixes'),
autoprefixer({
env: 'development',
flexbox: 'no-2009',
}),
],
},
},
];
const rules = [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: eslintSettings,
},
{
test: /\.js$/,
exclude: excludes,
loader: 'babel-loader',
options: babelSettings,
},
{
test: /\.css$/,
exclude: excludes,
use: [
...getCommonCSSLoaders(true),
],
},
{
test: /\.css$/,
include: excludes,
use: [
...getCommonCSSLoaders(false),
],
},
{
test: /\.scss$/,
exclude: excludes,
use: [
...getCommonCSSLoaders(true),
{
loader: 'resolve-url-loader',
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /.*\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
name: 'images/[name].[hash].[ext]',
limit: 20000,
},
},
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
quality: 80,
},
pngquant: {
quality: '80-90',
},
bypassOnDebug: true,
},
},
],
},
];
const plugins = [
new webpack.LoaderOptionsPlugin({
debug: true,
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new StyleLintPlugin({
configFile: path.join(__dirname, '.stylelintrc.js'),
files: [
'**/*.s?(a|c)ss',
'../shared/**/*.s?(a|c)ss',
],
emitErrors: false,
}),
new webpack.NormalModuleReplacementPlugin(/\/components\/Bundles/, './components/AsyncBundles'),
new webpack.NormalModuleReplacementPlugin(/\/Bundles/, './AsyncBundles'),
new webpack.optimize.CommonsChunkPlugin({
name: 'client',
async: 'common',
children: true,
minChunks: (module, count) => {
if (module.resource && (/^.*\.(css|scss)$/).test(module.resource)) {
return false;
}
return count >= 3 && module.context && !module.context.includes('node_modules');
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'client',
children: true,
minChunks: module => module.context && module.context.includes('node_modules'),
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks: module => module.context && module.context.includes('node_modules'),
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// eslint-disable-next-line func-names
function () {
this.plugin('done', (stats) => {
notifier.notify({
title: 'Webpack : Build Succeeded',
message: `${stats.compilation.errors.length} Error(s) - ${stats.compilation.warnings.length} Warning(s)`,
});
});
this.plugin('failed', () => {
notifier.notify({
title: 'Webpack',
message: 'Build Failed HARD',
});
});
},
];
const config = {
name: 'client',
target: 'web',
devtool: 'inline-source-map',
entry: ['webpack-hot-middleware/client', 'react-hot-loader/patch', './client/src/entry/js/polyfills', './client/src/entry/js/client'],
output: {
filename: 'client/[name].js',
chunkFilename: 'client/chunks/[name].chunk.js',
path: path.join(__dirname, 'public/dist'),
publicPath: '/dist/',
pathinfo: true,
},
module: {
rules,
},
plugins,
resolve: {
modules: roots,
},
resolveLoader: {
modules: roots,
},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
},
};
module.exports = config;
最佳答案
也许它与您的 webpack.config 文件有关?您的条目中是否设置了热门内容?
const config = {
entry: [
'babel-polyfill',
'react-hot-loader/patch', // activate HMR for React
`webpack-hot-middleware/client?path=http://${HOST}:${PORT}/__webpack_hmr`, // bundle the client for webpack-hot-middleware and connect to the provided endpoint
'./src/client.jsx',
],
我使用 Hapi 作为带有 webpack-hot-middleware 和 webpack-dev-middleware 的服务器时运气更好。这是一个片段
import Webpack from 'webpack';
import HapiWebpackPlugin from 'hapi-webpack-plugin';
const config = require('../../../webpack.config.js'); // eslint-disable-line global-require
const compiler = Webpack(config);
const options = {
assets: {
// webpack-dev-middleware options - https://github.com/webpack/webpack-dev-middleware
index: '/public/index.html',
},
hot: {
// webpack-hot-middleware options - https://github.com/glenjamin/webpack-hot-middleware
},
compiler,
};
server.register({
register: HapiWebpackPlugin,
options,
}, (error) => {
if (error) {
console.error(error);
}
});
如果你想尝试 Hapi,请查看我的 hapi-react-hot-loader-example
这是我完整的 webpack.config
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const RobotstxtPlugin = require('robotstxt-webpack-plugin').default;
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || 'localhost';
const NODE_ENV = process.env.NODE_ENV || 'production';
const isProduction = (NODE_ENV === 'production');
const isDevelopment = (NODE_ENV === 'development');
const config = {
entry: isDevelopment
? [
'babel-polyfill',
'react-hot-loader/patch', // activate HMR for React
`webpack-hot-middleware/client?path=http://${HOST}:${PORT}/__webpack_hmr`, // bundle the client for webpack-hot-middleware and connect to the provided endpoint
'./src/client.jsx',
]
: [
'babel-polyfill',
'./src/client.jsx',
],
resolve: {
extensions: ['.js', '.jsx', '.json'],
},
output: {
path: path.join(__dirname, 'dist/public/'),
filename: isDevelopment
? 'main.js'
: 'assets/scripts/[name].[chunkhash].js',
},
module: {
rules: [
{
test: /\.css$/,
use: ['css-hot-loader'].concat(
ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {minimize: true},
},
{
loader: 'postcss-loader',
},
],
})
),
},
{
test: /\.jsx?$/,
use: ['babel-loader'],
include: path.join(__dirname, 'src'),
},
],
},
plugins: [
new ProgressBarPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
}),
isDevelopment
? null
: new webpack.optimize.ModuleConcatenationPlugin(),
isDevelopment
? new webpack.HotModuleReplacementPlugin() // enable HMR globally
: null,
isDevelopment
? new webpack.NamedModulesPlugin() // prints more readable module names in the browser console on HMR updates
: null,
isDevelopment
? new webpack.NoEmitOnErrorsPlugin() // do not emit compiled assets that include errors
: null,
new ExtractTextPlugin({
filename: isDevelopment
? 'assets/styles/main.css'
: 'assets/styles/[name].[chunkhash].css',
}),
isDevelopment
? null
: new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: module => /node_modules/.test(module.resource),
}),
isDevelopment
? null
: new webpack.optimize.CommonsChunkPlugin({name: 'manifest'}),
isProduction
? new webpack.optimize.UglifyJsPlugin()
: null,
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src/index.html'),
minify: isProduction ? {collapseWhitespace: true, collapseInlineTagWhitespace: true} : false,
alwaysWriteToDisk: true,
}),
new HtmlWebpackHarddiskPlugin(),
new CopyWebpackPlugin([
{
context: 'src/assets/media',
from: '**/*',
to: 'assets/media',
},
]),
new RobotstxtPlugin({
policy: [
isProduction
? {userAgent: '*', allow: '/'}
: {userAgent: '*', disallow: '/'},
],
}),
].filter(Boolean),
devtool: isProduction
? 'none'
: 'cheap-module-eval-source-map',
performance: {
maxAssetSize: 500000,
},
};
module.exports = config;
关于reactjs - 使用 React-hot-loader 3、React-router 4 和 Webpack-hot-middleware 进行 React Hot Reload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678452/
我正在使用 webpack 开始我的学习之路,但遇到了一个问题,我认为这是由 extract-loader 引起的。 .当webpack抓取我的HTML文件时,似乎无法正常编译,在使用import.m
我正在使用 tomcat 8,在 tomcat-home/config/catalina.properties ,有关于的部分 common.loader, server.loader and sha
在使用 import 语句时,我对区分 sass-loader 和 css-loader 有点困惑。据我所知,css loader resolve import statment(@import) 和
我的 webpack 加载器数组中有这个加载器: { test: /\.scss$/, exclude: /node_modules/, loaders: ExtractTextPlugin('sty
我对 url-loader 、 file-loader 和 image-loader 感到很困惑。谁能解释一下 url-loader 、 file-loader 和 image-loader 的区别是
我有 page.css @imports index.css。 page.css 和 index.css 都有 display: flex Webpack.config.js 包含: module:
我在 webpack 中使用生产模式构建的多入口点最终 bundle 中导出的多入口编译 js 文件始终包含加载器内容。如何消除它们以及为什么包含它们? 重现 git clone https://gi
模板加载器找到模板但未加载模板 TemplateDoesNotExist at /cardpayment/ cardpayment.html Request Method: GET Reque
当我尝试运行 gradle tR (tomcatRun) 时出现此错误 A child container failed during start java.util.concurrent.Execu
Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/projectna
我计划将 Webpack 用于一个项目,并且我正在使用 Html-loader + file-loader 设置我的工作流程,以获取带有图像动态 src 的生产 html 文件,正如 Colt Ste
我有一个巨大的 csv 文件,其中包含数百万条记录,我想使用 python 脚本将它加载到 Netezza 数据库中。我尝试了简单的插入查询,但速度非常非常慢。可以指出一些示例 python 脚本或一
我想将 ts-loader 与 babel-polyfill 一起使用,但不使用 babel-loader。但是当我尝试构建该项目时,我收到了此错误。谁能告诉我我缺少什么。 ERROR in ./sr
下面是我的 webpack.config.js 和 package.json module.exports = { entry: "./entry.js", output: { fi
我在两台不同的 PC 上遇到了一个问题。对于我的项目,我为开发安装了以下依赖项:(webpack webpack-cli @babel/core @babel/preset-env @babel/pr
模板文件保存在app目录下,但渲染时引发TemplateDoesNotExist异常: 模板加载器事后分析如下: Django 尝试按以下顺序加载这些模板: Using loader django.t
PHPUnit 手册说: If you point the PHPUnit command-line test runner to a directory it will look for *Test
我正在开发一个需要 html 的角度应用程序要提取为纯 HTML 文件的文件,同时应检查任何 要求这些图像(作为 Assets )。另外,图片是基于根路径的(所以 /images/something.
我们在 sql 加载器中遇到了问题。我们正在尝试将一个大约 46 亿行(近 340 GB)的数据文件加载到 2 个 oracle 表中,基于一些使用 Sql Loader 的条件。但是在加载了 42
我将 CSS 模块与 webpack css-loader 一起使用,然后将它们与 mini-css-extract-plugin 捆绑在一起。 这是我的配置的样子: { test: /\.c
我是一名优秀的程序员,十分优秀!