为webpack-dev-server设置资源路径

3

又浪费了一天的时间来配置带有热重载功能的webpack-dev-server。我想要在index.html中使用以下路径提供我的JS和CSS资源:/dist/js/app.min.js/dist/css/styles.min.css

我该如何在webpack.config.js中设置才能加载具有正确路径的index.html文件?当前的配置是,webpack-dev-server只有在/dist文件夹下生成构建文件,而在index.html中我必须设置不包含js或css文件夹的路径(例如:<script src="/dist/app.min.js" />)。

我的webpack.config.js文件内容:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  context: path.resolve(__dirname, './'),
  entry: './src/app.js',
  output: {
    publicPath: '/dist/',
    path: path.join(__dirname, '/dist/js'),
    filename: 'app.min.js'
  },
  devServer: {
    hot: true
  },
  resolve: {
    extensions: ['.js', '.jsx', '.ts', '.tsx']
  },
  module: {
    rules: [
      {
        test: /\.(scss|sass|css)$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      }
    ]
  },
  plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin()
  ]
};

我的项目结构:

项目结构

1个回答

0

我尝试了这个插件,但它将index.html放在dist/js文件夹中。如果我编辑index.html的渲染位置,那么webpack-dev-server会提供我的模板index.html。另外一件事,当我想要使用ExtractTextWebpackPlugin进行生产构建时,它不会添加任何CSS链接,所以我需要在我的index.html模板中指定该链接,但是这样webpack-dev-server就无法按预期工作。 - exoslav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接