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

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

csharp 复制代码
npm install --save-dev script-ext-html-webpack-plugin
csharp 复制代码
yarn add script-ext-html-webpack-plugin --dev

在weebpack中配置

vue.config.js

csharp 复制代码
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')
  }
};
相关推荐
@菜菜_达41 分钟前
jquery.inputmask插件介绍
前端·javascript·jquery
QuZhengRong41 分钟前
【Luck-Report】缓存
java·前端·后端·vue·excel
jiayong231 小时前
前端面试题库 - 浏览器与网络篇
前端·网络·面试
Csvn1 小时前
小程序开发:微信小程序与 uni-app 实战指南
前端
摸鱼小李上线了1 小时前
vue项目页面添加水印实现方法
前端·javascript·vue.js
砍材农夫1 小时前
物联网 基于netty构建mqtt协议规范(主题通配符订阅)
java·前端·javascript·物联网·netty
彩票管理中心秘书长1 小时前
智能体状态指示:何时思考、何时调用工具、何时出错
前端·后端·程序员
彩票管理中心秘书长1 小时前
React + TypeScript拆解一整套“AI 变现代码流程”
前端·后端·程序员
广州华水科技1 小时前
单北斗GNSS变形监测在基础设施安全中的应用与维护
前端
码途漫谈1 小时前
把前端组件做成一座小岛:Animal-Island-UI 的自然风 React 组件库拆解
前端·开源