Webpack publicPath在HTMLWebpackPlugin创建的HTML中不可用

问题描述

我在一个项目上使用Webpack 4.44,同时我还使用HTMLWebpack插件创建了index.html。要在生产环境中使用我的代码,模板中的图像和外部CSS的几个相对资产路径必须以CDN的基本网址为前缀。

在这种情况下,我将output.publicPath设置为CDN的URL,并希望URL模板中的所有资产URL都带有前缀。但是,事实并非如此。

webpack.config.js

module.exports = {
  entry: './src/js/index.js',module: {
    rules: [
      {
        test: /\.(js)$/,exclude: /node_modules/,use: ['babel-loader']
      },]
  },resolve: {
    extensions: ['*','.js','.css','.*']
  },output: {
    filename: 'main.js',publicPath: 'https:/CDN_URL',path: path.resolve(__dirname,'dist'),},plugins: [
    new HtmlWebpackPlugin({
      title: 'title',template: './src/index.html',minify: false,}),],};

./ src / index.html

<section>
  <link rel="stylesheet" href="css/main.css">
</section>

./ dist / index.html

<section>
  <link rel="stylesheet" href="css/main.css">
</section>
<script src="https:/CDN_URL/main.js"></script>

如您所见,在正确捆绑和引用JS包时,引用的CSS没有前缀。 有人可以给我提示如何在HTMLWebpackPlugin中使publicPath可用吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)