Skip to content

Commit

Permalink
✨ 增加文件上传忽略参数
Browse files Browse the repository at this point in the history
  • Loading branch information
lgldlk committed Aug 4, 2024
1 parent 952652f commit 4cb196d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
20 changes: 12 additions & 8 deletions packages/vite-add-cdn-script/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function viteAddCdnScript(opt: IOptions): PluginOption {
const files = glob.sync(outDirPath + "/**/*", {
nodir: true,
dot: true,
ignore: "**/*.html",
ignore: opt.uploadIgnore || "**/*.html",
});

const upLoadRes = await Promise.all(
Expand All @@ -46,14 +46,18 @@ function viteAddCdnScript(opt: IOptions): PluginOption {
dot: true,
});
if (htmlFilePath.length === 0) return;
const htmlFile = htmlFilePath[0];
let html = fs.readFileSync(htmlFile, "utf-8");
for (const mainJsName of mainJsNames) {
const find = upLoadRes.find((item) => mainJsName.includes(item.fileName));
if (!find) continue;
html = html.replace(mainJsName, find.ossPath);
for (const htmlFile of htmlFilePath) {
if (files.includes(htmlFile)) {
continue;
}
let html = fs.readFileSync(htmlFile, "utf-8");
for (const mainJsName of mainJsNames) {
const find = upLoadRes.find((item) => mainJsName.includes(item.fileName));
if (!find) continue;
html = html.replace(mainJsName, find.ossPath);
}
fs.writeFileSync(htmlFile, html);
}
fs.writeFileSync(htmlFile, html);
} catch (error) {
console.error(`${libName} error:`, (error as Error).message);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-add-cdn-script/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export interface IOptions {
customScript?: { [key: string]: string };
retryTimes?: number;
defaultCdns?: PropertyCdn[];

uploadIgnore?: string;
uploadFiles?: (filePath: string, info: {}) => string | Promise<string>;
}
22 changes: 13 additions & 9 deletions packages/webpack-add-cdn-script/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ class WebpackAddCdnScript {
});

compiler.hooks.afterEmit.tapPromise(`${libName} upload`, async (compilation) => {
if (!compiler.options.output.path) return;
if (!compiler.options.output.path || !this.options.uploadFiles || !mainJsNames.length) return;
const outDirPath = path.resolve(normalizePath(compiler.options.output.path));
const files = glob.sync(outDirPath + "/**/*", {
nodir: true,
dot: true,
ignore: "**/*.html",
ignore: this.options.uploadIgnore || "**/*.html",
});
const upLoadRes = await Promise.all(
files.map(async (file) => ({
Expand All @@ -74,14 +74,18 @@ class WebpackAddCdnScript {
dot: true,
});
if (htmlFilePath.length === 0) return;
const htmlFile = htmlFilePath[0];
let html = fs.readFileSync(htmlFile, "utf-8");
for (const mainJsName of mainJsNames) {
const find = upLoadRes.find((item) => mainJsName.includes(item.fileName));
if (!find) continue;
html = html.replace(mainJsName, find.ossPath);
for (const htmlFile of htmlFilePath) {
if (files.includes(htmlFile)) {
continue;
}
let html = fs.readFileSync(htmlFile, "utf-8");
for (const mainJsName of mainJsNames) {
const find = upLoadRes.find((item) => mainJsName.includes(item.fileName));
if (!find) continue;
html = html.replace(mainJsName, find.ossPath);
}
fs.writeFileSync(htmlFile, html);
}
fs.writeFileSync(htmlFile, html);
});
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-add-cdn-script/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export interface IOptions {
defaultCdns?: PropertyCdn[];

external: string[];
uploadIgnore?: string;
uploadFiles?: (filePath: string, info: {}) => string | Promise<string>;
}

0 comments on commit 4cb196d

Please sign in to comment.