项目结构与Webpack配置
//webpack.config.js
"use strict";
const path = require("path");
const webpack = require("webpack");
const htmlWebpackPlugin = require("html-webpack-plugin");
const srcPath = path.resolve(__dirname, "src/render");
const outPath = path.resolve(__dirname, "dist/render");
//判断是否生产模式
const isProduction = process.env.NODE_ENV === "production";
//awesome-typescript-loader config
const awesomeTSLoaderOptions = {
loader: "awesome-typescript-loader",
options: {
useBabel: true,
babelOptions: {
presets: ["es2015"]
}
}
};
module.exports = {
context: srcPath,
entry: {
render: "./app.tsx",
vendor: [
'react',
'react-dom',
// 'react-redux',
// 'react-router',
// 'react-router-redux',
// 'redux'
]
},
output: {
filename: "[name].js",
path: outPath,
publicPath: "",
},
module: {
rules: [
{
test: /\.tsx?$/,
// loader:"ts-loader",
use: isProduction
? awesomeTSLoaderOptions
: [
"react-hot-loader",
awesomeTSLoaderOptions
],
exclude: /node_modules/
}
]
},
target: 'web',
resolve: {
//引入模块时,会去查找这3个类型文件
extensions: ['.tsx', '.ts', '.js']
},
devServer: {
contentBase: outPath,
publicPath:""
},
//sourceMap 类型
devtool: "eval-source-map",
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
}),
new htmlWebpackPlugin({
template: "./index.html",
filename: "index.html"
})
]
};
//生产模式
if (isProduction) {
module.exports.devtool = "source-map";
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
//uglifyJs压缩
new webpack.optimize.UglifyJsPlugin({
sourceMap: true
})
]);
}
目录结构
|--src
|--render
|--index.html
|--app.tsx
|--dist
|--render
|--index.html
|--render.js
|--vendor.js
|--node_modules
|--webpack.config.js
|--package.json
问题描述
执行cross-env NODE_ENV=development webpack-dev-server -d --history-api-fallback --hot --inline --progress --colors --port 1235 --open"
后,webpack.dev.server打开站点后,修改文件,有在浏览器的Console中看到其获取接收文件变更信息,如[WDS] App updated. Recompiling...
[WDS] App hot update...
但页面内容并没有进行热加载,请问是我的webpack设置项编写的有问题吗?
备注
cross-env
模块为设定process.NODE_ENV为development,只是为了简化在不同平台下的设定操作。
提醒一下,
webpack-dev-server
带hot
参数的时候,要去掉config里面的 HotModuleReplacementPlugin不然会内存溢出。
另外,你这个问题。看配置,是没问题的。如果不更新页面,建议你在app.jsx 里面加上如下代码: