使用 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')
}
};