Skip to content

Commit 82d5a98

Browse files
authored
feat: all cdn resources can be replaced (#325)
1 parent ef3d17a commit 82d5a98

File tree

9 files changed

+521
-13
lines changed

9 files changed

+521
-13
lines changed

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,73 @@ const store = useStore(
132132
<Sandbox :store="store" />
133133
</template>
134134
```
135+
136+
<details>
137+
<summary>Configuration options for resource links. (replace CDN resources)</summary>
138+
139+
```ts
140+
export type ResourceLinkConfigs = {
141+
/** URL for ES Module Shims. */
142+
esModuleShims?: string
143+
/** Function that generates the Vue compiler URL based on the version. */
144+
vueCompilerUrl?: (version: string) => string
145+
/** Function that generates the TypeScript library URL based on the version. */
146+
typescriptLib?: (version: string) => string
147+
148+
/** [monaco] Function that generates a URL to fetch the latest version of a package. */
149+
pkgLatestVersionUrl?: (pkgName: string) => string
150+
/** [monaco] Function that generates a URL to browse a package directory. */
151+
pkgDirUrl?: (pkgName: string, pkgVersion: string, pkgPath: string) => string
152+
/** [monaco] Function that generates a URL to fetch the content of a file from a package. */
153+
pkgFileTextUrl?: (
154+
pkgName: string,
155+
pkgVersion: string | undefined,
156+
pkgPath: string,
157+
) => string
158+
}
159+
```
160+
161+
**unpkg**
162+
163+
```ts
164+
const store = useStore({
165+
resourceLinks: ref({
166+
esModuleShims:
167+
'https://unpkg.com/es-module-shims@1.5.18/dist/es-module-shims.wasm.js',
168+
vueCompilerUrl: (version) =>
169+
`https://unpkg.com/@vue/compiler-sfc@${version}/dist/compiler-sfc.esm-browser.js`,
170+
typescriptLib: (version) =>
171+
`https://unpkg.com/typescript@${version}/lib/typescript.js`,
172+
pkgLatestVersionUrl: (pkgName) =>
173+
`https://unpkg.com/${pkgName}@latest/package.json`,
174+
pkgDirUrl: (pkgName, pkgVersion, pkgPath) =>
175+
`https://unpkg.com/${pkgName}@${pkgVersion}/${pkgPath}/?meta`,
176+
pkgFileTextUrl: (pkgName, pkgVersion, pkgPath) =>
177+
`https://unpkg.com/${pkgName}@${pkgVersion || 'latest'}/${pkgPath}`,
178+
}),
179+
})
180+
```
181+
182+
**npmmirror**
183+
184+
```ts
185+
const store = useStore({
186+
resourceLinks: ref({
187+
esModuleShims:
188+
'https://registry.npmmirror.com/es-module-shims/1.5.18/files/dist/es-module-shims.wasm.js',
189+
vueCompilerUrl: (version) =>
190+
`https://registry.npmmirror.com/@vue/compiler-sfc/${version}/files/dist/compiler-sfc.esm-browser.js`,
191+
typescriptLib: (version) =>
192+
`https://registry.npmmirror.com/typescript/${version}/files/lib/typescript.js`,
193+
194+
pkgLatestVersionUrl: (pkgName) =>
195+
`https://registry.npmmirror.com/${pkgName}/latest/files/package.json`,
196+
pkgDirUrl: (pkgName, pkgVersion, pkgPath) =>
197+
`https://registry.npmmirror.com/${pkgName}/${pkgVersion}/files/${pkgPath}/?meta`,
198+
pkgFileTextUrl: (pkgName, pkgVersion, pkgPath) =>
199+
`https://registry.npmmirror.com/${pkgName}/${pkgVersion || 'latest'}/files/${pkgPath}`,
200+
}),
201+
})
202+
```
203+
204+
</details>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"@types/node": "^24.2.0",
9090
"@vitejs/plugin-vue": "^6.0.1",
9191
"@volar/jsdelivr": "2.4.23",
92+
"@volar/language-service": "~2.4.11",
9293
"@volar/monaco": "2.4.23",
9394
"@volar/typescript": "2.4.23",
9495
"@vue/babel-plugin-jsx": "^2.0.1",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/monaco/env.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ export interface WorkerMessage {
122122
event: 'init'
123123
tsVersion: string
124124
tsLocale?: string
125+
pkgDirUrl?: string
126+
pkgFileTextUrl?: string
127+
pkgLatestVersionUrl?: string
128+
typescriptLib?: string
125129
}
126130

127131
export function loadMonacoEnv(store: Store) {
@@ -135,11 +139,27 @@ export function loadMonacoEnv(store: Store) {
135139
resolve()
136140
}
137141
})
138-
worker.postMessage({
142+
143+
const {
144+
pkgDirUrl,
145+
pkgFileTextUrl,
146+
pkgLatestVersionUrl,
147+
typescriptLib,
148+
} = store.resourceLinks || {}
149+
150+
const message: WorkerMessage = {
139151
event: 'init',
140152
tsVersion: store.typescriptVersion,
141153
tsLocale: store.locale,
142-
} satisfies WorkerMessage)
154+
pkgDirUrl: pkgDirUrl ? String(pkgDirUrl) : undefined,
155+
pkgFileTextUrl: pkgFileTextUrl ? String(pkgFileTextUrl) : undefined,
156+
pkgLatestVersionUrl: pkgLatestVersionUrl
157+
? String(pkgLatestVersionUrl)
158+
: undefined,
159+
typescriptLib: typescriptLib ? String(typescriptLib) : undefined,
160+
}
161+
162+
worker.postMessage(message)
143163
})
144164
await init
145165
return worker

0 commit comments

Comments
 (0)