Webpack-dev-server无法从html-webpack-plugin提取生成的HTML文件

时间:2019-04-12 09:13:18

标签: webpack webpack-dev-server html-webpack-plugin

我想为我的React应用程序创建一个简单的开发环境。我使用webpack-dev-serverhtml-webpack-plugin。我了解webpack-dev-server不会写文件,只会从内存中提供文件。当输出文件位于同一目录中时,它可以正常工作,但这是问题所在,我希望资产位于/dist文件夹中,而不是生成的/所在的index.html根文件夹中

以下是我要实现的目标的直观表示:

- public
  - dist
    * app.js
  * index.html
- webpack
  * dev.config.js

这是我用于相同目录模式的配置:

// webpack/dev.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const assetsPath = path.resolve(__dirname, '../public');

module.exports = {
  mode: 'development',
  entry: {
    app: './src/index.js'
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: assetsPath
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin()
  ],
  output: {
    path: assetsPath,
    filename: '[name]-[hash].js',
    publicPath: '/'
  }
};

这是我尝试使用不同目录模式的尝试:

// webpack/dev.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const assetsPath = path.resolve(__dirname, '../public/dist');
const publicPath = path.resolve(__dirname, '../public');

module.exports = {
  mode: 'development',
  entry: {
    app: './src/index.js'
  },
  output: {
    path: assetsPath,
    filename: '[name]-[hash].js',
    publicPath: '/dist'
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: publicPath,
    publicPath: '/dist'
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      filename: '../index.html'
    })
  ]
};

我尝试的配置有效,它将打开一个新的浏览器选项卡,但没有包含我期望的内容。它只显示./public文件夹的目录列表,其中./public/dist是空的,并且是预期的,因为开发服务器不会写入任何文件。

有人知道我该怎么解决吗?

0 个答案:

没有答案