Skip to content

Commit 43ab36d

Browse files
committed
feat: add npmmirror.com cdn
1 parent c8156ca commit 43ab36d

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,17 @@ export default defineConfig({
238238
| Parameter | Type | Default | Description |
239239
| --- | --- | --- | --- |
240240
| **modules** | ([NpmModule](#NpmModule) \| [PresetNpmModule](#PresetNpmModule) \| [HtmlInjectCode](#HtmlInjectCode))[] | [] | Modules to import |
241-
| type | `'unpkg' \| 'jsdelivr' \| 'custom'` | 'unpkg' | CDN source type |
241+
| type | `'npmmirror' \| 'unpkg' \| 'jsdelivr' \| 'custom'` | 'jsdelivr' | `CDN` source type, parameters `name`/`version`/`file` are taken from the modules configuration. When the OS language is `zh_CN` , the default value is `npmmirror`, otherwise it is `jsdelivr`. |
242242
| url | `string` | '' | Custom URL (used with `type` ) |
243243
| local | 'boolean' \| 'string[]' \| [HtmlCdnLocal](#HtmlCdnLocal) | false | Local mode or specific modules |
244244

245+
CDN type:
246+
247+
- npmmirror: url defaults to https://registry.npmmirror.com/{name}/{version}/files/{file}
248+
- jsdelivr: url defaults to https://cdn.jsdelivr.net/npm/{name}@{version}/{file}
249+
- unpkg: url defaults to https://unpkg.com/{name}@{version}/{file}
250+
- custom: custom url can be defined
251+
245252
##### NpmModule
246253

247254
Configuration for CDN modules.

README.zh_CN.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,17 @@ export default defineConfig({
238238
| 参数名 | 类型 | 默认值 | 说明 |
239239
| --- | --- | --- | --- |
240240
| **modules** | ([NpmModule](#NpmModule) \| [PresetNpmModule](#PresetNpmModule) \| [HtmlInjectCode](#HtmlInjectCode))[] | [] | 引入的模块 |
241-
| type | `'unpkg' \| 'jsdelivr' \| 'custom'` | 'unpkg' | cdn 源类型。jsdelivr: url默认值为 https://cdn.jsdelivr.net/npm/{name}@{version}/{file}; unpkg: url默认值为 https://unpkg.com/{name}@{version}/{file}; custom: 可自定义url |
241+
| type | `'npmmirror' \| 'unpkg' \| 'jsdelivr' \| 'custom'` | `CDN` 源类型,参数 `name`/`version`/`file` 取自模块配置。 当操作系统语言为 `zh_CN` 时,默认值为 `npmmirror` ,否则为 `jsdelivr` |
242242
| url | `string` | '' | 结合 type 参数使用, 设置不同url,最终路径为 {url}/{file} |
243243
| local | 'boolean' \| 'string[]' \| [HtmlCdnLocal](#HtmlCdnLocal) | false | 本地模式或指定模块为本地模块,默认为 false |
244244

245+
CDN type:
246+
247+
- npmmirror: url 默认为 https://registry.npmmirror.com/{name}/{version}/files/{file}
248+
- jsdelivr: url 默认为 https://cdn.jsdelivr.net/npm/{name}@{version}/{file}
249+
- unpkg: url 默认为 https://unpkg.com/{name}@{version}/{file}
250+
- custom: 可以自定义 url
251+
245252
##### NpmModule
246253

247254
cdn 模块配置

src/cdn/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,22 @@ export interface HtmlCdnOptions {
149149
*/
150150
selector?: string;
151151
/**
152-
* cdn source type, parameters name/version/file are taken from the modules configuration. Default is "unpkg".
152+
* `CDN` source type, parameters `name`/`version`/`file` are taken from the modules configuration.
153+
*
154+
* When the OS language is `zh_CN` , the default value is `npmmirror`, otherwise it is `jsdelivr`.
155+
*
156+
* * npmmirror: url defaults to https://registry.npmmirror.com/{name}/{version}/files/{file}
153157
* * jsdelivr: url defaults to https://cdn.jsdelivr.net/npm/{name}@{version}/{file}
154158
* * unpkg: url defaults to https://unpkg.com/{name}@{version}/{file}
155159
* * custom: custom url can be defined
156160
*
157-
* @default "unpkg"
161+
* @default "npmmirror"
158162
*/
159-
type?: 'unpkg' | 'jsdelivr' | 'custom';
163+
type?: 'npmmirror' | 'unpkg' | 'jsdelivr' | 'custom';
160164
/**
161165
* Used in conjunction with the type parameter, sets different urls, the final path will be {url}/{file}
166+
*
167+
* * npmmirror: url defaults to https://registry.npmmirror.com/{name}/{version}/files
162168
* * jsdelivr: url defaults to https://cdn.jsdelivr.net/npm/{name}@{version}
163169
* * unpkg: url defaults to https://unpkg.com/{name}@{version}
164170
* * custom: custom url can be defined

src/cdn/util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { PRESET_MODULES } from './data';
77
import { DepModuleFiles, HtmlCdnLocal, HtmlCdnOptions, HtmlInjectCode, NpmModule } from './types';
88

99
const urlTypeMap: Record<string, string> = {
10+
npmmirror: 'https://registry.npmmirror.com/{name}/{version}/files',
1011
jsdelivr: 'https://cdn.jsdelivr.net/npm/{name}@{version}',
1112
unpkg: 'https://unpkg.com/{name}@{version}',
1213
};
@@ -102,11 +103,11 @@ function preHandleOptions(options?: HtmlCdnOptions): PreHandleOptions {
102103

103104
// URL type
104105
let { type, url } = opts;
105-
if (!type || !['jsdelivr', 'unpkg', 'custom', 'local'].includes(type)) {
106-
type = 'unpkg';
106+
if (!type || !['npmmirror', 'jsdelivr', 'unpkg', 'custom'].includes(type)) {
107+
type = new Intl.NumberFormat().resolvedOptions().locale === 'zh-CN' ? 'npmmirror' : 'jsdelivr';
107108
}
108109

109-
if (['jsdelivr', 'unpkg'].includes(type)) {
110+
if (['npmmirror', 'jsdelivr', 'unpkg'].includes(type)) {
110111
url = urlTypeMap[type];
111112
} else if (!url) {
112113
throw new Error('When the type parameter is custom, the url parameter is required.');

0 commit comments

Comments
 (0)