-
Notifications
You must be signed in to change notification settings - Fork 473
build: 公共依赖cdn解耦并增加开关控制 #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
986d7f8
build: external public cdn localize 公共cdn依赖包支持本地化
rhlin 7659116
build: cdn外部依赖解耦,修复baseUrl获取
rhlin d3d6824
build: cdn依赖解耦 修正baseUrl获取方式
rhlin 7973917
build: cdn依赖三方解耦,优化目录型拷贝
rhlin 2d3c574
build: cdn依赖解耦,解决目录复制文件的相对路径问题
rhlin c8712a7
build(design-core): 修复monaco的worker使用cdn地址前缀的情况下打包失效
rhlin 4e1cc0b
build: 本地开发也支持脱离cdn三方依赖
rhlin 9fce65d
build: 去除代码注释
rhlin 9ccd0e5
build: 三方cdn依赖解耦 物料bundle文件依赖支持替换cdn依赖
rhlin 64413c2
build(design-core) 去除冗余用不到的文件和配置项硬编码
rhlin 75929ec
build: 公共依赖cdn解耦测试完成 开关默认关闭
rhlin cf6da9d
refactor: 简化三方cdn依赖解耦的正则,改为path方法
rhlin ba19f07
refactor: 三方依赖解耦脚本代码优化,减少雷同的正则匹配
rhlin 73ffc68
refactor: 三方cdn依赖解耦脚本 简化合并正则
rhlin ea5156d
Merge remote-tracking branch 'upstream/develop' into build-toggle-cdn
rhlin 2e385d0
build: 三方cdn解耦 物料本地打包逻辑优化修正
rhlin c92d08f
build: 三方cdn依赖解耦,优化代码简化正则,修复计算版本号问题,修复函数名大小写问题
rhlin 3f85722
build: 优化importMap版本站位符号
rhlin fb47d08
build: 三方cdn物料解耦 变量改为环境变量
rhlin f0cf45d
build: 三方cdn依赖解耦 补充处理复制文件夹时候的去重
rhlin e2deede
fix: 三方cdn解耦, 解决本地启动问题
rhlin 676b366
Merge remote-tracking branch 'upstream/develop' into build-toggle-cdn
rhlin ea9778c
feat(preview): preview也支持cdn依赖解耦
rhlin ddecae8
refactor(desing-core/scripts): 重构复制cdn文件本地复制模块
rhlin e685ee7
build: 重构预览的importMap复制逻辑,修复两个importMap文件不存在
rhlin 22a2101
build: 进一步优化monacoEditor地址,直接打包不再跨网络获取
rhlin 6e93c1a
refactor(design-core/scripts): 调整复制应用importMap函数的参数顺序,与其他结构类似
rhlin 3d267b1
refactor(design-core/script): 调整文件位置和文件引用顺序
rhlin 7cd40c8
feat(design-core/preview): preview importMap.js 使用 importMap.json 数据归一
rhlin f95106d
fix(design-core/preview): 修复importJson的引用
rhlin 1a6bdf9
fix: mock端口号订正,解决element-plus拖入画布后无法渲染
rhlin a0400f9
build(design-core/script): 修复临时安装包插件安装完包后返回目录不正确
rhlin f99deb5
fix: 修正monaco-editor的worker资源的打包地址
rhlin 6e74402
build(design-core/script): 修复临时安装包插件安装完包后返回目录不正确
rhlin a1d7fb3
fix(design-core/preview): 修正cdn解耦preview获取动态的importMap.json的base路径问题
rhlin 7246165
build: 三方cdn解耦,优化拷贝脚本,修复文件夹当文件拷贝当文件夹是包路径时候目标路径版本号丢失
rhlin 7c499f2
build: 三方cdn解耦,解决包未安装的情况下,glob匹配不到文件导致打包不拷贝内容
rhlin a9de8cd
fix: 根据检视意见使用fs-extra readJsonSync替代utils工具函数
rhlin c892c7a
fix: 根据检视意见修改函数名大小写和端口号读取
rhlin c71b24f
feat: 三方物料解耦 preview变量替换增加注释
rhlin dc1ad0f
feat: 根据检视意见修改函数名字
rhlin 707a075
refactor: 根据检视意见,将脚本函数的参数整改为对象,并把目录值迁移到参数默认值
rhlin d9b6710
docs(design-core/scripts): 三方cdn解耦 修正描述错误
rhlin b94d758
Merge remote-tracking branch 'upstream/develop' into build-toggle-cdn
rhlin 85e42b2
fix: 三方cdn解耦 修正preview脚本TinyVue版本号
rhlin e11c76c
Merge remote-tracking branch 'upstream/develop' into build-toggle-cdn
rhlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/design-core/scripts/localCdnFile/copyBundleDeps.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import path from 'node:path' | ||
| import { readJsonSync } from 'fs-extra' | ||
| import { installPackageTemporary } from '../vite-plugins/installPackageTemporary' | ||
| import { configServerAddProxy } from '../vite-plugins/configureServerAddProxy' | ||
| import { viteStaticCopy } from 'vite-plugin-static-copy' | ||
| import { | ||
| getCdnPathNpmInfoForSingleFile, | ||
| getPackageNeedToInstallAndFilesUsingSameVersion, | ||
| dedupeCopyFiles, | ||
| copyfileToDynamicSrcMapper | ||
| } from './locateCdnNpmInfo' | ||
|
|
||
| export function extraBundleCdnLink(filename, originCdnPrefix) { | ||
| const result = [] | ||
| const bundle = readJsonSync(filename) | ||
| bundle.data?.materials?.components?.forEach((component) => { | ||
| if (component.npm) { | ||
| const possibleUrl = [component.npm.script, component.npm.css] | ||
| possibleUrl.forEach((url) => { | ||
| if (url?.startsWith(originCdnPrefix) && !result.includes(url)) { | ||
| result.push(url) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| return result | ||
| } | ||
|
|
||
| export function replaceBundleCdnLink(bundle, fileMap) { | ||
| bundle.data?.materials?.components?.forEach((component) => { | ||
| if (component.npm) { | ||
| const possibleUrl = ['script', 'css'] | ||
| possibleUrl.forEach((key) => { | ||
| const matchRule = fileMap.find((rule) => component.npm[key] === rule.originUrl) | ||
| if (matchRule) { | ||
| component.npm[key] = matchRule.newUrl | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| export function copyBundleDeps({ | ||
| bundleFile, | ||
| targetBundleFile, | ||
| originCdnPrefix, | ||
| base, | ||
| dir = 'material-static', | ||
| bundleTempDir = 'bundle-deps/material-static' | ||
| }) { | ||
| const cdnFiles = extraBundleCdnLink(bundleFile, originCdnPrefix).map((url) => | ||
| getCdnPathNpmInfoForSingleFile(url, originCdnPrefix, base, dir, false, bundleTempDir) | ||
| ) | ||
| const { packages: packageNeedToInstall, files } = getPackageNeedToInstallAndFilesUsingSameVersion(cdnFiles) | ||
|
|
||
| const plugin = (isDev) => { | ||
| return [ | ||
| ...(isDev ? configServerAddProxy(targetBundleFile, targetBundleFile.replace(/\.([^.]+?$)/, '-local.$1')) : []), | ||
| ...installPackageTemporary(packageNeedToInstall, bundleTempDir), | ||
| ...viteStaticCopy({ | ||
| targets: [ | ||
| ...dedupeCopyFiles(files).map(copyfileToDynamicSrcMapper), | ||
| { | ||
| src: bundleFile, | ||
| dest: path.dirname(targetBundleFile), | ||
| transform: (content) => { | ||
| const json = JSON.parse(content) | ||
| replaceBundleCdnLink(json, files) | ||
| return JSON.stringify(json, null, 2) | ||
| }, | ||
| rename: (filename, fileExtension) => | ||
| isDev ? `${filename}-local.${fileExtension}` : path.basename(targetBundleFile), | ||
| overwrite: true // 覆盖public的 | ||
| } | ||
| ] | ||
| }) | ||
| ] | ||
| } | ||
| return { | ||
| plugin | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
packages/design-core/scripts/localCdnFile/copyImportMap.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { viteStaticCopy } from 'vite-plugin-static-copy' | ||
| import { | ||
| getPackageNeedToInstallAndFilesUsingSameVersion, | ||
| copyfileToDynamicSrcMapper, | ||
| dedupeCopyFiles, | ||
| getCdnPathNpmInfoForPackage, | ||
| getCdnPathNpmInfoForSingleFile | ||
| } from './locateCdnNpmInfo' | ||
| import { importmapPlugin } from '../externalDeps' | ||
| import { installPackageTemporary } from '../vite-plugins/installPackageTemporary' | ||
|
|
||
| export const copyLocalImportMap = ({ | ||
| importMap, | ||
| styleUrls, | ||
| originCdnPrefix, | ||
| base, | ||
| dir = 'import-map-static', | ||
| bundleTempDir = 'bundle-deps/design-core-import-map', | ||
| packageCopy = [] // 值为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, base, dir, true, bundleTempDir) | ||
| } | ||
| return getCdnPathNpmInfoForSingleFile(libPath, originCdnPrefix, base, dir, false, bundleTempDir) | ||
| }) | ||
| const styleFiles = styleUrls | ||
| .filter((styleUrl) => styleUrl.startsWith(originCdnPrefix)) | ||
| .map((url) => getCdnPathNpmInfoForSingleFile(url, originCdnPrefix, base, dir, false), bundleTempDir) | ||
|
|
||
| const { packages: packageNeedToInstall, files } = getPackageNeedToInstallAndFilesUsingSameVersion( | ||
| importMapFiles.concat(styleFiles) | ||
| ) | ||
|
|
||
| return [ | ||
| ...installPackageTemporary(packageNeedToInstall, bundleTempDir), | ||
| ...viteStaticCopy({ | ||
| targets: dedupeCopyFiles(files).map(copyfileToDynamicSrcMapper) | ||
| }), | ||
| { | ||
| config(config, { command }) { | ||
| // 处理devAlias带CDN域名, 另外需要使得本地vue和importMap的vue是同一个实例 | ||
| if (command === 'serve') { | ||
| config.resolve.alias = [ | ||
| ...config.resolve.alias, | ||
| { | ||
| find: /^vue$/, | ||
| replacement: `http://localhost:${config.server.port || 8080}/${ | ||
| files.find(({ originUrl }) => importMap.imports.vue === originUrl).newUrl | ||
| }` // 实际端口号需要更具本地启动修改 | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| importmapPlugin( | ||
| { | ||
| imports: Object.fromEntries( | ||
| Object.entries(importMap.imports).map(([k, v]) => [k, files.find((f) => f.originUrl === v)?.newUrl ?? v]) | ||
| ) | ||
| }, | ||
| styleUrls.map((url) => styleFiles.find((f) => f.originUrl === url).newUrl ?? url) | ||
| ) | ||
| ] | ||
| } | ||
82 changes: 82 additions & 0 deletions
82
packages/design-core/scripts/localCdnFile/copyPreviewImportMap.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import path from 'node:path' | ||
| import { readJsonSync } from 'fs-extra' | ||
| import { | ||
| getPackageNeedToInstallAndFilesUsingSameVersion, | ||
| copyfileToDynamicSrcMapper, | ||
| dedupeCopyFiles, | ||
| getCdnPathNpmInfoForPackage, | ||
| getCdnPathNpmInfoForSingleFile | ||
| } from './locateCdnNpmInfo' | ||
| import { viteStaticCopy } from 'vite-plugin-static-copy' | ||
| import { installPackageTemporary } from '../vite-plugins/installPackageTemporary' | ||
|
|
||
| export function extraPreviewImport(filename, originCdnPrefix) { | ||
| const result = [] | ||
| const importMap = readJsonSync(filename) | ||
| Object.entries(importMap.imports)?.forEach(([_key, location]) => { | ||
| const url = location.replace('${VITE_CDN_DOMAIN}', originCdnPrefix).replace('${opentinyVueVersion}', '~3.14') | ||
| if (url?.startsWith(originCdnPrefix) && !result.includes(url)) { | ||
| result.push(url) | ||
| } | ||
| }) | ||
| return result | ||
| } | ||
|
|
||
| export function replacePreviewImport(importMap, fileMap, originCdnPrefix) { | ||
| return { | ||
| imports: Object.fromEntries( | ||
| Object.entries(importMap.imports)?.map(([key, location]) => { | ||
| // 这里的替换占位符规则等同于packages/design-core/src/preview/src/preview/importMap.js, 两边修改需要同步 | ||
| const url = location.replace('${VITE_CDN_DOMAIN}', originCdnPrefix).replace('${opentinyVueVersion}', '~3.14') | ||
| const matchRule = fileMap.find((rule) => url === rule.originUrl) | ||
| if (matchRule) { | ||
| return [key, matchRule.newUrl] | ||
| } | ||
| return [key, location] | ||
| }) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export function extraPreviewImportFile(filename, targetFileName, originCdnPrefix) { | ||
| return (fileMap) => [ | ||
| { | ||
| src: filename, | ||
| dest: path.dirname(targetFileName), | ||
| transform: (content) => { | ||
| return JSON.stringify(replacePreviewImport(JSON.parse(content), fileMap, originCdnPrefix), null, 2) | ||
| }, | ||
| rename: path.basename(targetFileName) | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| export function copyPreviewImportMap({ | ||
| importMapJson, | ||
| targetImportMapJson, | ||
| originCdnPrefix, | ||
| base, | ||
| dir = 'preview-import-map-static', | ||
| bundleTempDir = 'bundle-deps/preview-import-map', | ||
| packageCopyLib = [] // 值为cdn地址上的包名 | ||
| }) { | ||
| const cdnFiles = extraPreviewImport(importMapJson, originCdnPrefix).map((url) => { | ||
| const { packageName } = url.match( | ||
| new RegExp(`^${originCdnPrefix}/?(?<packageName>.+?)@(?<versionDemand>[^/]+)(?<filePathInPackage>.*?)$`) | ||
| ).groups | ||
| if (packageCopyLib.includes(packageName)) { | ||
| return getCdnPathNpmInfoForPackage(url, originCdnPrefix, base, dir, true, bundleTempDir) | ||
| } | ||
| return getCdnPathNpmInfoForSingleFile(url, originCdnPrefix, base, dir, false, bundleTempDir) | ||
| }) | ||
| const { packages: packageNeedToInstall, files } = getPackageNeedToInstallAndFilesUsingSameVersion(cdnFiles) | ||
| return [ | ||
| ...installPackageTemporary(packageNeedToInstall, bundleTempDir), | ||
| ...viteStaticCopy({ | ||
| targets: [ | ||
| ...dedupeCopyFiles(files).map(copyfileToDynamicSrcMapper), | ||
| ...extraPreviewImportFile(importMapJson, targetImportMapJson, originCdnPrefix)(files) | ||
| ] | ||
| }) | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export * from './copyBundleDeps' | ||
| export * from './copyImportMap' | ||
| export * from './copyPreviewImportMap' | ||
| export * from './utils' | ||
| export * from './locateCdnNpmInfo' | ||
| export * from './replaceImportPath.mjs' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.