@@ -38,6 +38,34 @@ export function copyfileToDynamicSrcMapper({ src, dest, transform, rename, folde
3838 }
3939}
4040
41+ function extractPackageInfo ( url , originCdnPrefix ) {
42+ let match = null
43+
44+ try {
45+ const mergedRegex = new RegExp (
46+ `^${ originCdnPrefix } /?` +
47+ // 包名捕获组(支持作用域包 @scope/name 格式)
48+ // (?:@[^/]+/)? 匹配 @scope/ 格式
49+ // [^/@]+ 匹配包名,不包含 '/' 和 '@'
50+ `(?<packageName>(?:@[^/]+/)?[^/@]+)` +
51+ // 版本号部分(@或/分隔)
52+ // /(?=.*/files) 匹配斜杠的分割的文件路径,但是需满足正向预查,确保后续路径包含 /files
53+ `(?:@|/(?=.*/files))` +
54+ // 捕获版本号
55+ `(?<versionDemand>[^/]+)` +
56+ // 路径部分 处理/files前缀(npmmirror)
57+ `(?:/files)?` +
58+ // 路径部分 匹配文件路径
59+ `(?<filePathInPackage>.*?)$`
60+ )
61+ match = url . match ( mergedRegex )
62+ } catch ( error ) {
63+ // ignore
64+ }
65+
66+ return match
67+ }
68+
4169// 生成复制单个文件所需要的信息
4270export function getCdnPathNpmInfoForSingleFile (
4371 url , // cdn托管的npm文件地址数组
@@ -48,24 +76,7 @@ export function getCdnPathNpmInfoForSingleFile(
4876 tempDir = 'bundle-deps' // 新安装包的安装目录
4977) {
5078 const baseSlash = base . endsWith ( '/' ) ? '' : '/'
51-
52- // 分别匹配 unpkg 和 npmmirror 格式
53- let unpkgMatch = null
54- let npmmirrorMatch = null
55-
56- try {
57- unpkgMatch = url . match (
58- new RegExp ( `^${ originCdnPrefix } /?(?<packageName>.+?)@(?<versionDemand>[^/]+)(?<filePathInPackage>.*?)$` )
59- )
60- npmmirrorMatch = url . match (
61- new RegExp ( `^${ originCdnPrefix } /?(?<packageName>.+?)/(?<versionDemand>[^/]+)/files(?<filePathInPackage>.*?)$` )
62- )
63- } catch ( error ) {
64- // ignore
65- }
66-
67- // 使用匹配到的结果
68- const match = npmmirrorMatch || unpkgMatch
79+ const match = extractPackageInfo ( url , originCdnPrefix )
6980
7081 if ( ! match ) {
7182 return null
@@ -132,17 +143,9 @@ export function getCdnPathNpmInfoForPackage(
132143 tempDir = 'bundle-deps' // 新安装包的安装目录
133144) {
134145 const baseSlash = base . endsWith ( '/' ) ? '' : '/'
135-
136- // 分别匹配 unpkg 和 npmmirror 格式
137- const unpkgMatch = url . match (
138- new RegExp ( `^${ originCdnPrefix } /?(?<packageName>.+?)@(?<versionDemand>[^/]+)(?<filePathInPackage>.*?)$` )
139- )
140- const npmmirrorMatch = url . match (
141- new RegExp ( `^${ originCdnPrefix } /?(?<packageName>.+?)/(?<versionDemand>[^/]+)/files(?<filePathInPackage>.*?)$` )
142- )
143-
144146 // 使用匹配到的结果
145- const match = npmmirrorMatch || unpkgMatch
147+ const match = extractPackageInfo ( url , originCdnPrefix )
148+
146149 if ( ! match ) {
147150 return null
148151 }
0 commit comments