【发布时间】:2017-01-08 16:00:34
【问题描述】:
我来自哪里
我使用 webpack 已经有一段时间了,但这是我第一次不为它修改入门工具包,而是尝试从头开始设置所有内容。
我遵循this nice article on survivejs.com 并继承了其中描述的一些方法,将它们与我早期的一些设置相结合(例如根据发送的生产变量设置不同的名称和路径)。
我尝试在讲座期间尽可能多地运行“构建”和“启动”,以免被一些我无法追踪的意外行为所困扰。这很好用。
然后在某个时间点,在摆弄关于excluding unused css from third party frameworks 的章节时我不知何故破坏了我的开发服务器设置。 ("run build" 仍然有效!)
问题
我的热服务器定义在 192.168.1.2:3000(这正是我想要的)。
当我使用下面进一步显示的设置运行webpack-dev-server 时,我在该位置得到 404 (Cannot GET /)。
现在我已经阅读了很多关于如何正确设置 webpack 开发服务器的信息,但我仍然没有正确理解......(很可能不是一个人在这里......)因为事实上我还没有完全理解路径是如何工作的。
这是我的理解(通用开发服务器设置):
(如果我错了,请帮助我)
-
webpack.config.js的输出路径将仅在运行开发服务器时在内存中提供,因此在物理上不可见(例如,如果输出为path: path.join(__dirname, 'devServerFolder',我将永远不会在我的项目结构中的任何位置看到该文件夹如果不是我创造的) - 在我用
publicPath和contentBase指向的文件夹中需要一个指向我的bundle.js的静态index.html(这可能是我需要创建的devServerFolder里面有index.html;通常在网络上的大多数设置中,相同的public文件夹用于保存生产版本) - 如果我使用
html-webpack-plugin,插件将自动为我生成该文件,而无需指向任何内容,因为 bundle.js 由插件写入其中
这些是真正困扰我理解的一些要点。我试过 lots 。 of 。 different 。 combinations 玩弄 publicPath 和 contentBase 设置,但没有运气重新建立我的开发服务器(首先工作正常)。
请注意,我在代码或使用 run build 编译我的构建时没有任何问题,它工作正常,就像它应该的那样。
配置
文件夹结构
webpack.test
|
| - app // App folder
|
| - index.js // Entry point
| - greet.js // Hello World called by index.js
| - sass // Sass assets folder
| - pug // Pug (formerly jade) template and assets folder
| - node_modules
| - public // Production output folder
| - webpack-config-chunks // Split up webpack configs folder
| - .babelrc
| - package.json
| - webpack.config.js
Webpack.config.js
// Irrelevant parts omitted
const paths: {
app: path.join(__dirname, 'app'), // The obvious one
dev: path.join(__dirname, 'bin'),
/* As mentioned above, from my understanding,
the output path for the dev server doesn't really matter at all
when using html-webpack-plugin, am I wrong here? */
build: path.join(__dirname, 'public') // Another obvious one
...
};
...
entry: {
app: production ? paths.app : [
'webpack-dev-server/client?192.168.1.2:3000',
'webpack/hot/only-dev-server',
paths.app
]
/* I can modify the entry to just be paths.app for every case
but it won't change a thing regarding my problem. */
},
output: {
path: production ? paths.build : paths.dev,
publicPath: production ? '' : paths.dev + '/',
/* ^ This is where I've tried just '' or paths.app or paths.build
instead of paths.dev and delete publicPath all together. */
filename: production ? '[name].[chunkhash].js' : '[name].js',
chunkFilename: '[chunkhash].js'
},
devServer: {
contentBase: paths.dev,
/* ^ This = "webpack.test/bin" – See above.
Tried several different paths here as well without luck. */
hot: true,
inline: true,
stats: 'minimal',
compress: true,
host: '192.168.1.2',
port: '3000',
},
plugins: [
new HtmlWebpackPlugin(),
new webpack.HotModuleReplacementPlugin(){
multiStep: true
}
]
...
【问题讨论】:
-
您找到问题了吗?
标签: configuration webpack webpack-dev-server html-webpack-plugin hot-module-replacement