1.七牛官網申請賬號
具體申請流程不詳細贅述了.最好申請一個自己的專屬檔案域名.
去阿里雲配置CNAME
2.修改專案index.js
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
// assetsPublicPath: '/',
assetsPublicPath: 'https://file.cfun.vip/file/',
//修改為自己的域名
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: true,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
//assetsPublicPath: '/',
assetsPublicPath: 'https://file.cfun.vip/file/',
proxyTable: {},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
最後打包後可以看到所有靜態檔案的字首都換了地址
3.建立指令碼上傳
在Vue專案根目錄新建一個qiniu.js檔案.
需要安裝node環境執行
var fs = require('fs');
var path = require('path');
var qiniu = require("qiniu");
//自己七牛雲的秘鑰
var accessKey = 'XXXXX'
var secretKey = 'XXXXXX';
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var config = new qiniu.conf.Config();
// 空間對應的機房,zone_z1代表華北,其他配置參見七牛雲文件
config.zone = qiniu.zone.Zone_z0;
// 是否使用https域名
//config.useHttpsDomain = true;
// 上傳是否使用cdn加速
//config.useCdnDomain = true;
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
main()
function main(){
displayFile('./dist')
}
//upload('static/css/app.qwer.css',"./dist/static/css/app.qwer.css")
function upload(key,localFile) {
//這裡base-html是儲存空間名
var Bucket = `cfun:${key}`;
var options = {
scope: Bucket,
MimeType:0,
};
var putPolicy = new qiniu.rs.PutPolicy(options);
var uploadToken = putPolicy.uploadToken(mac);
formUploader.putFile(uploadToken, key, localFile, putExtra, function (respErr,
respBody, respInfo) {
if (respErr) {
throw respErr;
}
if (respInfo.statusCode == 200) {
console.log(respBody);
} else {
console.log(respInfo.statusCode);
console.log(respBody);
if (respBody.error) {
console.log(respBody.error)
}
}
});
}
//遍歷資料夾
function displayFile(param) {
//轉換為絕對路徑
//var param = path.resolve(param);
fs.stat(param, function (err, stats) {
//如果是目錄的話,遍歷目錄下的檔案資訊
if (stats.isDirectory()) {
fs.readdir(param, function (err, file) {
file.forEach((e) => {
//遍歷之後遞迴呼叫檢視檔案函式
//遍歷目錄得到的檔名稱是不含路徑的,需要將前面的絕對路徑拼接
var absolutePath = path.join(param, e);
//var absolutePath = path.resolve(path.join(param, e));
displayFile(absolutePath)
})
})
} else {
//file/這裡是空間裡的檔案字首
var key ='file/' +param.split('dist/')[1];
var localFile = './' + param;
//console.log(key,localFile);
upload(key,localFile)
}
})
}
4.命令.即可上傳檔案
node qiniu.js
之後可以在你的對應域名的空間下看到對應的驚天檔案
5.開啟網站
發現執行速度快了很多,
注意:
node環境
七牛的對應安裝
七牛https是要付費
趕緊動手做起來吧,有什麼問題大家再一起探討
解決的痛點
雖然是個人網站部落格,但是你會發現首次進來打包生成的css檔案雖然就幾百K,但是載入依舊很耗時(伺服器騰訊雲最低配),所以就把靜態檔案都放到七牛上加速.如果哪位小夥伴有更好的解決辦法可以私聊.
本作品採用《CC 協議》,轉載必須註明作者和本文連結