【问题标题】:<div id="root"></div> not transfering in webpack build<div id="root"></div> 没有在 webpack 构建中传输
【发布时间】:2018-03-12 03:05:34
【问题描述】:

我正在使用 Webpack 3.8.1 和 webpack-dev-server 2.9.2。这是针对我正在编写的一个项目的反应。下面是我的 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']
  }
};

【问题讨论】:

    标签: webpack webpack-dev-server


    【解决方案1】:

    这是因为 html-webpack-plugin。您没有指定,因此您看不到它。在快速浏览 html-webpack-plugin doc 之后,我建议您在构建的 index.html 文件中创建一个包含所需内容的 template.html 文件。

    并将配置的插件部分更改为,

    new HtmlWebpackPlugin({
       title: 'Production'
       template: 'template.html'
    }),
    

    这样你就可以在你的模板文件中填写你想要的所有 DOM,你构建的index.html 就会有这些。

    要读取template.html 中的配置变量(标题),只需使用

    &lt;title&gt;&lt;%= htmlWebpackPlugin.options.title %&gt;&lt;/title&gt;

    【讨论】:

    • 谢谢。我将html-webpack-plugin 放在单独的developmentproduction webpack 文件中。但我只是在 production 配置上指定 favicon,而我在 common 文件上设置的模板。它似乎覆盖了common 配置。
    【解决方案2】:

    如果您不想创建模板文件,您可以考虑改用插件html-webpack-root-plugin,它会在生成的 HTML 中插入一个&lt;div id="root"&gt;&lt;/div&gt;

    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 的正文中将包含 &lt;div id="root"&gt;&lt;/div&gt;

    【讨论】:

      【解决方案3】:

      如果有人仍然对此感到困惑,您需要指定模板的相对路径:

       plugins: [
          new HtmlWebpackPlugin({
            template: 'src/index.html'
          })
        ]
      

      它从“src/index.html”中获取您的模板,并将其构建在“your_folder/index.html”中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 2016-10-20
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        相关资源
        最近更新 更多