-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remoteEntry support hash; format dynamic es module (#35)
- Loading branch information
1 parent
dec3b9b
commit 7c3ba59
Showing
8 changed files
with
100 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
import { applyVueInReact } from 'veaury'; | ||
import App from 'viteRemote/App'; | ||
const AppComponent = App.default; | ||
import ViteApp from 'viteRemote/App'; | ||
|
||
export default function Button() { | ||
return ( | ||
<div> | ||
rust host | ||
<hr /> | ||
<AppComponent /> | ||
<ViteApp /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,94 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { Plugin } from 'vite'; | ||
|
||
interface AddEntryOptions { | ||
entryName: string; | ||
entryPath: string; | ||
fileName: string; | ||
fileName?: string; | ||
} | ||
|
||
const addEntry = ({ entryName, entryPath, fileName }: AddEntryOptions): Plugin => { | ||
let command: string; | ||
const addEntry = ({ entryName, entryPath, fileName }: AddEntryOptions): Plugin[] => { | ||
let entryFiles: string[] = []; | ||
let htmlFilePath: string; | ||
|
||
return { | ||
name: 'add-entry', | ||
configureServer(server) { | ||
server.middlewares.use((req, res, next) => { | ||
if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) { | ||
req.url = entryPath; | ||
} | ||
next(); | ||
}); | ||
}, | ||
config(config, { command: _command }) { | ||
command = _command; | ||
}, | ||
buildStart() { | ||
if (command !== 'build') return; | ||
// if we don't expose any modules, there is no need to emit file | ||
this.emitFile({ | ||
fileName: `${fileName}`, | ||
type: 'chunk', | ||
id: entryPath, | ||
preserveSignature: 'strict', | ||
}); | ||
}, | ||
transformIndexHtml(c) { | ||
if (command !== 'build') | ||
return [ | ||
{ | ||
name: 'add-entry', | ||
apply: "serve", | ||
configureServer(server) { | ||
server.httpServer?.once?.('listening', () => { | ||
const { port } = server.config.server; | ||
fetch(`http://localhost:${port}${entryPath}`) | ||
}); | ||
server.middlewares.use((req, res, next) => { | ||
if (!fileName) { | ||
next() | ||
return | ||
} | ||
if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) { | ||
req.url = entryPath; | ||
} | ||
next(); | ||
}); | ||
}, | ||
transformIndexHtml(c) { | ||
return c.replace( | ||
'<head>', | ||
`<head><script type="module" src=${JSON.stringify( | ||
entryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\/g, '/') | ||
entryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\/g, '/') | ||
)}></script>` | ||
); | ||
return c.replace('<head>', `<head><script type="module" src=${fileName}></script>`); | ||
}, | ||
}, | ||
}; | ||
{ | ||
name: "add-entry", | ||
enforce: "post", | ||
apply: "build", | ||
configResolved(config) { | ||
const inputOptions = config.build.rollupOptions.input; | ||
|
||
if (!inputOptions) { | ||
htmlFilePath = path.resolve(config.root, 'index.html'); | ||
} else if (typeof inputOptions === 'string') { | ||
entryFiles = [inputOptions]; | ||
htmlFilePath = path.resolve(config.root, inputOptions); | ||
} else if (Array.isArray(inputOptions)) { | ||
entryFiles = inputOptions; | ||
htmlFilePath = path.resolve(config.root, inputOptions[0]); | ||
} else if (typeof inputOptions === 'object') { | ||
entryFiles = Object.values(inputOptions); | ||
htmlFilePath = path.resolve(config.root, Object.values(inputOptions)[0]); | ||
} | ||
}, | ||
buildStart() { | ||
const hasHash = fileName?.includes?.("[hash") | ||
this.emitFile({ | ||
[hasHash ? "name" : "fileName"]: fileName, | ||
type: 'chunk', | ||
id: entryPath, | ||
preserveSignature: 'strict', | ||
}); | ||
if (htmlFilePath) { | ||
const htmlContent = fs.readFileSync(htmlFilePath, 'utf-8'); | ||
const scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi; | ||
let match: RegExpExecArray | null; | ||
|
||
while ((match = scriptRegex.exec(htmlContent)) !== null) { | ||
entryFiles.push(match[1]); | ||
} | ||
} | ||
}, | ||
transform(code, id) { | ||
if (entryFiles.some(file => id.endsWith(file))) { | ||
const injection = ` | ||
import ${JSON.stringify(entryPath)}; | ||
`; | ||
return injection + code | ||
} | ||
} | ||
} | ||
] | ||
}; | ||
|
||
export default addEntry; |