-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
233 lines (196 loc) · 7.84 KB
/
index.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import type { Plugin } from 'vite'
import { tokenizer } from 'acorn'
import MagicString from 'magic-string'
type PluginOptions = {
name: string
devSandbox?: boolean
}
type Lifecyle = {
bootstrap(): Promise<any> | void
mount(props: any): Promise<any> | void
unmount(props: any): Promise<any> | void
update?(props: any): Promise<any> | void
}
type MicroApp = Partial<{
publicPath: string
__INJECTED_PUBLIC_PATH_BY_QIANKUN__: string
__POWERED_BY_QIANKUN__: string
lifecyle: Lifecyle
}>
const scriptModuleReg = /<script(\s+)type=('|")module\2(.*)>([^<]*)<\/script>/g
const scriptNoModuleReg = /<script(\s+)nomodule(.*)>([^<]*)<\/script>/g
const createAttrReg = (attr: string) => new RegExp(attr + "=('|\")([^>\\s]+)\\1")
const replaceScript = (script: string) => `<!-- replace by vite-plugin-legacy-qiankun ${script} -->`
const hasProtocol = (url: string) => url.startsWith('//') || url.startsWith('http://') || url.startsWith('https://')
export const convertVariable = (code: string, from: string, to: string) => {
const s = new MagicString(code)
const tokens = tokenizer(code, {
ecmaVersion: "latest",
sourceType: "module",
allowHashBang: true,
allowAwaitOutsideFunction: true,
allowImportExportEverywhere: true,
})
for (const token of tokens) {
if (token.value === from && !['.', '"', "'"].includes(code[token.start - 1])) {
s.overwrite(token.start, token.end, to)
}
}
return s.toString()
}
export const getMicroApp = (appName: string): MicroApp => {
const global = (0, eval)('window')
return (global.legacyQiankun && global.legacyQiankun[appName]) || {}
}
export const createLifecyle = (name: string, lifecyle: Lifecyle) => {
const global = (0, eval)('window')
global.legacyQiankun = global.legacyQiankun || {}
global.legacyQiankun[name] = global.legacyQiankun[name] || {}
global.legacyQiankun[name].lifecyle = lifecyle
}
export const createCtx = ({ name, devSandbox = false }: PluginOptions) => {
const createScriptStr = (scriptContent: string) => `<script>\n(function (){\nconst global = (0, eval)('window')\nconst name = '${name}'\n${scriptContent}})()\n</script>`
const preInjectStr = `
const proxy = global.proxy
let publicPath = (proxy && proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__) || ''
publicPath = publicPath.slice(0, publicPath.length - 1)
global.legacyQiankun = global.legacyQiankun || {}
global.legacyQiankun[name] = global.legacyQiankun[name] || {}
Object.assign(global.legacyQiankun[name], {
proxy,
publicPath,
'__INJECTED_PUBLIC_PATH_BY_QIANKUN__': proxy && proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
'__POWERED_BY_QIANKUN__': proxy && proxy.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
})`
const postInjectStr = `
const app = global.legacyQiankun[name]
if (!app.proxy) return
window[name] = {
bootstrap: (...args) => app.dynamicImport.then(() => app.lifecyle.bootstrap(...args)),
mount: (...args) => app.dynamicImport.then(() => app.lifecyle.mount(...args)),
unmount: (...args) => app.dynamicImport.then(() => app.lifecyle.unmount(...args)),
update: (...args) => app.dynamicImport.then(() => app.lifecyle.update(...args)),
}`
const legacyCode = `
const legacyQiankunWindow = window?.legacyQiankun?.['${name}']?.proxy || window
const legacyQiankunDocument = new Proxy({}, {
get(target, p, receiver) {
const fakeDocument = legacyQiankunWindow.document
return typeof fakeDocument[p] === 'function' ? fakeDocument[p].bind(fakeDocument) : fakeDocument[p]
},
set(target, p, newValue, receiver) {
return legacyQiankunWindow.document[p] = newValue
},
});\n`
const varMap = {
'document': 'legacyQiankunDocument',
'window': 'legacyQiankunWindow',
'globalThis': 'legacyQiankunWindow',
'self': 'legacyQiankunWindow',
}
const include = [/\.[jt]sx?$/, /\.(m)?js?$/, /\.vue$/, /\.vue\?vue/, /\.svelte$/]
// const exclude = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/]
const devTransform = (code: string, id?: string) => {
if(
id.includes('vite-plugin-legacy-qiankun/dist') ||
!include.some(reg => reg.test(id))
) return code
if (
!/(document|window|globalThis|self)/g.test(code)
) return code
if (id && id.includes('client')) {
code = code.replace('(!style)', '(style?.remove() || true)')
Object.keys(varMap).forEach(k => {
code = convertVariable(code, k, varMap[k])
})
}
if (devSandbox) {
Object.keys(varMap).forEach(k => {
code = convertVariable(code, k, varMap[k])
})
}
return `${legacyCode}${code}`
}
const devTransformIndexHtml = (html: string) => {
const srcReg = createAttrReg('src')
return html
.replace(scriptModuleReg, (script, ...args) => {
const srcMatch = script.match(srcReg)
const src = srcMatch?.[2] || ''
if (!src && args[3]) {
const importReg = /import(.+)from(\s*)('|")([^'"]+)\3/g
const nameList: string[] = []
const urlList: string[] = []
let res: RegExpExecArray | null
// eslint-disable-next-line no-cond-assign
while (res = importReg.exec(args[3] as string)) {
nameList.push(res[1].trim())
let url = res[4].trim()
if (url.startsWith('/')) url = url.slice(1)
urlList.push(`import((window?.proxy?.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || '/') + '${url}')`)
}
return `<script>
(function(){
window.addEventListener('load', () => {
Promise.all([${urlList.join(',')}]).then(([${nameList.join(',')}]) => {
${nameList.map(name => `${name} = ${name}.default || ${name}`).join('\n')}
${args[3].replace(importReg, '')}
})
})
})()
</script>`
}
if (!src.includes('@vite')) return `${createScriptStr(`global.legacyQiankun[name].dynamicImport = import(global.legacyQiankun[name].publicPath + '${src}')`)}`
return `${createScriptStr(`import(global.legacyQiankun[name].publicPath + '${src}')`)}`
})
.replace('<head>', match => `${match}\n${createScriptStr(preInjectStr)}`)
.replace('</body>', match => `${createScriptStr(postInjectStr)}\n${match}`)
}
const proTransformIndexHtml = (html: string) => {
const idReg = createAttrReg('id')
const srcReg = createAttrReg('data-src')
return html
.replace(scriptModuleReg, replaceScript)
.replace(scriptNoModuleReg, script => {
const idMatcher = script.match(idReg)
const id = idMatcher?.[2]
if (!id || id === 'vite-legacy-polyfill') return script.replace('nomodule', '')
if (id === 'vite-legacy-entry') {
const srcMatch = script.match(srcReg)
let src = `'${srcMatch?.[2] || ""}'`
if (!hasProtocol(src)) src = `!global.legacyQiankun[name].__POWERED_BY_QIANKUN__ ? ${src} : new URL(${src}, global.legacyQiankun[name].__POWERED_BY_QIANKUN__).toString()`
return `${createScriptStr(`global.legacyQiankun[name].dynamicImport = System.import(${src})`)}`
}
return replaceScript(script)
})
.replace('<head>', match => `${match}\n${createScriptStr(preInjectStr)}`)
.replace('</body>', match => `${createScriptStr(postInjectStr)}${match}`)
}
return {
devTransform,
devTransformIndexHtml,
proTransformIndexHtml
}
}
export const legacyQiankun = (options: PluginOptions): Plugin[] => {
const {
devTransform,
devTransformIndexHtml,
proTransformIndexHtml
} = createCtx(options)
return [
{
name: 'legacy-qiankun:dev',
enforce: 'post',
apply: 'serve',
transformIndexHtml: devTransformIndexHtml,
transform: devTransform,
},
{
name: 'legacy-qiankun:build',
enforce: 'post',
apply: 'build',
transformIndexHtml: proTransformIndexHtml
}
]
}