<div id="root"></div> 未在 webpack 构建中传输
<div id="root"></div> not transfering in webpack build
我正在使用 Webpack 3.8.1 和 webpack-dev-server 2.9.2。这是我正在用 React 编写的一个项目。下面是我的 webpack.config 文件。每次我 运行 构建命令都不会传输到 dist 目录中的 index.html 文件。当我进行生产构建时也会发生这种情况。我可以将该行写入 index.html 文件,但每次重建时都会删除该行。有什么想法吗?
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: './dist',
hot: true
},
entry: {
app: './src/index.js',
vendor: ['react']
},
plugins: [
new CleanWebpackPlugin('dist'),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
title: 'Production'
}),
new ExtractTextPlugin({
filename: 'styles.css'
}),
new webpack.HotModuleReplacementPlugin({
multiStep: true
})
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash].js',
sourceMapFilename: 'bundle.js.map'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
'babel-loader'
]
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use:'css-loader'
})
},
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000'
},
{ test: /\.eot(\?v=\d+.\d+.\d+)?$/,
loader: 'file-loader'
},
{ test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(jpg|jpeg|gif|png)$/,
exclude: /node_modules/,
loader:'file-loader?name=img/[name].[ext]&context=./app/images'
},
{
test: /\.(ico)$/,
exclude: /node_modules/,
loader:'file-loader?name=[name].[ext]&context=./app/images'
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
}
};
这是因为 html-webpack-plugin。您没有指定,因此您看不到它。快速浏览 html-webpack-plugin doc 后,我建议您创建一个 template.html
文件,其中包含您在构建的 index.html 文件中所需的内容。
并将配置的插件部分更改为,
new HtmlWebpackPlugin({
title: 'Production'
template: 'template.html'
}),
这样你就可以 fill-in 你想要的所有 DOM 你想要的模板文件和你构建的 index.html
都会有。
要读取 template.html
中的配置变量(标题),只需使用
<title><%= htmlWebpackPlugin.options.title %></title>
如果您不想创建模板文件,您可以考虑使用插件 html-webpack-root-plugin,它将在生成的 HTML 中插入一个 <div id="root"></div>
。
npm install html-webpack-root-plugin --save-dev
然后,在您的 webpack.config.js:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackRootPlugin = require('html-webpack-root-plugin');
const config = {
...
plugins: [ new HtmlWebpackPlugin(), new HtmlWebpackRootPlugin() ]
...
}
现在,生成的 index.html 将在正文中包含 <div id="root"></div>
。
如果有人还在纠结于此,您需要指定模板的相对路径:
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
它从“src/index.html”中获取您的模板,并在“your_folder/index.html”中构建它。
我正在使用 Webpack 3.8.1 和 webpack-dev-server 2.9.2。这是我正在用 React 编写的一个项目。下面是我的 webpack.config 文件。每次我 运行 构建命令都不会传输到 dist 目录中的 index.html 文件。当我进行生产构建时也会发生这种情况。我可以将该行写入 index.html 文件,但每次重建时都会删除该行。有什么想法吗?
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: './dist',
hot: true
},
entry: {
app: './src/index.js',
vendor: ['react']
},
plugins: [
new CleanWebpackPlugin('dist'),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
title: 'Production'
}),
new ExtractTextPlugin({
filename: 'styles.css'
}),
new webpack.HotModuleReplacementPlugin({
multiStep: true
})
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash].js',
sourceMapFilename: 'bundle.js.map'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
'babel-loader'
]
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use:'css-loader'
})
},
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000'
},
{ test: /\.eot(\?v=\d+.\d+.\d+)?$/,
loader: 'file-loader'
},
{ test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(jpg|jpeg|gif|png)$/,
exclude: /node_modules/,
loader:'file-loader?name=img/[name].[ext]&context=./app/images'
},
{
test: /\.(ico)$/,
exclude: /node_modules/,
loader:'file-loader?name=[name].[ext]&context=./app/images'
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
}
};
这是因为 html-webpack-plugin。您没有指定,因此您看不到它。快速浏览 html-webpack-plugin doc 后,我建议您创建一个 template.html
文件,其中包含您在构建的 index.html 文件中所需的内容。
并将配置的插件部分更改为,
new HtmlWebpackPlugin({
title: 'Production'
template: 'template.html'
}),
这样你就可以 fill-in 你想要的所有 DOM 你想要的模板文件和你构建的 index.html
都会有。
要读取 template.html
中的配置变量(标题),只需使用
<title><%= htmlWebpackPlugin.options.title %></title>
如果您不想创建模板文件,您可以考虑使用插件 html-webpack-root-plugin,它将在生成的 HTML 中插入一个 <div id="root"></div>
。
npm install html-webpack-root-plugin --save-dev
然后,在您的 webpack.config.js:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackRootPlugin = require('html-webpack-root-plugin');
const config = {
...
plugins: [ new HtmlWebpackPlugin(), new HtmlWebpackRootPlugin() ]
...
}
现在,生成的 index.html 将在正文中包含 <div id="root"></div>
。
如果有人还在纠结于此,您需要指定模板的相对路径:
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
它从“src/index.html”中获取您的模板,并在“your_folder/index.html”中构建它。