From 707a075675f784df97d6b8ee29b65e00105f46db Mon Sep 17 00:00:00 2001 From: rhlin Date: Sat, 11 May 2024 12:52:27 -0700 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=A0=B9=E6=8D=AE=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81=EF=BC=8C=E5=B0=86=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E5=8F=82=E6=95=B0=E6=95=B4=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=AF=B9=E8=B1=A1=EF=BC=8C=E5=B9=B6=E6=8A=8A=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=80=BC=E8=BF=81=E7=A7=BB=E5=88=B0=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scripts/localCdnFile/copyBundleDeps.js | 6 +-- .../scripts/localCdnFile/copyImportMap.js | 24 +++++------ .../localCdnFile/copyPreviewImportMap.js | 10 ++--- packages/design-core/vite.config.js | 43 +++++++++---------- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/packages/design-core/scripts/localCdnFile/copyBundleDeps.js b/packages/design-core/scripts/localCdnFile/copyBundleDeps.js index 5cb31e927..0f56353dc 100644 --- a/packages/design-core/scripts/localCdnFile/copyBundleDeps.js +++ b/packages/design-core/scripts/localCdnFile/copyBundleDeps.js @@ -40,14 +40,14 @@ export function replaceBundleCdnLink(bundle, fileMap) { }) } -export function copyBundleDeps( +export function copyBundleDeps({ bundleFile, targetBundleFile, originCdnPrefix, base, - dir, + dir = 'material-static', bundleTempDir = 'bundle-deps/material-static' -) { +}) { const cdnFiles = extraBundleCdnLink(bundleFile, originCdnPrefix).map((url) => getCdnPathNpmInfoForSingleFile(url, originCdnPrefix, base, dir, false, bundleTempDir) ) diff --git a/packages/design-core/scripts/localCdnFile/copyImportMap.js b/packages/design-core/scripts/localCdnFile/copyImportMap.js index 35d3fb275..4dab65ba7 100644 --- a/packages/design-core/scripts/localCdnFile/copyImportMap.js +++ b/packages/design-core/scripts/localCdnFile/copyImportMap.js @@ -9,26 +9,26 @@ import { import { importmapPlugin } from '../externalDeps' import { installPackageTemporary } from '../vite-plugins/installPackageTemporary' -export const copyLocalImportMap = ( +export const copyLocalImportMap = ({ importMap, - styles, + styleUrls, originCdnPrefix, - publicPath, - dir, - packageCopy, - bundleTempDir = 'bundle-deps/design-core-import-map' -) => { + base, + dir = 'import-map-static', + bundleTempDir = 'bundle-deps/design-core-import-map', + packageCopy = [] // key之为importMap的imports的左值 (非右值的地址上的包名) +}) => { const importMapFiles = Object.entries(importMap.imports) .filter(([_libKey, libPath]) => libPath.startsWith(originCdnPrefix)) .map(([libKey, libPath]) => { if (packageCopy.includes(libKey)) { - return getCdnPathNpmInfoForPackage(libPath, originCdnPrefix, publicPath, dir, true, bundleTempDir) + return getCdnPathNpmInfoForPackage(libPath, originCdnPrefix, base, dir, true, bundleTempDir) } - return getCdnPathNpmInfoForSingleFile(libPath, originCdnPrefix, publicPath, dir, false, bundleTempDir) + return getCdnPathNpmInfoForSingleFile(libPath, originCdnPrefix, base, dir, false, bundleTempDir) }) - const styleFiles = styles + const styleFiles = styleUrls .filter((styleUrl) => styleUrl.startsWith(originCdnPrefix)) - .map((url) => getCdnPathNpmInfoForSingleFile(url, originCdnPrefix, publicPath, dir, false), bundleTempDir) + .map((url) => getCdnPathNpmInfoForSingleFile(url, originCdnPrefix, base, dir, false), bundleTempDir) const { packages: packageNeedToInstall, files } = getPackageNeedToInstallAndFilesUsingSameVersion( importMapFiles.concat(styleFiles) @@ -61,7 +61,7 @@ export const copyLocalImportMap = ( Object.entries(importMap.imports).map(([k, v]) => [k, files.find((f) => f.originUrl === v)?.newUrl ?? v]) ) }, - styles.map((url) => styleFiles.find((f) => f.originUrl === url).newUrl ?? url) + styleUrls.map((url) => styleFiles.find((f) => f.originUrl === url).newUrl ?? url) ) ] } diff --git a/packages/design-core/scripts/localCdnFile/copyPreviewImportMap.js b/packages/design-core/scripts/localCdnFile/copyPreviewImportMap.js index 050a7aa5b..9f6a0e7ef 100644 --- a/packages/design-core/scripts/localCdnFile/copyPreviewImportMap.js +++ b/packages/design-core/scripts/localCdnFile/copyPreviewImportMap.js @@ -51,15 +51,15 @@ export function extraPreviewImportFile(filename, targetFileName, originCdnPrefix ] } -export function copyPreviewImportMap( +export function copyPreviewImportMap({ importMapJson, targetImportMapJson, originCdnPrefix, base, - dir, - packageCopyLib = [], - bundleTempDir = 'bundle-deps/preview-import-map' -) { + dir = 'preview-import-map-static', + bundleTempDir = 'bundle-deps/preview-import-map', + packageCopyLib = [] // key之为cdn地址上的包名 +}) { const cdnFiles = extraPreviewImport(importMapJson, originCdnPrefix).map((url) => { const { packageName } = url.match( new RegExp(`^${originCdnPrefix}/?(?.+?)@(?[^/]+)(?.*?)$`) diff --git a/packages/design-core/vite.config.js b/packages/design-core/vite.config.js index 62482c891..11bb4a742 100644 --- a/packages/design-core/vite.config.js +++ b/packages/design-core/vite.config.js @@ -288,40 +288,37 @@ export default defineConfig(({ command, mode }) => { monacoEditorPluginInstance, htmlPlugin(mode), isLocalImportMap - ? copyLocalImportMap( - importmap, - importMapStyles, - VITE_CDN_DOMAIN, - getBaseUrlFromCli(config.base), - 'import-map-static', - [ + ? copyLocalImportMap({ + importMap: importmap, + styleUrls: importMapStyles, + originCdnPrefix: VITE_CDN_DOMAIN, + base: getBaseUrlFromCli(config.base), + packageCopy: [ // 这两个包的js存在相对路径引用,不能单独拷贝一个文件,需要整个包拷贝 '@opentiny/vue-theme/theme-tool', '@opentiny/vue-theme/theme' ] - ) + }) : importmapPlugin(importmap, importMapStyles), isCopyBundleDeps - ? copyBundleDeps( - 'public/mock/bundle.json', - 'mock/bundle.json', - VITE_CDN_DOMAIN, // mock 中bundle的域名当前和环境的VITE_CDN_DOMAIN一致 - getBaseUrlFromCli(config.base), - 'material-static' - ).plugin(command === 'serve') + ? copyBundleDeps({ + bundleFile: 'public/mock/bundle.json', + targetBundleFile: 'mock/bundle.json', + originCdnPrefix: VITE_CDN_DOMAIN, // mock 中bundle的域名当前和环境的VITE_CDN_DOMAIN一致 + base: getBaseUrlFromCli(config.base) + }).plugin(command === 'serve') : [], isLocalImportMap - ? copyPreviewImportMap( - './src/preview/src/preview/importMap.json', - 'preview-import-map-static/preview-importmap.json', - VITE_CDN_DOMAIN, - getBaseUrlFromCli(config.base), - 'preview-import-map-static', - [ + ? copyPreviewImportMap({ + importMapJson: './src/preview/src/preview/importMap.json', + targetImportMapJson: 'preview-import-map-static/preview-importmap.json', + originCdnPrefix: VITE_CDN_DOMAIN, + base: getBaseUrlFromCli(config.base), + packageCopyLib: [ // 以下的js存在相对路径引用,不能单独拷贝一个文件,需要整个包拷贝 '@vue/devtools-api' ] - ) + }) : [] ) return config