| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- const OSS = require('ali-oss');
- const queue = require('queue');
- const fs = require('fs');
- const path = require('path');
- let client = new OSS({
- region: 'oss-cn-shenzhen',
- accessKeyId: '########################',
- accessKeySecret: '##############################',
- bucket: 'fashion-2022'
- });
- class UploadCDNPlugin {
- constructor(basePath) {
- this.basePath = basePath + '/';
- }
- // Define `apply` as its prototype method which is supplied with compiler as its argument
- apply(compiler) {
- compiler.hooks.done.tapAsync('UploadPlugin', (stats, callback) => {
- let basePath = this.basePath;
- const q = queue({ results: [], concurrency: 16 });
- function uploadDir(dir) {
- fs.readdirSync(dir).forEach(child => {
- if (/^\./.test(child)) return;
- let file = path.resolve(dir, child);
- let stat = fs.statSync(file);
- if (stat.isDirectory()) {
- uploadDir(file);
- } else {
- q.push(async cb => {
- let relpath = path.relative(path.resolve(__dirname, 'dist'), file);
- const result = await client.put(path.normalize(basePath + relpath), file);
- console.log(result);
- cb();
- });
- }
- });
- }
- uploadDir(path.resolve(__dirname, 'dist'));
- q.start(() => {
- callback();
- });
- });
- }
- }
- module.exports = UploadCDNPlugin;
|