Skip to content

Commit

Permalink
🔧 基础架构
Browse files Browse the repository at this point in the history
  • Loading branch information
lgldlk committed Jul 24, 2024
1 parent 7067136 commit 89a9181
Show file tree
Hide file tree
Showing 4 changed files with 4,256 additions and 5,225 deletions.
28 changes: 24 additions & 4 deletions packages/vite-add-cdn-script/lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import { PluginOption, UserConfig } from "vite";
import { BuildOptions, PluginOption, UserConfig, normalizePath } from "vite";
import { EEnforce, IOptions } from "./types";
import { libName } from "./config";
import { getExternalScript } from "cdn-script-core";
import glob from "glob";

import path from "path";
function viteAddCdnScript(opt: IOptions): PluginOption {
const { customScript = {}, defaultCdns = ["jsdelivr", "unpkg"] } = opt;
let _config: UserConfig;

let buildConfig: BuildOptions | undefined = undefined;
return {
name: libName,
enforce: EEnforce.PRE,
apply: "build",
config(confing) {
_config = confing;
config(config) {
_config = config;
buildConfig = config.build;
},
closeBundle: {
sequential: true,
order: "post",
async handler() {
if (!buildConfig || !opt.uploadFiles) return;
const outDirPath = normalizePath(path.resolve(normalizePath(buildConfig.outDir)));
const files = glob.sync(outDirPath + "/**/*", {
nodir: true,
dot: true,
ignore: "**/*.html",
});
// 上传文件
for (const file of files) {
opt.uploadFiles(file, {});
}
},
},
async transformIndexHtml(html) {
if (!defaultCdns || defaultCdns.length === 0) throw new Error("defaultCdns不能为空");
Expand Down
2 changes: 2 additions & 0 deletions packages/vite-add-cdn-script/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export interface IOptions {
customScript?: { [key: string]: string };
retryTimes?: number;
defaultCdns?: PropertyCdn[];

uploadFiles?: (filePath: string, info: {}) => {};
}
4 changes: 3 additions & 1 deletion packages/vite-add-cdn-script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"cdn-script-core": "workspace:*"
"@types/glob": "^8.1.0",
"cdn-script-core": "workspace:*",
"glob": "^11.0.0"
}
}
Loading

0 comments on commit 89a9181

Please sign in to comment.