29 lines
920 B
JavaScript
29 lines
920 B
JavaScript
// Vite plugin: a virtual client entry that mounts the user's app. The
|
|||
|
|
// user writes one isomorphic module; bankai generates the two lines of
|
||
|
|
// browser glue instead of asking for a second file.
|
||
|
|
|
||
|
|
export const CLIENT_ID = 'bankai:client'
|
||
|
|
const RESOLVED_CLIENT_ID = '\0' + CLIENT_ID
|
||
|
|
|
||
|
|
// the URL a browser uses to import the virtual module through Vite's dev
|
||
|
|
// middleware ('\0' encodes as '__x00__')
|
||
|
|
export const CLIENT_URL = '/@id/__x00__' + CLIENT_ID
|
||
|
|
|
||
|
|
export default function bankaiPlugin (entry) {
|
||
|
|
return {
|
||
|
|
name: 'bankai',
|
||
|
|
resolveId (id) {
|
||
|
|
if (id === CLIENT_ID) return RESOLVED_CLIENT_ID
|
||
|
|
},
|
||
|
|
load (id) {
|
||
|
|
if (id === RESOLVED_CLIENT_ID) {
|
||
|
|
return [
|
||
|
|
`import create from ${JSON.stringify(entry)}`,
|
||
|
|
'const app = typeof create === "function" && !create.mount ? create() : create',
|
||
|
|
'app.mount(app.selector || "body")'
|
||
|
|
].join('\n')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|