vue-cli2打包上傳七牛雲腳本配置

打包腳本

var fs = require('fs');
var path = require('path');
var qiniu = require("qiniu");
//本身七牛雲的祕鑰
var accessKey = '';
var secretKey = '';
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);


var config = new qiniu.conf.Config();
// 空間對應的機房,zone_z1表明華北,其餘配置參見七牛雲文檔
config.zone = qiniu.zone.Zone_z1;
// 是否使用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 = `base-html:${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 {
            //music-base-h5/這裏是空間裏的文件前綴
            var key = 'music-base-h5/' + param.split('dist/')[1];
            var localFile = './' + param;
            //console.log(key,localFile);
            upload(key,localFile)
        }
    })
}

複製代碼

vue-cli配置

修改html內資源引用地址css

/config/index.js

    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    // assetsPublicPath: '/',   
    //修改成:
    assetsPublicPath: 'http://pntn6hym0.bkt.clouddn.com/music-base-h5',
複製代碼

修改css內的資源引用html

//更改 build/utils.js 文件中 ExtractTextPlugin 插件的options 配置:
if (options.extract) {
  return ExtractTextPlugin.extract({
    use: loaders,
    publicPath: '../../',         // 注意配置這一部分,根據目錄結構自由調整
    fallback: 'vue-style-loader'
  })
} else {
  return ['vue-style-loader'].concat(loaders)
}
複製代碼

總結

配置並無更改以前的打包生成結構,dist文件夾結構仍然是 vue

dist
引用路徑除了前綴cdn名稱,路徑也沒有改變
html引用路徑

只是咱們上傳到七牛的資源文件按引用路徑把前綴補全了 vue-cli

上傳後的文件名
相關文章
相關標籤/搜索