@vuepress/plugin-replace-assets 2.0.0-rc.123 → 2.0.0-rc.125
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/node/index.d.ts +182 -0
- package/dist/node/index.js +2 -0
- package/dist/node/index.js.map +1 -0
- package/package.json +10 -8
- package/lib/node/index.d.ts +0 -177
- package/lib/node/index.js +0 -2
- package/lib/node/index.js.map +0 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { VitePlugin, WebpackPluginInstance } from "unplugin";
|
|
2
|
+
import { Plugin } from "vuepress/core";
|
|
3
|
+
|
|
4
|
+
//#region src/node/types.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Assets Replacement Target Path
|
|
7
|
+
* - `string`: Directly concatenated before the original path
|
|
8
|
+
* - `(url) => string`: Custom replacement method, returns the new path
|
|
9
|
+
*
|
|
10
|
+
* 资源替换目标路径
|
|
11
|
+
* - `string`: 直接拼接在原始路径前面
|
|
12
|
+
* - `(url) => string`: 自定义替换方法,返回新路径
|
|
13
|
+
*/
|
|
14
|
+
type Replacement = string | ((url: string) => string);
|
|
15
|
+
/**
|
|
16
|
+
* Assets Replacement Rule
|
|
17
|
+
*
|
|
18
|
+
* 资源替换规则
|
|
19
|
+
*/
|
|
20
|
+
interface ReplacementRule {
|
|
21
|
+
/**
|
|
22
|
+
* Assets Matching
|
|
23
|
+
*
|
|
24
|
+
* - `RegExp`: Match using regular expression
|
|
25
|
+
* - `string`: Match using string
|
|
26
|
+
* - Strings starting with `^` or ending with `$` are automatically converted to regular expressions
|
|
27
|
+
* - For ordinary strings, checks if they appear at the start or end
|
|
28
|
+
*
|
|
29
|
+
* 资源匹配
|
|
30
|
+
*
|
|
31
|
+
* - `RegExp`: 匹配正则
|
|
32
|
+
* - `string`: 匹配字符串
|
|
33
|
+
* - 以 `^` 开头或以 `$` 结尾的字符串,会自动转换为正则
|
|
34
|
+
* - 普通字符串检查是否以其作为开头或结尾
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```
|
|
38
|
+
* '^/foo/'
|
|
39
|
+
* '.jpg$'
|
|
40
|
+
* /\.jpg$/
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
find: RegExp | string;
|
|
44
|
+
/**
|
|
45
|
+
* Assets Replacement Target Path
|
|
46
|
+
* - `string`: Directly concatenated before the original path
|
|
47
|
+
* - `(url) => string`: Custom replacement method, returns the new path
|
|
48
|
+
*
|
|
49
|
+
* 资源替换目标路径
|
|
50
|
+
* - `string`: 直接拼接在原始路径前面
|
|
51
|
+
* - `(url) => string`: 自定义替换方法,返回新路径
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```
|
|
55
|
+
* 'https://example.com'
|
|
56
|
+
* (url) => `https://example.com${url}`
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
replacement: Replacement;
|
|
60
|
+
}
|
|
61
|
+
interface ReplaceAssetsOptions {
|
|
62
|
+
/**
|
|
63
|
+
* Custom Assets Replacement Rules
|
|
64
|
+
*
|
|
65
|
+
* 自定义资源替换规则
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* {
|
|
70
|
+
* rules: [{
|
|
71
|
+
* find: /^\/images\/.*\.(jpe?g|png|gif|svg)$/,
|
|
72
|
+
* replacement: 'https://cdn.example.com/images/',
|
|
73
|
+
* }]
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
rules?: ReplacementRule | ReplacementRule[];
|
|
78
|
+
/**
|
|
79
|
+
* Built-in image matching rules, designed to match and find common image paths starting with `^/images/`
|
|
80
|
+
*
|
|
81
|
+
* 内置的图片匹配规则,匹配查找 `^/images/` 开头的常见的图片路径
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```
|
|
85
|
+
* {
|
|
86
|
+
* image: 'https://example.com'
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
image?: Replacement;
|
|
91
|
+
/**
|
|
92
|
+
* Built-in media matching rules, designed to match and locate common media paths such as videos and audio that start with `^/medias/`.
|
|
93
|
+
*
|
|
94
|
+
* 内置的媒体匹配规则,匹配查找 `^/medias/` 开头的常见的视频、音频等媒体路径
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```
|
|
98
|
+
* {
|
|
99
|
+
* media: 'https://example.com'
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
media?: Replacement;
|
|
104
|
+
/**
|
|
105
|
+
* Equivalent to setting both {@link ReplaceAssetsOptions.image image} and {@link ReplaceAssetsOptions.media media} simultaneously.
|
|
106
|
+
*
|
|
107
|
+
* 相当于同时设置 {@link ReplaceAssetsOptions.image image} 和 {@link ReplaceAssetsOptions.media media}
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```
|
|
111
|
+
* {
|
|
112
|
+
* all: 'https://example.com'
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
all?: Replacement;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Assets Replacement Plugin Options
|
|
120
|
+
*
|
|
121
|
+
* 资源替换插件配置项
|
|
122
|
+
*/
|
|
123
|
+
type ReplaceAssetsPluginOptions = ReplaceAssetsOptions | Replacement | ReplacementRule | ReplacementRule[];
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/node/replaceAssetsPlugin.d.ts
|
|
126
|
+
/**
|
|
127
|
+
* Plugin for replacing assets path
|
|
128
|
+
*
|
|
129
|
+
* 资源路径替换插件
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```
|
|
133
|
+
* export default defineUserConfig({
|
|
134
|
+
* plugins: [
|
|
135
|
+
* replaceAssetsPlugin('https://cnd.example.com')
|
|
136
|
+
* ]
|
|
137
|
+
* })
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
declare const replaceAssetsPlugin: (options?: ReplaceAssetsPluginOptions) => Plugin;
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/node/normalizeRules.d.ts
|
|
143
|
+
declare const createFindPattern: (dir: string, extensions: string[]) => RegExp;
|
|
144
|
+
/**
|
|
145
|
+
* Normalize replacement rules
|
|
146
|
+
*
|
|
147
|
+
* @param options - The plugin options to normalize
|
|
148
|
+
* @returns Normalized replacement rules
|
|
149
|
+
*/
|
|
150
|
+
declare const normalizeRules: (options?: ReplaceAssetsPluginOptions) => ReplacementRule[];
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/node/transformAssets.d.ts
|
|
153
|
+
/**
|
|
154
|
+
* Check if url matches find
|
|
155
|
+
*
|
|
156
|
+
* @param find - The find pattern, can be a string or a RegExp / 查找模式,可以是字符串或正则表达式
|
|
157
|
+
* @param url - The URL to check / 要检查的 URL
|
|
158
|
+
* @returns True if the URL matches the find pattern, false otherwise / 如果 URL 匹配查找模式则返回 true,否则返回 false
|
|
159
|
+
*/
|
|
160
|
+
declare const isMatchUrl: (find: RegExp | string, url: string) => boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Replace asset with rules
|
|
163
|
+
*
|
|
164
|
+
* @param rules - Replacement rules / 替换规则
|
|
165
|
+
* @param url - The original asset URL / 原始资源 URL
|
|
166
|
+
* @returns The replaced asset URL if matched, otherwise return undefined / 如果匹配则返回替换后的资源 URL,否则返回 undefined
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* replacementAssetWithRules([{ find: '/foo/', replacement: 'https://example.com' }], '/foo/a.jpg')
|
|
171
|
+
* // -> 'https://example.com/foo/a.jpg'
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
declare const replacementAssetWithRules: (rules: ReplacementRule[], url: string) => string | void;
|
|
175
|
+
declare const transformAssets: (code: string, pattern: RegExp, rules: ReplacementRule[]) => string;
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/node/unplugin.d.ts
|
|
178
|
+
declare const createVitePluginReplaceAssets: () => (options: ReplacementRule[]) => VitePlugin | VitePlugin[];
|
|
179
|
+
declare const createWebpackPluginReplaceAssets: () => (options: ReplacementRule[]) => WebpackPluginInstance;
|
|
180
|
+
//#endregion
|
|
181
|
+
export { ReplaceAssetsOptions, ReplaceAssetsPluginOptions, Replacement, ReplacementRule, createFindPattern, createVitePluginReplaceAssets, createWebpackPluginReplaceAssets, isMatchUrl, normalizeRules, replaceAssetsPlugin, replacementAssetWithRules, transformAssets };
|
|
182
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{addViteConfig as e,configWebpack as t,getBundlerName as n,isArray as r,isFunction as i,removeEndingSlash as a,removeLeadingSlash as o}from"@vuepress/helper";import{createVitePlugin as s,createWebpackPlugin as c}from"unplugin";import l from"magic-string";const u=[`apng`,`bmp`,`png`,`jpe?g`,`jfif`,`pjpeg`,`pjp`,`gif`,`svg`,`ico`,`webp`,`avif`,`cur`,`jxl`],d=[`mp4`,`webm`,`ogg`,`mp3`,`wav`,`flac`,`aac`,`opus`,`mov`,`m4a`,`vtt`,`pdf`],f=(e,t)=>RegExp(`^/${e}/.*\\.(?:${t.join(`|`)})(\\?.*)?$`),p=e=>{let t=[];return e?((typeof e==`string`||i(e))&&(e={all:e}),r(e)?(t.push(...e),t):`find`in e?(e.find&&e.replacement&&t.push(e),t):(e.rules&&t.push(...r(e.rules)?e.rules:[e.rules]),(e.image||e.all)&&t.push({find:f(`images`,u),replacement:e.image||e.all}),(e.media||e.all)&&t.push({find:f(`medias`,d),replacement:e.media||e.all}),t)):[]},m=e=>{let t=`(${e}.*?)`;return new RegExp([`(?:"${t}")`,`(?:'${t}')`,`(?:\\(${t}\\))`,`(?:\\('${t}'\\))`,`(?:\\("${t}"\\))`,`(?:\\\\"${t}\\\\")`].join(`|`),`gu`)},h=(e,t)=>e?t?`${a(t)}/${o(e)}`:e:``,g=(e,t)=>typeof e==`string`?e.startsWith(`^`)||e.endsWith(`$`)?new RegExp(e).test(t):t.endsWith(e)||t.startsWith(e):e.test(t),_=new Map,v=(e,t)=>{if(_.has(t))return _.get(t);for(let{find:n,replacement:r}of e)if(n&&g(n,t)){let e=typeof r==`function`?h(r(t)):h(t,r);if(e)return _.set(t,e),e}},y=String.raw`\"`,b=(e,t,n)=>{let r=new l(e),i,a=!1;for(;i=t.exec(e);){let e=i[6]||i[5]||i[4]||i[3]||i[2]||i[1],[t,o]=i[0].startsWith(`(`)?[`("`,`")`]:i[0].startsWith(y)?[y,y]:[`"`,`"`],s=i.index,c=s+i[0].length,l=v(n,e);l&&(a=!0,r.update(s,c,`${t}${l}${o}`))}return a?r.toString():e},x=e=>{let t=m(`/[^/]`);return{name:`vuepress:replace-assets`,enforce:`pre`,transform:{filter:{id:{exclude:[/\.json(?:$|\?)/,/\.html?$/]}},handler(n){return b(n,t,e)}}}},S=()=>s(x),C=()=>c(x),w=r=>{let i={name:`@vuepress/plugin-replace-assets`},a=p(r);return a.length===0?i:{...i,extendsBundlerOptions(r,i){let o=n(i);o===`vite`&&e(r,i,{plugins:[S()(a)]}),o===`webpack`&&t(r,i,e=>{e.plugins??=[];let t=C();e.plugins.push(t(a))})}}};export{f as createFindPattern,S as createVitePluginReplaceAssets,C as createWebpackPluginReplaceAssets,g as isMatchUrl,p as normalizeRules,w as replaceAssetsPlugin,v as replacementAssetWithRules,b as transformAssets};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/node/constants.ts","../../src/node/normalizeRules.ts","../../src/node/utils.ts","../../src/node/transformAssets.ts","../../src/node/unplugin.ts","../../src/node/replaceAssetsPlugin.ts"],"sourcesContent":["export const PLUGIN_NAME = '@vuepress/plugin-replace-assets'\n\n/**\n * images extensions\n */\nexport const KNOWN_IMAGE_EXTENSIONS: string[] = [\n 'apng',\n 'bmp',\n 'png',\n 'jpe?g',\n 'jfif',\n 'pjpeg',\n 'pjp',\n 'gif',\n 'svg',\n 'ico',\n 'webp',\n 'avif',\n 'cur',\n 'jxl',\n]\n\n/**\n * media extensions\n */\nexport const KNOWN_MEDIA_EXTENSIONS: string[] = [\n 'mp4',\n 'webm',\n 'ogg',\n 'mp3',\n 'wav',\n 'flac',\n 'aac',\n 'opus',\n 'mov',\n 'm4a',\n 'vtt',\n\n 'pdf',\n]\n","import { isArray, isFunction } from '@vuepress/helper'\nimport { KNOWN_IMAGE_EXTENSIONS, KNOWN_MEDIA_EXTENSIONS } from './constants.js'\nimport type { ReplaceAssetsPluginOptions, ReplacementRule } from './types.js'\n\nexport const createFindPattern = (dir: string, extensions: string[]): RegExp =>\n new RegExp(`^/${dir}/.*\\\\.(?:${extensions.join('|')})(\\\\?.*)?$`)\n\n/**\n * Normalize replacement rules\n *\n * @param options - The plugin options to normalize\n * @returns Normalized replacement rules\n */\nexport const normalizeRules = (\n options?: ReplaceAssetsPluginOptions,\n): ReplacementRule[] => {\n const normalized: ReplacementRule[] = []\n\n if (!options) return []\n\n if (typeof options === 'string' || isFunction(options)) {\n // eslint-disable-next-line no-param-reassign\n options = {\n all: options,\n }\n }\n\n if (isArray(options)) {\n normalized.push(...options)\n return normalized\n }\n\n if ('find' in options) {\n if (options.find && options.replacement) normalized.push(options)\n return normalized\n }\n\n if (options.rules) {\n normalized.push(\n ...(isArray(options.rules) ? options.rules : [options.rules]),\n )\n }\n\n if (options.image || options.all) {\n normalized.push({\n find: createFindPattern('images', KNOWN_IMAGE_EXTENSIONS),\n replacement: options.image || options.all!,\n })\n }\n\n if (options.media || options.all) {\n normalized.push({\n find: createFindPattern('medias', KNOWN_MEDIA_EXTENSIONS),\n replacement: options.media || options.all!,\n })\n }\n\n return normalized\n}\n","import { removeEndingSlash, removeLeadingSlash } from '@vuepress/helper'\n\nexport const createAssetPattern = (prefix: string): RegExp => {\n // oxlint-disable-next-line id-length\n const s = `(${prefix}.*?)`\n return new RegExp(\n [\n `(?:\"${s}\")`, // \"prefix\"\n `(?:'${s}')`, // 'prefix'\n `(?:\\\\(${s}\\\\))`, // (prefix)\n `(?:\\\\('${s}'\\\\))`, // ('prefix')\n `(?:\\\\(\"${s}\"\\\\))`, // (\"prefix\")\n `(?:\\\\\\\\\"${s}\\\\\\\\\")`, // \\\"prefix\\\"\n ].join('|'),\n 'gu',\n )\n}\n\n/**\n * Normalize url\n *\n * @param url - The url to normalize\n * @param base - The base url to prepend to the url\n * @returns Normalized url\n *\n * @example\n * ```ts\n * normalizeUrl('/bar', '/foo/') // -> /foo/bar\n * ```\n */\nexport const normalizeUrl = (url: string, base?: string): string => {\n if (!url) return ''\n\n if (base) return `${removeEndingSlash(base)}/${removeLeadingSlash(url)}`\n\n return url\n}\n","import MagicString from 'magic-string'\nimport type { ReplacementRule } from './types.js'\nimport { normalizeUrl } from './utils.js'\n\n/**\n * Check if url matches find\n *\n * @param find - The find pattern, can be a string or a RegExp / 查找模式,可以是字符串或正则表达式\n * @param url - The URL to check / 要检查的 URL\n * @returns True if the URL matches the find pattern, false otherwise / 如果 URL 匹配查找模式则返回 true,否则返回 false\n */\nexport const isMatchUrl = (find: RegExp | string, url: string): boolean => {\n if (typeof find === 'string') {\n // like regexp string, start with `^` or end with `$`\n if (find.startsWith('^') || find.endsWith('$'))\n return new RegExp(find).test(url)\n\n return url.endsWith(find) || url.startsWith(find)\n }\n\n return find.test(url)\n}\n\nconst cache = new Map<string, string>()\n\n/**\n * Replace asset with rules\n *\n * @param rules - Replacement rules / 替换规则\n * @param url - The original asset URL / 原始资源 URL\n * @returns The replaced asset URL if matched, otherwise return undefined / 如果匹配则返回替换后的资源 URL,否则返回 undefined\n *\n * @example\n * ```ts\n * replacementAssetWithRules([{ find: '/foo/', replacement: 'https://example.com' }], '/foo/a.jpg')\n * // -> 'https://example.com/foo/a.jpg'\n * ```\n */\nexport const replacementAssetWithRules = (\n rules: ReplacementRule[],\n url: string,\n): string | void => {\n if (cache.has(url)) return cache.get(url)\n\n for (const { find, replacement } of rules) {\n if (find && isMatchUrl(find, url)) {\n const replaced =\n typeof replacement === 'function'\n ? normalizeUrl(replacement(url))\n : normalizeUrl(url, replacement)\n\n /* istanbul ignore if -- @preserve */\n if (replaced) {\n cache.set(url, replaced)\n return replaced\n }\n }\n }\n}\n\nconst ESCAPED_DOUBLE_QUOTE = String.raw`\\\"`\n\nexport const transformAssets = (\n code: string,\n pattern: RegExp,\n rules: ReplacementRule[],\n): string => {\n const str = new MagicString(code)\n let matched: RegExpExecArray | null\n let hasMatched = false\n\n while ((matched = pattern.exec(code))) {\n const assetUrl =\n matched[6] ||\n matched[5] ||\n matched[4] ||\n matched[3] ||\n matched[2] ||\n matched[1]\n const [left, right] = matched[0].startsWith('(')\n ? ['(\"', '\")']\n : matched[0].startsWith(ESCAPED_DOUBLE_QUOTE)\n ? [ESCAPED_DOUBLE_QUOTE, ESCAPED_DOUBLE_QUOTE]\n : ['\"', '\"']\n\n const start = matched.index\n const end = start + matched[0].length\n const resolved = replacementAssetWithRules(rules, assetUrl)\n if (resolved) {\n hasMatched = true\n str.update(start, end, `${left}${resolved}${right}`)\n }\n }\n\n if (!hasMatched) return code\n\n return str.toString()\n}\n","import type {\n UnpluginFactory,\n VitePlugin,\n WebpackPluginInstance,\n} from 'unplugin'\nimport { createVitePlugin, createWebpackPlugin } from 'unplugin'\nimport { transformAssets } from './transformAssets.js'\nimport type { ReplacementRule } from './types.js'\nimport { createAssetPattern } from './utils.js'\n\nconst replaceAssetsFactory: UnpluginFactory<ReplacementRule[]> = (rules) => {\n const pattern = createAssetPattern('/[^/]')\n return {\n name: 'vuepress:replace-assets',\n enforce: 'pre',\n transform: {\n filter: { id: { exclude: [/\\.json(?:$|\\?)/, /\\.html?$/] } },\n handler(code) {\n return transformAssets(code, pattern, rules)\n },\n },\n }\n}\n\nexport const createVitePluginReplaceAssets: () => (\n options: ReplacementRule[],\n) => VitePlugin | VitePlugin[] = () => createVitePlugin(replaceAssetsFactory)\n\nexport const createWebpackPluginReplaceAssets: () => (\n options: ReplacementRule[],\n) => WebpackPluginInstance = () => createWebpackPlugin(replaceAssetsFactory)\n","import { addViteConfig, configWebpack, getBundlerName } from '@vuepress/helper'\nimport type { Plugin } from 'vuepress/core'\nimport { PLUGIN_NAME } from './constants.js'\nimport { normalizeRules } from './normalizeRules.js'\nimport type { ReplaceAssetsPluginOptions } from './types.js'\nimport {\n createVitePluginReplaceAssets,\n createWebpackPluginReplaceAssets,\n} from './unplugin.js'\n\n/**\n * Plugin for replacing assets path\n *\n * 资源路径替换插件\n *\n * @example\n * ```\n * export default defineUserConfig({\n * plugins: [\n * replaceAssetsPlugin('https://cnd.example.com')\n * ]\n * })\n * ```\n */\nexport const replaceAssetsPlugin = (\n options?: ReplaceAssetsPluginOptions,\n): Plugin => {\n const EMPTY_PLUGIN = { name: PLUGIN_NAME }\n\n const rules = normalizeRules(options)\n\n if (rules.length === 0) return EMPTY_PLUGIN\n\n return {\n ...EMPTY_PLUGIN,\n\n extendsBundlerOptions(bundlerOptions, app) {\n const bundle = getBundlerName(app)\n\n if (bundle === 'vite') {\n const replaceAssets = createVitePluginReplaceAssets()\n addViteConfig(bundlerOptions, app, {\n plugins: [replaceAssets(rules)],\n })\n }\n\n if (bundle === 'webpack') {\n configWebpack(bundlerOptions, app, (config) => {\n config.plugins ??= []\n const replaceAssets = createWebpackPluginReplaceAssets()\n config.plugins.push(replaceAssets(rules))\n })\n }\n },\n }\n}\n"],"mappings":"qQAAA,MAKa,EAAmC,CAC9C,OACA,MACA,MACA,QACA,OACA,QACA,MACA,MACA,MACA,MACA,OACA,OACA,MACA,MACD,CAKY,EAAmC,CAC9C,MACA,OACA,MACA,MACA,MACA,OACA,MACA,OACA,MACA,MACA,MAEA,MACD,CCnCY,GAAqB,EAAa,IACzC,OAAO,KAAK,EAAI,WAAW,EAAW,KAAK,IAAI,CAAC,YAAY,CAQrD,EACX,GACsB,CACtB,IAAM,EAA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C,IAAM,EAAS,EAAe,EAAI,CAE9B,IAAW,QAEb,EAAc,EAAgB,EAAK,CACjC,QAAS,CAFW,GAA+B,CAE3B,EAAM,CAAC,CAChC,CAAC,CAGA,IAAW,WACb,EAAc,EAAgB,EAAM,GAAW,CAC7C,EAAO,UAAY,EAAE,CACrB,IAAM,EAAgB,GAAkC,CACxD,EAAO,QAAQ,KAAK,EAAc,EAAM,CAAC,EACzC,EAGP"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vuepress/plugin-replace-assets",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.125",
|
|
4
4
|
"description": "VuePress plugin - replace assets",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"plugin",
|
|
@@ -26,28 +26,30 @@
|
|
|
26
26
|
"directory": "plugins/tools/plugin-replace-assets"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
|
-
"
|
|
29
|
+
"dist"
|
|
30
30
|
],
|
|
31
31
|
"type": "module",
|
|
32
|
-
"
|
|
33
|
-
"types": "./lib/node/index.d.ts",
|
|
32
|
+
"sideEffects": false,
|
|
34
33
|
"exports": {
|
|
35
|
-
".":
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/node/index.d.ts",
|
|
36
|
+
"default": "./dist/node/index.js"
|
|
37
|
+
},
|
|
36
38
|
"./package.json": "./package.json"
|
|
37
39
|
},
|
|
38
40
|
"publishConfig": {
|
|
39
41
|
"access": "public"
|
|
40
42
|
},
|
|
41
43
|
"dependencies": {
|
|
42
|
-
"@vuepress/helper": "2.0.0-rc.
|
|
44
|
+
"@vuepress/helper": "2.0.0-rc.125",
|
|
43
45
|
"magic-string": "^0.30.21",
|
|
44
46
|
"unplugin": "^3.0.0"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
|
-
"webpack": "^5.105.
|
|
49
|
+
"webpack": "^5.105.4"
|
|
48
50
|
},
|
|
49
51
|
"peerDependencies": {
|
|
50
52
|
"vuepress": "2.0.0-rc.26"
|
|
51
53
|
},
|
|
52
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "58e660bb7138f1883e5440a08903d09cdbabcdc8"
|
|
53
55
|
}
|
package/lib/node/index.d.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import { Plugin } from 'vuepress/core';
|
|
2
|
-
import { VitePlugin, WebpackPluginInstance } from 'unplugin';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Assets Replacement Target Path
|
|
6
|
-
* - `string`: Directly concatenated before the original path
|
|
7
|
-
* - `(url) => string`: Custom replacement method, returns the new path
|
|
8
|
-
*
|
|
9
|
-
* 资源替换目标路径
|
|
10
|
-
* - `string`: 直接拼接在原始路径前面
|
|
11
|
-
* - `(url) => string`: 自定义替换方法,返回新路径
|
|
12
|
-
*/
|
|
13
|
-
type Replacement = string | ((url: string) => string);
|
|
14
|
-
/**
|
|
15
|
-
* Assets Replacement Rule
|
|
16
|
-
*
|
|
17
|
-
* 资源替换规则
|
|
18
|
-
*/
|
|
19
|
-
interface ReplacementRule {
|
|
20
|
-
/**
|
|
21
|
-
* Assets Matching
|
|
22
|
-
*
|
|
23
|
-
* - `RegExp`: Match using regular expression
|
|
24
|
-
* - `string`: Match using string
|
|
25
|
-
* - Strings starting with `^` or ending with `$` are automatically converted to regular expressions
|
|
26
|
-
* - For ordinary strings, checks if they appear at the start or end
|
|
27
|
-
*
|
|
28
|
-
* 资源匹配
|
|
29
|
-
*
|
|
30
|
-
* - `RegExp`: 匹配正则
|
|
31
|
-
* - `string`: 匹配字符串
|
|
32
|
-
* - 以 `^` 开头或以 `$` 结尾的字符串,会自动转换为正则
|
|
33
|
-
* - 普通字符串检查是否以其作为开头或结尾
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```
|
|
37
|
-
* '^/foo/'
|
|
38
|
-
* '.jpg$'
|
|
39
|
-
* /\.jpg$/
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
find: RegExp | string;
|
|
43
|
-
/**
|
|
44
|
-
* Assets Replacement Target Path
|
|
45
|
-
* - `string`: Directly concatenated before the original path
|
|
46
|
-
* - `(url) => string`: Custom replacement method, returns the new path
|
|
47
|
-
*
|
|
48
|
-
* 资源替换目标路径
|
|
49
|
-
* - `string`: 直接拼接在原始路径前面
|
|
50
|
-
* - `(url) => string`: 自定义替换方法,返回新路径
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```
|
|
54
|
-
* 'https://example.com'
|
|
55
|
-
* (url) => `https://example.com${url}`
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
replacement: Replacement;
|
|
59
|
-
}
|
|
60
|
-
interface ReplaceAssetsOptions {
|
|
61
|
-
/**
|
|
62
|
-
* Custom Assets Replacement Rules
|
|
63
|
-
*
|
|
64
|
-
* 自定义资源替换规则
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```ts
|
|
68
|
-
* {
|
|
69
|
-
* rules: [{
|
|
70
|
-
* find: /^\/images\/.*\.(jpe?g|png|gif|svg)$/,
|
|
71
|
-
* replacement: 'https://cdn.example.com/images/',
|
|
72
|
-
* }]
|
|
73
|
-
* }
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
rules?: ReplacementRule | ReplacementRule[];
|
|
77
|
-
/**
|
|
78
|
-
* Built-in image matching rules, designed to match and find common image paths starting with `^/images/`
|
|
79
|
-
*
|
|
80
|
-
* 内置的图片匹配规则,匹配查找 `^/images/` 开头的常见的图片路径
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```
|
|
84
|
-
* {
|
|
85
|
-
* image: 'https://example.com'
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
image?: Replacement;
|
|
90
|
-
/**
|
|
91
|
-
* Built-in media matching rules, designed to match and locate common media paths such as videos and audio that start with `^/medias/`.
|
|
92
|
-
*
|
|
93
|
-
* 内置的媒体匹配规则,匹配查找 `^/medias/` 开头的常见的视频、音频等媒体路径
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* ```
|
|
97
|
-
* {
|
|
98
|
-
* media: 'https://example.com'
|
|
99
|
-
* }
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
media?: Replacement;
|
|
103
|
-
/**
|
|
104
|
-
* Equivalent to setting both {@link ReplaceAssetsOptions.image image} and {@link ReplaceAssetsOptions.media media} simultaneously.
|
|
105
|
-
*
|
|
106
|
-
* 相当于同时设置 {@link ReplaceAssetsOptions.image image} 和 {@link ReplaceAssetsOptions.media media}
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* ```
|
|
110
|
-
* {
|
|
111
|
-
* all: 'https://example.com'
|
|
112
|
-
* }
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
all?: Replacement;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Assets Replacement Plugin Options
|
|
119
|
-
*
|
|
120
|
-
* 资源替换插件配置项
|
|
121
|
-
*/
|
|
122
|
-
type ReplaceAssetsPluginOptions = ReplaceAssetsOptions | Replacement | ReplacementRule | ReplacementRule[];
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Plugin for replacing assets path
|
|
126
|
-
*
|
|
127
|
-
* 资源路径替换插件
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```
|
|
131
|
-
* export default defineUserConfig({
|
|
132
|
-
* plugins: [
|
|
133
|
-
* replaceAssetsPlugin('https://cnd.example.com')
|
|
134
|
-
* ]
|
|
135
|
-
* })
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
declare const replaceAssetsPlugin: (options?: ReplaceAssetsPluginOptions) => Plugin;
|
|
139
|
-
|
|
140
|
-
declare const createFindPattern: (dir: string, extensions: string[]) => RegExp;
|
|
141
|
-
/**
|
|
142
|
-
* Normalize replacement rules
|
|
143
|
-
*
|
|
144
|
-
* @param options - The plugin options to normalize
|
|
145
|
-
* @returns Normalized replacement rules
|
|
146
|
-
*/
|
|
147
|
-
declare const normalizeRules: (options?: ReplaceAssetsPluginOptions) => ReplacementRule[];
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Check if url matches find
|
|
151
|
-
*
|
|
152
|
-
* @param find - The find pattern, can be a string or a RegExp / 查找模式,可以是字符串或正则表达式
|
|
153
|
-
* @param url - The URL to check / 要检查的 URL
|
|
154
|
-
* @returns True if the URL matches the find pattern, false otherwise / 如果 URL 匹配查找模式则返回 true,否则返回 false
|
|
155
|
-
*/
|
|
156
|
-
declare const isMatchUrl: (find: RegExp | string, url: string) => boolean;
|
|
157
|
-
/**
|
|
158
|
-
* Replace asset with rules
|
|
159
|
-
*
|
|
160
|
-
* @param rules - Replacement rules / 替换规则
|
|
161
|
-
* @param url - The original asset URL / 原始资源 URL
|
|
162
|
-
* @returns The replaced asset URL if matched, otherwise return undefined / 如果匹配则返回替换后的资源 URL,否则返回 undefined
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* replacementAssetWithRules([{ find: '/foo/', replacement: 'https://example.com' }], '/foo/a.jpg')
|
|
167
|
-
* // -> 'https://example.com/foo/a.jpg'
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
declare const replacementAssetWithRules: (rules: ReplacementRule[], url: string) => string | void;
|
|
171
|
-
declare const transformAssets: (code: string, pattern: RegExp, rules: ReplacementRule[]) => string;
|
|
172
|
-
|
|
173
|
-
declare const createVitePluginReplaceAssets: () => (options: ReplacementRule[]) => VitePlugin | VitePlugin[];
|
|
174
|
-
declare const createWebpackPluginReplaceAssets: () => (options: ReplacementRule[]) => WebpackPluginInstance;
|
|
175
|
-
|
|
176
|
-
export { createFindPattern, createVitePluginReplaceAssets, createWebpackPluginReplaceAssets, isMatchUrl, normalizeRules, replaceAssetsPlugin, replacementAssetWithRules, transformAssets };
|
|
177
|
-
export type { ReplaceAssetsOptions, ReplaceAssetsPluginOptions, Replacement, ReplacementRule };
|
package/lib/node/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{isFunction as x,isArray as m,removeEndingSlash as j,removeLeadingSlash as P,getBundlerName as R,addViteConfig as S,configWebpack as b}from"@vuepress/helper";import{createVitePlugin as I,createWebpackPlugin as M}from"unplugin";import O from"magic-string";const _="@vuepress/plugin-replace-assets",k=["apng","bmp","png","jpe?g","jfif","pjpeg","pjp","gif","svg","ico","webp","avif","cur","jxl"],y=["mp4","webm","ogg","mp3","wav","flac","aac","opus","mov","m4a","vtt","pdf"],l=(t,e)=>new RegExp(`^/${t}/.*\\.(?:${e.join("|")})(\\?.*)?$`),f=t=>{const e=[];return t?((typeof t=="string"||x(t))&&(t={all:t}),m(t)?(e.push(...t),e):"find"in t?(t.find&&t.replacement&&e.push(t),e):(t.rules&&e.push(...m(t.rules)?t.rules:[t.rules]),(t.image||t.all)&&e.push({find:l("images",k),replacement:t.image||t.all}),(t.media||t.all)&&e.push({find:l("medias",y),replacement:t.media||t.all}),e)):[]},U=t=>{const e=`(${t}.*?)`;return new RegExp([`(?:"${e}")`,`(?:'${e}')`,`(?:\\(${e}\\))`,`(?:\\('${e}'\\))`,`(?:\\("${e}"\\))`,`(?:\\\\"${e}\\\\")`].join("|"),"gu")},d=(t,e)=>t?e?`${j(e)}/${P(t)}`:t:"",h=(t,e)=>typeof t=="string"?t.startsWith("^")||t.endsWith("$")?new RegExp(t).test(e):e.endsWith(t)||e.startsWith(t):t.test(e),c=new Map,$=(t,e)=>{if(c.has(e))return c.get(e);for(const{find:s,replacement:r}of t)if(s&&h(s,e)){const n=typeof r=="function"?d(r(e)):d(e,r);/* istanbul ignore if -- @preserve */if(n)return c.set(e,n),n}},p=String.raw`\"`,W=(t,e,s)=>{const r=new O(t);let n,a=!1;for(;n=e.exec(t);){const i=n[6]||n[5]||n[4]||n[3]||n[2]||n[1],[o,w]=n[0].startsWith("(")?['("','")']:n[0].startsWith(p)?[p,p]:['"','"'],u=n.index,A=u+n[0].length,g=$(s,i);g&&(a=!0,r.update(u,A,`${o}${g}${w}`))}return a?r.toString():t},E=t=>{const e=U("/[^/]");return{name:"vuepress:replace-assets",enforce:"pre",transform:{filter:{id:{exclude:[/\.json(?:$|\?)/,/\.html?$/]}},handler(s){return W(s,e,t)}}}},N=()=>I(E),v=()=>M(E),V=t=>{const e={name:_},s=f(t);return s.length===0?e:{...e,extendsBundlerOptions(r,n){const a=R(n);if(a==="vite"){const i=N();S(r,n,{plugins:[i(s)]})}a==="webpack"&&b(r,n,i=>{i.plugins??=[];const o=v();i.plugins.push(o(s))})}}};export{l as createFindPattern,N as createVitePluginReplaceAssets,v as createWebpackPluginReplaceAssets,h as isMatchUrl,f as normalizeRules,V as replaceAssetsPlugin,$ as replacementAssetWithRules,W as transformAssets};
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
package/lib/node/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/node/constants.ts","../../src/node/normalizeRules.ts","../../src/node/utils.ts","../../src/node/transformAssets.ts","../../src/node/unplugin.ts","../../src/node/replaceAssetsPlugin.ts"],"sourcesContent":["export const PLUGIN_NAME = '@vuepress/plugin-replace-assets'\n\n/**\n * images extensions\n */\nexport const KNOWN_IMAGE_EXTENSIONS: string[] = [\n 'apng',\n 'bmp',\n 'png',\n 'jpe?g',\n 'jfif',\n 'pjpeg',\n 'pjp',\n 'gif',\n 'svg',\n 'ico',\n 'webp',\n 'avif',\n 'cur',\n 'jxl',\n]\n\n/**\n * media extensions\n */\nexport const KNOWN_MEDIA_EXTENSIONS: string[] = [\n 'mp4',\n 'webm',\n 'ogg',\n 'mp3',\n 'wav',\n 'flac',\n 'aac',\n 'opus',\n 'mov',\n 'm4a',\n 'vtt',\n\n 'pdf',\n]\n","import { isArray, isFunction } from '@vuepress/helper'\nimport { KNOWN_IMAGE_EXTENSIONS, KNOWN_MEDIA_EXTENSIONS } from './constants.js'\nimport type { ReplaceAssetsPluginOptions, ReplacementRule } from './types.js'\n\nexport const createFindPattern = (dir: string, extensions: string[]): RegExp =>\n new RegExp(`^/${dir}/.*\\\\.(?:${extensions.join('|')})(\\\\?.*)?$`)\n\n/**\n * Normalize replacement rules\n *\n * @param options - The plugin options to normalize\n * @returns Normalized replacement rules\n */\nexport const normalizeRules = (\n options?: ReplaceAssetsPluginOptions,\n): ReplacementRule[] => {\n const normalized: ReplacementRule[] = []\n\n if (!options) return []\n\n if (typeof options === 'string' || isFunction(options)) {\n // eslint-disable-next-line no-param-reassign\n options = {\n all: options,\n }\n }\n\n if (isArray(options)) {\n normalized.push(...options)\n return normalized\n }\n\n if ('find' in options) {\n if (options.find && options.replacement) normalized.push(options)\n return normalized\n }\n\n if (options.rules) {\n normalized.push(\n ...(isArray(options.rules) ? options.rules : [options.rules]),\n )\n }\n\n if (options.image || options.all) {\n normalized.push({\n find: createFindPattern('images', KNOWN_IMAGE_EXTENSIONS),\n replacement: options.image || options.all!,\n })\n }\n\n if (options.media || options.all) {\n normalized.push({\n find: createFindPattern('medias', KNOWN_MEDIA_EXTENSIONS),\n replacement: options.media || options.all!,\n })\n }\n\n return normalized\n}\n","import { removeEndingSlash, removeLeadingSlash } from '@vuepress/helper'\n\nexport const createAssetPattern = (prefix: string): RegExp => {\n // oxlint-disable-next-line id-length\n const s = `(${prefix}.*?)`\n return new RegExp(\n [\n `(?:\"${s}\")`, // \"prefix\"\n `(?:'${s}')`, // 'prefix'\n `(?:\\\\(${s}\\\\))`, // (prefix)\n `(?:\\\\('${s}'\\\\))`, // ('prefix')\n `(?:\\\\(\"${s}\"\\\\))`, // (\"prefix\")\n `(?:\\\\\\\\\"${s}\\\\\\\\\")`, // \\\"prefix\\\"\n ].join('|'),\n 'gu',\n )\n}\n\n/**\n * Normalize url\n *\n * @param url - The url to normalize\n * @param base - The base url to prepend to the url\n * @returns Normalized url\n *\n * @example\n * ```ts\n * normalizeUrl('/bar', '/foo/') // -> /foo/bar\n * ```\n */\nexport const normalizeUrl = (url: string, base?: string): string => {\n if (!url) return ''\n\n if (base) {\n return `${removeEndingSlash(base)}/${removeLeadingSlash(url)}`\n }\n\n return url\n}\n","import MagicString from 'magic-string'\nimport type { ReplacementRule } from './types.js'\nimport { normalizeUrl } from './utils.js'\n\n/**\n * Check if url matches find\n *\n * @param find - The find pattern, can be a string or a RegExp / 查找模式,可以是字符串或正则表达式\n * @param url - The URL to check / 要检查的 URL\n * @returns True if the URL matches the find pattern, false otherwise / 如果 URL 匹配查找模式则返回 true,否则返回 false\n */\nexport const isMatchUrl = (find: RegExp | string, url: string): boolean => {\n if (typeof find === 'string') {\n // like regexp string, start with `^` or end with `$`\n if (find.startsWith('^') || find.endsWith('$')) {\n return new RegExp(find).test(url)\n }\n\n return url.endsWith(find) || url.startsWith(find)\n }\n\n return find.test(url)\n}\n\nconst cache = new Map<string, string>()\n\n/**\n * Replace asset with rules\n *\n * @param rules - Replacement rules / 替换规则\n * @param url - The original asset URL / 原始资源 URL\n * @returns The replaced asset URL if matched, otherwise return undefined / 如果匹配则返回替换后的资源 URL,否则返回 undefined\n *\n * @example\n * ```ts\n * replacementAssetWithRules([{ find: '/foo/', replacement: 'https://example.com' }], '/foo/a.jpg')\n * // -> 'https://example.com/foo/a.jpg'\n * ```\n */\nexport const replacementAssetWithRules = (\n rules: ReplacementRule[],\n url: string,\n): string | void => {\n if (cache.has(url)) return cache.get(url)\n\n for (const { find, replacement } of rules) {\n if (find && isMatchUrl(find, url)) {\n const replaced =\n typeof replacement === 'function'\n ? normalizeUrl(replacement(url))\n : normalizeUrl(url, replacement)\n\n /* istanbul ignore if -- @preserve */\n if (replaced) {\n cache.set(url, replaced)\n return replaced\n }\n }\n }\n}\n\nconst ESCAPED_DOUBLE_QUOTE = String.raw`\\\"`\n\nexport const transformAssets = (\n code: string,\n pattern: RegExp,\n rules: ReplacementRule[],\n): string => {\n const str = new MagicString(code)\n let matched: RegExpExecArray | null\n let hasMatched = false\n\n while ((matched = pattern.exec(code))) {\n const assetUrl =\n matched[6] ||\n matched[5] ||\n matched[4] ||\n matched[3] ||\n matched[2] ||\n matched[1]\n const [left, right] = matched[0].startsWith('(')\n ? ['(\"', '\")']\n : matched[0].startsWith(ESCAPED_DOUBLE_QUOTE)\n ? [ESCAPED_DOUBLE_QUOTE, ESCAPED_DOUBLE_QUOTE]\n : ['\"', '\"']\n\n const start = matched.index\n const end = start + matched[0].length\n const resolved = replacementAssetWithRules(rules, assetUrl)\n if (resolved) {\n hasMatched = true\n str.update(start, end, `${left}${resolved}${right}`)\n }\n }\n\n if (!hasMatched) return code\n\n return str.toString()\n}\n","import type {\n UnpluginFactory,\n VitePlugin,\n WebpackPluginInstance,\n} from 'unplugin'\nimport { createVitePlugin, createWebpackPlugin } from 'unplugin'\nimport { transformAssets } from './transformAssets.js'\nimport type { ReplacementRule } from './types.js'\nimport { createAssetPattern } from './utils.js'\n\nconst replaceAssetsFactory: UnpluginFactory<ReplacementRule[]> = (rules) => {\n const pattern = createAssetPattern('/[^/]')\n return {\n name: 'vuepress:replace-assets',\n enforce: 'pre',\n transform: {\n filter: { id: { exclude: [/\\.json(?:$|\\?)/, /\\.html?$/] } },\n handler(code) {\n return transformAssets(code, pattern, rules)\n },\n },\n }\n}\n\nexport const createVitePluginReplaceAssets: () => (\n options: ReplacementRule[],\n) => VitePlugin | VitePlugin[] = () => createVitePlugin(replaceAssetsFactory)\n\nexport const createWebpackPluginReplaceAssets: () => (\n options: ReplacementRule[],\n) => WebpackPluginInstance = () => createWebpackPlugin(replaceAssetsFactory)\n","import { addViteConfig, configWebpack, getBundlerName } from '@vuepress/helper'\nimport type { Plugin } from 'vuepress/core'\nimport { PLUGIN_NAME } from './constants.js'\nimport { normalizeRules } from './normalizeRules.js'\nimport type { ReplaceAssetsPluginOptions } from './types.js'\nimport {\n createVitePluginReplaceAssets,\n createWebpackPluginReplaceAssets,\n} from './unplugin.js'\n\n/**\n * Plugin for replacing assets path\n *\n * 资源路径替换插件\n *\n * @example\n * ```\n * export default defineUserConfig({\n * plugins: [\n * replaceAssetsPlugin('https://cnd.example.com')\n * ]\n * })\n * ```\n */\nexport const replaceAssetsPlugin = (\n options?: ReplaceAssetsPluginOptions,\n): Plugin => {\n const EMPTY_PLUGIN = { name: PLUGIN_NAME }\n\n const rules = normalizeRules(options)\n\n if (rules.length === 0) return EMPTY_PLUGIN\n\n return {\n ...EMPTY_PLUGIN,\n\n extendsBundlerOptions(bundlerOptions, app) {\n const bundle = getBundlerName(app)\n\n if (bundle === 'vite') {\n const replaceAssets = createVitePluginReplaceAssets()\n addViteConfig(bundlerOptions, app, {\n plugins: [replaceAssets(rules)],\n })\n }\n\n if (bundle === 'webpack') {\n configWebpack(bundlerOptions, app, (config) => {\n config.plugins ??= []\n const replaceAssets = createWebpackPluginReplaceAssets()\n config.plugins.push(replaceAssets(rules))\n })\n }\n },\n }\n}\n"],"names":["PLUGIN_NAME","KNOWN_IMAGE_EXTENSIONS","KNOWN_MEDIA_EXTENSIONS","createFindPattern","dir","extensions","normalizeRules","options","normalized","isFunction","isArray","createAssetPattern","prefix","s","normalizeUrl","url","base","removeEndingSlash","removeLeadingSlash","isMatchUrl","find","cache","replacementAssetWithRules","rules","replacement","replaced","ESCAPED_DOUBLE_QUOTE","transformAssets","code","pattern","str","MagicString","matched","hasMatched","assetUrl","left","right","start","end","resolved","replaceAssetsFactory","createVitePluginReplaceAssets","createVitePlugin","createWebpackPluginReplaceAssets","createWebpackPlugin","replaceAssetsPlugin","EMPTY_PLUGIN","bundlerOptions","app","bundle","getBundlerName","replaceAssets","addViteConfig","configWebpack","config"],"mappings":"qQAAO,MAAMA,EAAc,kCAKdC,EAAmC,CAC9C,OACA,MACA,MACA,QACA,OACA,QACA,MACA,MACA,MACA,MACA,OACA,OACA,MACA,KACF,EAKaC,EAAmC,CAC9C,MACA,OACA,MACA,MACA,MACA,OACA,MACA,OACA,M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