项目中使用script-ext-html-webpack-plugin

script-ext插件在Webpack中的应用

使用 npm 或 yarn 安装 script-ext-html-webpack-plugin

npm install --save-dev script-ext-html-webpack-plugin

yarn add script-ext-html-webpack-plugin --dev

在weebpack中配置
vue.config.js


const webpack = require("webpack");
const publicPath={
  dev:'./',
}
function resolve(dir) {
  return path.join(__dirname, dir)
}
const path = require('path');
module.exports = {
  publicPath: './',
  devServer: {
    proxy: {
      [process.env.VUE_APP_URL]: {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          ['^' + process.env.VUE_APP_URL]: ''
        }
      },
      "/files": {
        target: "http://adds.org.cn/",
        changeOrigin: true,
      },
    },
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src') // 将 @ 指向 src 目录
      }
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env"],
            },
          },
        },
      ],
    },
  },
   chainWebpack(config) {
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])
    config.plugins.delete('prefetch')
    config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    config.module
      .rule('file-loader')
      .test(/\.(pdf|docx|doc|xlsx|xls|mp4)$/)
      .use('file-loader?name=[path][name].[ext]')
      .loader('file-loader')
      .end()
    config
      .plugin('ScriptExtHtmlWebpackPlugin')
      .after('html')
      .use('script-ext-html-webpack-plugin', [
        {
          inline: /runtime\..*\.js$/
        }
      ])
      .end()
    config.optimization.splitChunks({
      chunks: 'async',
      minSize: 30000,
      maxSize: 244000,
      minChunks: 1,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      automaticNameDelimiter: '~',
      cacheGroups: {
        defaultVendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        },
        libs: {
          name: 'chunk-libs',
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: 'initial' // only package third parties that are initially dependent
        },
        elementUI: {
          name: 'chunk-elementUI', // split elementUI into a single package
          priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
          test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
        },
        commons: {
          name: 'chunk-commons',
          test: resolve('src/components'), // can customize your rules
          minChunks: 3, //  minimum common number
          priority: 5,
          reuseExistingChunk: true
        }
      }
    })
    config.optimization.runtimeChunk('single')
  }
};

### script-ext-html-webpack-plugin 的最新版本 `script-ext-html-webpack-plugin` 是一个用于扩展 `html-webpack-plugin` 功能的插件,能够为生成的 HTML 文件添加额外的脚本属性。根据 npm 官方仓库的信息[^1],`script-ext-html-webpack-plugin` 的最新稳定版本为 `2.1.5`,但需要注意的是,此版本仅支持 `html-webpack-plugin@^3.0.0 || ^4.0.0`。 如果项目使用了 `html-webpack-plugin@5.x`,则需要寻找替代方案或升级到兼容的版本。目前,`script-ext-html-webpack-plugin` 尚未发布支持 `html-webpack-plugin@5.x` 的正式版本。可以通过以下方式解决依赖冲突问题: #### 方法一:降级 `html-webpack-plugin` 将 `html-webpack-plugin` 的版本降级至 `^4.0.0` 或 `^3.0.0`,以匹配 `script-ext-html-webpack-plugin@2.1.5` 的依赖要求。在 `package.json` 中修改依赖版本: ```json "devDependencies": { "html-webpack-plugin": "^4.0.0", "script-ext-html-webpack-plugin": "^2.1.5" } ``` 然后重新安装依赖: ```bash npm install ``` #### 方法二:寻找替代插件 由于 `script-ext-html-webpack-plugin` 未能及时更新以支持 `html-webpack-plugin@5.x`,可以考虑使用其他功能类似的插件,例如 `webpack-subresource-integrity` 或自定义 Webpack 插件来实现类似功能[^2]。 #### 方法三:手动调整插件代码 如果必须使用 `html-webpack-plugin@5.x` 和 `script-ext-html-webpack-plugin`,可以尝试克隆 `script-ext-html-webpack-plugin` 的源码并手动修改其对 `html-webpack-plugin` 的依赖版本。具体步骤如下: 1. 克隆插件源码: ```bash git clone https://github.com/numical/script-ext-html-webpack-plugin.git ``` 2. 修改 `package.json` 中的 `peerDependencies` 字段,允许 `html-webpack-plugin@5.x`。 3. 构建并链接插件到项目中: ```bash npm install npm link ``` #### 示例代码:验证安装后的版本 安装完成后,可以验证 `script-ext-html-webpack-plugin` 和 `html-webpack-plugin` 的版本是否正确: ```bash npm list script-ext-html-webpack-plugin html-webpack-plugin ``` ### 注意事项 - 强制安装(如 `--force` 或 `--legacy-peer-deps`)可能会导致潜在的兼容性问题,因此建议优先尝试调整依赖版本的方法。 - 如果项目使用了较新的 Webpack 版本(如 Webpack 5),需要确保所有相关插件都已更新至兼容版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值