-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.ts
More file actions
173 lines (146 loc) · 4.06 KB
/
index.ts
File metadata and controls
173 lines (146 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import externalGlobals from 'rollup-plugin-external-globals'
import fs from 'fs'
import path from 'path'
import { Plugin, UserConfig } from 'vite'
import { Module, Options } from './type'
import autoComplete from './autoComplete'
import pkgUp from 'pkg-up'
/**
* get npm module version
* @param name
* @returns
*/
function getModuleVersion(name: string): string {
const packageJsonPath = pkgUp.sync({
cwd: require.resolve(name),
})
if (fs.existsSync(packageJsonPath)) {
const pkgJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
return pkgJson.version
}
return ''
}
/**
* 是否完整的 url
* @param path
* @returns
*/
function isFullPath(path: string) {
return path.startsWith('http:')
|| path.startsWith('https:')
|| path.startsWith('//') ? true : false
}
function renderUrl(url: string, data: {
name: string
version: string
path: string
}) {
const { path } = data
if (isFullPath(path)
) {
url = path
}
return url.replace(/\{name\}/g, data.name)
.replace(/\{version\}/g, data.version)
.replace(/\{path\}/g, path)
}
function PluginImportToCDN(options: Options): Plugin[] {
const {
modules = [],
prodUrl = 'https://cdn.jsdelivr.net/npm/{name}@{version}/{path}',
} = options
let isBuild = false
const data = modules.map((m) => {
let v: Module
if (typeof m === 'function') {
v = m(prodUrl)
} else {
v = m
}
const version = getModuleVersion(v.name)
let pathList: string[] = []
if (!Array.isArray(v.path)) {
pathList.push(v.path)
} else {
pathList = v.path
}
const data = {
...v,
version
}
pathList = pathList.map(p => {
if (!version && !isFullPath(p)) {
throw new Error(`modules: ${data.name} package.json file does not exist`)
}
return renderUrl(prodUrl, {
...data,
path: p
})
})
let css = v.css || []
if (!Array.isArray(css) && css) {
css = [css]
}
const cssList = !Array.isArray(css) ? [] : css.map(c => renderUrl(prodUrl, {
...data,
path: c
}))
return {
...v,
version,
pathList,
cssList
}
})
const externalMap: {
[name: string]: string
} = {}
data.forEach((v) => {
externalMap[v.name] = v.var
})
const externalLibs = Object.keys(externalMap)
const plugins: Plugin[] = [
{
name: 'vite-plugin-cdn-import',
config(_, { command }) {
const userConfig: UserConfig = {
build: {
rollupOptions: {}
}
}
if (command === 'build') {
isBuild = true
userConfig!.build!.rollupOptions = {
external: [...externalLibs],
plugins: [externalGlobals(externalMap)]
}
} else {
isBuild = false
}
return userConfig
},
transformIndexHtml(html) {
const cssCode = data
.map(v => v.cssList.map(css => `<link href="${css}" rel="stylesheet">`).join('\n'))
.filter(v => v)
.join('\n')
const jsCode = !isBuild
? ''
: data
.map(p => p.pathList.map(url => `<script src="${url}"></script>`).join('\n'))
.join('\n')
return html.replace(
/<\/title>/i,
`</title>${cssCode}\n${jsCode}`
)
},
},
]
return plugins
}
export {
PluginImportToCDN as Plugin,
Options,
autoComplete,
}
export default PluginImportToCDN