Skip to content

Commit

Permalink
feat: visitor node bind extra info
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Jul 19, 2023
1 parent 7ca96c3 commit 95a05ef
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"unplugin-vue-components": "^0.24.1",
"vite": "^4",
"vite-plugin-cdn2": "file:../",
"vite-plugin-compression2":"^0.10.2",
"vite-plugin-inspect": "^0.7.26"
},
"dependencies": {
Expand Down
5 changes: 5 additions & 0 deletions example/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 { compression } from 'vite-plugin-compression2'
import Inspect from 'vite-plugin-inspect'


Expand All @@ -17,6 +18,10 @@ export default defineConfig(({ command }) => {
}),
apply: command
},
compression({
algorithm: 'gzip',
threshold: 3 * 1024
}),
Inspect()
]
}
Expand Down
9 changes: 8 additions & 1 deletion example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1463,14 +1463,21 @@ util-deprecate@~1.0.1:
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

"vite-plugin-cdn2@file:..":
version "0.5.1"
version "0.6.0"
dependencies:
"@babel/core" "^7.22.5"
"@rollup/pluginutils" "^5.0.2"
"@types/estree" "^1.0.1"
happy-dom "^6.0.4"
rs-module-lexer "^1.0.0"

vite-plugin-compression2@^0.10.2:
version "0.10.2"
resolved "https://registry.yarnpkg.com/vite-plugin-compression2/-/vite-plugin-compression2-0.10.2.tgz#2a60564e50118c57eb7531d1c3088d35daac879d"
integrity sha512-PARrZawHL0uc6Dt788eUHZcd+MS6JB24YFN5ubfICyhCdrJS84W5GDtytzgC5z2VmCfDRorfdk5SXf+AqAX3Uw==
dependencies:
"@rollup/pluginutils" "^5.0.2"

vite-plugin-inspect@^0.7.26:
version "0.7.26"
resolved "https://registry.yarnpkg.com/vite-plugin-inspect/-/vite-plugin-inspect-0.7.26.tgz#5ec08a16a7b2198e708cfd56b39ce3cba8d0933a"
Expand Down
15 changes: 8 additions & 7 deletions src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ function makeURL(moduleMeta: IIFEModuleInfo, baseURL:string) {
return new URL(`${packageName}@${version}/${relativeModule}`, baseURL).href
}

function makeNode(packageName:string):ScriptNode | LinkNode {
function makeNode(moduleInfo:IIFEModuleInfo):ScriptNode | LinkNode {
return {
tag: 'link',
url: new Set(),
name: packageName
name: moduleInfo.name,
extra: moduleInfo
}
}

Expand All @@ -35,7 +36,7 @@ class InjectScript {
const tags = []
this.modules.forEach((node) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { tag, url, name: _, ...restProps } = node
const { tag, url, name: _, extra: __, ...restProps } = node
if (url.size) {
url.forEach((l) => {
const element = this.window.document.createElement(tag)
Expand Down Expand Up @@ -76,7 +77,7 @@ class InjectScript {
const container:Map<string, LinkNode | ScriptNode> = new Map()

const traverseModule = (moduleMeta: IIFEModuleInfo, moduleName: string) => {
const { spare, name: packageName } = moduleMeta
const { spare } = moduleMeta
if (!spare) return
if (Array.isArray(spare)) {
for (const s of uniq(spare)) {
Expand All @@ -91,20 +92,20 @@ class InjectScript {
node.url.add(spare)
return
}
const node = makeNode(packageName)
const node = makeNode(moduleMeta)
node.url.add(spare)
node.tag = isScript(spare)
container.set(mark, node)
}

modules.forEach((meta, moduleName) => {
const node = makeNode(meta.name)
const node = makeNode(meta)
const url = makeURL(meta, baseURL)
node.url.add(url)
node.tag = isScript(url)
const mark = `__${moduleName}__${node.tag}__`
container.set(mark, node)
if (meta.spare) traverseModule(meta, moduleName)
if (meta.spare) traverseModule(meta, moduleName)
})
return container
}
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Serialization {
type?: string
name: string
tag: 'link' | 'script'
extra:Record<string, any>
}


Expand Down
5 changes: 0 additions & 5 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export function lookup(entry: string, target: string): string {
return lookup(dir, target)
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function hasOwn<T extends { [key: string]: unknown }>(obj: T, key: keyof T | (string & {})): boolean {
return Object.hasOwnProperty.call(obj, key)
}

export function len<T extends ArrayLike<unknown>>(source: T) {
return source.length
}
Expand Down

0 comments on commit 95a05ef

Please sign in to comment.