Skip to content

Commit

Permalink
feat: add global resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Jul 24, 2023
1 parent 55f91c8 commit e6de3f4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default defineConfig({
- [`url`](#url)
- [`transform`](#transform)
- [`logLevel`](#logLevel)
- [`resolve`](#resolve)

### include

Expand Down Expand Up @@ -134,7 +135,15 @@ Type: `slient` | `warn`

Default: `warn`

Adjust console output verbosity
Adjust console output verbosity.

### resolve

Type: `ResolverFunction`

Default: `undefined`

A global url parser.

### Acknowledgements

Expand Down
13 changes: 8 additions & 5 deletions example/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { VarletUIResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { cdn } from 'vite-plugin-cdn2'
import { jsdelivr } from 'vite-plugin-cdn2/url'
import { compression } from 'vite-plugin-compression2'
import Inspect from 'vite-plugin-inspect'


export default defineConfig(({ command }) => {
return {
plugins: [
vue(),
Components({ resolvers: [VarletUIResolver()] }),
{
...cdn({
modules: [{
name: 'vue',
resolve: 'https://cdn.bootcdn.net/ajax/libs/[name]/[version]/vue.global.js'
}, 'vue-demi', 'pinia', '@varlet/ui', 'axios']
url: 'https://cdn.bootcdn.net/ajax/libs/',
modules: ['vue', 'vue-demi', 'pinia', '@varlet/ui', 'axios'],
resolve(baseURL, { name, version, relativeModule }) {
if (name === '@varlet/ui') return new URL(`${name}@${version}/${relativeModule}`, jsdelivr).href
return new URL(`${name}/${version}/${path.basename(relativeModule)}`, baseURL).href
}
}),
apply: command
},
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { jsdelivr } from './url'
import type { CDNPluginOptions } from './interface'

function cdn(opts: CDNPluginOptions = {}): Plugin {
const { modules = [], url = jsdelivr, include = /\.(mjs|js|ts|vue|jsx|tsx)(\?.*|)$/, exclude, logLevel = 'warn' } = opts
const { modules = [], url = jsdelivr, include = /\.(mjs|js|ts|vue|jsx|tsx)(\?.*|)$/, exclude, logLevel = 'warn', resolve: resolver } = opts
const filter = createFilter(include, exclude)
const scanner = createScanner(modules)
const generator = createCodeGenerator()
Expand All @@ -35,7 +35,7 @@ function cdn(opts: CDNPluginOptions = {}): Plugin {
return generator.transform(code)
},
transformIndexHtml(html) {
const inject = createInjectScript(scanner.dependencies, url)
const inject = createInjectScript(scanner.dependencies, url, resolver)
return inject.text(html, opts.transform)
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ function replaceURL(p: string, url: string | ResolverFunction, options: Options)
return template.replace(/\[version\]/, options.extra.version).replace(/\[baseURL\]/, options.baseURL).replace(/\[name\]/, options.extra.name)
}

function makeURL(moduleMeta: ModuleInfo, baseURL: string) {
function makeURL(moduleMeta: ModuleInfo, baseURL: string, resolver?: ResolverFunction) {
const { version, name: packageName, relativeModule, resolve } = moduleMeta
if (!baseURL) return
const u = new URL(`${packageName}@${version}/${relativeModule}`, baseURL).href
const u = resolver ? resolver(baseURL, moduleMeta) : new URL(`${packageName}@${version}/${relativeModule}`, baseURL).href
if (resolve) return replaceURL(u, resolve, { extra: moduleMeta, baseURL })
return u
}

function makeNode(moduleInfo: ModuleInfo): ScriptNode | LinkNode {
return {
tag: 'link',
tag: 'link',
url: new Set(),
name: moduleInfo.name,
extra: moduleInfo
Expand All @@ -39,8 +39,8 @@ function makeNode(moduleInfo: ModuleInfo): ScriptNode | LinkNode {
class InjectScript {
private modules: Map<string, LinkNode | ScriptNode>
private window: Window
constructor(modules: Map<string, ModuleInfo>, url: string) {
this.modules = this.prepareSource(modules, url)
constructor(modules: Map<string, ModuleInfo>, url: string, resolver?: ResolverFunction) {
this.modules = this.prepareSource(modules, url, resolver)
this.window = new Window()
}

Expand Down Expand Up @@ -85,7 +85,7 @@ class InjectScript {
return document.body.innerHTML
}

private prepareSource(modules: Map<string, ModuleInfo>, baseURL: string) {
private prepareSource(modules: Map<string, ModuleInfo>, baseURL: string, resolver?: ResolverFunction) {
const container: Map<string, LinkNode | ScriptNode> = new Map()

const traverseModule = (moduleMeta: ModuleInfo, moduleName: string) => {
Expand All @@ -112,7 +112,7 @@ class InjectScript {

modules.forEach((meta, moduleName) => {
const node = makeNode(meta)
const url = makeURL(meta, baseURL)
const url = makeURL(meta, baseURL, resolver)
if (!url) return
node.url.add(url)
node.tag = isScript(url)
Expand All @@ -126,7 +126,8 @@ class InjectScript {

export function createInjectScript(
dependModules: Map<string, ModuleInfo>,
url: string
url: string,
resolver?: ResolverFunction
) {
return new InjectScript(dependModules, url)
return new InjectScript(dependModules, url, resolver)
}
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ export interface CDNPluginOptions {
include?: FilterPattern
exclude?: FilterPattern
logLevel?: 'slient' | 'warn'
resolve?: ResolverFunction
}

0 comments on commit e6de3f4

Please sign in to comment.