vue+阿里云oss自动化部署并访问
vue+阿里云oss+自动化部署+访问准备ossvue准备1:vue-cli3.0创建的vue项目 如何创建点这里~2:购买阿里云服务器,开通oss,域名备案通过(三个都要)oss在阿里oss对象存储中创建bucket仓库配置打开oss域名解析返回创建的oss存储仓库,将整个解析完成的域名进行关联如有不懂可参考~vue安装依赖npm i webpack-aliyun-oss根目录创建oss.js文
·
准备
1:vue-cli3.0创建的vue项目 如何创建点这里~
2:购买阿里云服务器,开通oss,域名备案通过(三个都要)
oss
在阿里oss对象存储中创建bucket仓库
配置
打开oss域名解析
返回创建的oss存储仓库,将整个解析完成的域名进行关联
如有不懂可参考~
vue
安装依赖
npm i webpack-aliyun-oss
根目录创建oss.js文件
module.exports = {
region: 'oss-cn-hangzhou', //oss地区
accessKeyId: '********', //你的osskeyid值
accessKeySecret: '*********', //你的ossKeySecret值
bucket: 'ceshiwa' //创建的oss存储仓库名称
}
根目录创建vue.config.js文件
'use strict'
const path = require('path')
const Oss = require('./oss')
function resolve (dir) {
return path.join(__dirname, dir)
}
const name = '测试'
const port = 8080
module.exports = {
publicPath: './',
outputDir: 'dist',
assetsDir: 'static',
lintOnSave: false,
productionSourceMap: false,
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
}
},
configureWebpack: {
name: name,
resolve: {
alias: {
'@': resolve('src')
}
}
},
chainWebpack (config) {
config.plugins.delete('preload')
config.plugins.delete('prefetch')
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
config
//主要是这里的配置,直接复制整个config即可
//process.env.NODE_ENV判断不是测试环境
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end()
config.plugin('webpack-aliyun-oss')
.use(require('webpack-aliyun-oss'), [{
from: "./dist/**",
dist: "/",
region: Oss.region,
accessKeyId: Oss.accessKeyId,
accessKeySecret: Oss.accessKeySecret,
bucket: Oss.bucket,
setOssPath: filePath => {
let index = filePath.lastIndexOf("dist");
let Path = filePath.substring(index + 4, filePath.length);
return Path.replace(/\\/g, "/");
}
}])
config
.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
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')
}
)
}
}
项目结构如下
再项目终端执行打包命令
npm run build
报错
如出现如上报错,终端执行npm i script-ext-html-webpack-plugin
重新打包上传即可,出现下面即为打包成功
oss仓库内容更新
部署成功后访问
更多推荐
已为社区贡献1条内容
所有评论(0)