unplugin-upload-cdn

Plugin for uploading resource files to cdn, Powered by on unplugin

Usage no npm install needed!

<script type="module">
  import unpluginUploadCdn from 'https://cdn.skypack.dev/unplugin-upload-cdn';
</script>

README

unplugin-upload-cdn

NPM version

Upload resource files to cdn. Powered by unplugin.

Install

npm i unplugin-upload-cdn

Provider

Support Tencent cos, Alibaba oss

COS Document OSS Document

import { unpluginUploadCDN, COS, OSS } from "unplugin-upload-cdn";

const cos = new COS({ /* options */ });
const oss = new OSS({ /* options */ });

import { unpluginUploadCDN, COS } from "unplugin-upload-cdn";

export default defineConfig({
  plugins: [
    unpluginUploadCDN.vite({
     /* options */
     provider: cos // oss,
    }),
  ],
})

The parameters of the environment variables are read by default

OSS

{
  accessKeyId: process.env.CDN_PLUGIN_ACCESSKEY_ID,
  accessKeySecret: process.env.CDN_PLUGIN_SECRET,
  bucket: process.env.CDN_PLUGIN_BUCKET,
  region: process.env.CDN_PLUGIN_REGION,
}

COS

{
  SecretId: process.env.CDN_PLUGIN_SECRET_ID,
  SecretKey: process.env.CDN_PLUGIN_SECRET_KEY,
  Bucket: process.env.CDN_PLUGIN_BUCKET,
  Region: process.env.CDN_PLUGIN_REGION,
}

Custom

implements IProvider

export interface IProvider {
  upload: (file: IFile) => void
  beforeUpload: (files: IResource[], existCheck?: boolean) => Promise<IResource[]>
}
Name Environment Variables Default Description
ignore CDN_PLUGIN_IGNORE [] ignore uploaded files, e.g. ["*/.html"]
dir null Uploaded directory
existCheck CDN_PLUGIN_EXISTCHECK null check if the file has been uploaded, judged by the file name
prefix null package.json.name prefix
useVersion CDN_PLUGIN_USE_VERSION false splice the version of package.json as a prefix
provider - null -

Options


Vite
// vite.config.ts
import { unpluginUploadCDN, COS } from "unplugin-upload-cdn";

export default defineConfig({
  plugins: [
    unpluginUploadCDN.vite({
     /* options */
    }),
  ],
})

Example: playground/


Rollup
// rollup.config.js
import { unpluginUploadCDN, COS } from "unplugin-upload-cdn";

export default {
  plugins: [
     unpluginUploadCDN.rollup({
      /* options */
    }),
  ],
}


Webpack

const { unpluginUploadCDN, COS } = require("unplugin-upload-cdn");

// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    unpluginUploadCDN.webpack({
      /* options */
    }),
  ]
}