From 4cb196d9e849b62eb9c0e12414010f064d52b6f5 Mon Sep 17 00:00:00 2001 From: lgldlk <1851293758@qq.com> Date: Sun, 4 Aug 2024 23:00:55 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=A2=9E=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=BF=BD=E7=95=A5=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/vite-add-cdn-script/lib/main.ts | 20 +++++++++++------- packages/vite-add-cdn-script/lib/types.ts | 2 +- packages/webpack-add-cdn-script/lib/index.ts | 22 ++++++++++++-------- packages/webpack-add-cdn-script/lib/types.ts | 1 + 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/packages/vite-add-cdn-script/lib/main.ts b/packages/vite-add-cdn-script/lib/main.ts index 5fe07d9..df206c2 100644 --- a/packages/vite-add-cdn-script/lib/main.ts +++ b/packages/vite-add-cdn-script/lib/main.ts @@ -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( @@ -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); diff --git a/packages/vite-add-cdn-script/lib/types.ts b/packages/vite-add-cdn-script/lib/types.ts index d7ef958..57af33e 100644 --- a/packages/vite-add-cdn-script/lib/types.ts +++ b/packages/vite-add-cdn-script/lib/types.ts @@ -16,6 +16,6 @@ export interface IOptions { customScript?: { [key: string]: string }; retryTimes?: number; defaultCdns?: PropertyCdn[]; - + uploadIgnore?: string; uploadFiles?: (filePath: string, info: {}) => string | Promise; } diff --git a/packages/webpack-add-cdn-script/lib/index.ts b/packages/webpack-add-cdn-script/lib/index.ts index c1e3c48..78fef4d 100644 --- a/packages/webpack-add-cdn-script/lib/index.ts +++ b/packages/webpack-add-cdn-script/lib/index.ts @@ -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) => ({ @@ -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); }); } } diff --git a/packages/webpack-add-cdn-script/lib/types.ts b/packages/webpack-add-cdn-script/lib/types.ts index 4c79065..5b8873e 100644 --- a/packages/webpack-add-cdn-script/lib/types.ts +++ b/packages/webpack-add-cdn-script/lib/types.ts @@ -18,5 +18,6 @@ export interface IOptions { defaultCdns?: PropertyCdn[]; external: string[]; + uploadIgnore?: string; uploadFiles?: (filePath: string, info: {}) => string | Promise; }