-
-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: replace vite-node with Vite's native RunnableDevEnvironment #1716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
caaf9a9
5a87e02
df52f4d
b09c352
41d44ff
ab1fb61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,5 @@ | ||||||||||||||||||||
| import { createServer, version as viteVersion, mergeConfig } from 'vite' | ||||||||||||||||||||
| import type { ViteDevServer, UserConfig } from 'vite' | ||||||||||||||||||||
| import { ViteNodeRunner } from 'vite-node/client' | ||||||||||||||||||||
| import { ViteNodeServer } from 'vite-node/server' | ||||||||||||||||||||
| import { installSourcemapsSupport } from 'vite-node/source-map' | ||||||||||||||||||||
| import { createServer, isRunnableDevEnvironment, mergeConfig } from 'vite' | ||||||||||||||||||||
| import type { ViteDevServer, RunnableDevEnvironment, UserConfig } from 'vite' | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import { getPaths } from '@cedarjs/project-config' | ||||||||||||||||||||
| import { | ||||||||||||||||||||
|
|
@@ -19,10 +16,16 @@ async function createViteServer(customConfig: UserConfig = {}) { | |||||||||||||||||||
| const defaultConfig: UserConfig = { | ||||||||||||||||||||
| mode: 'production', | ||||||||||||||||||||
| optimizeDeps: { | ||||||||||||||||||||
| // This is recommended in the vite-node readme | ||||||||||||||||||||
| noDiscovery: true, | ||||||||||||||||||||
| include: undefined, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| server: { | ||||||||||||||||||||
| hmr: false, | ||||||||||||||||||||
| watch: null, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| environments: { | ||||||||||||||||||||
| nodeRunnerEnv: {}, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| resolve: { | ||||||||||||||||||||
| alias: [ | ||||||||||||||||||||
| { | ||||||||||||||||||||
|
|
@@ -45,17 +48,12 @@ async function createViteServer(customConfig: UserConfig = {}) { | |||||||||||||||||||
|
|
||||||||||||||||||||
| const server = await createServer(mergedConfig) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // For old Vite, this is needed to initialize the plugins. | ||||||||||||||||||||
| if (Number(viteVersion.split('.')[0]) < 6) { | ||||||||||||||||||||
| await server.pluginContainer.buildStart({}) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return server | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export class NodeRunner { | ||||||||||||||||||||
| private viteServer?: ViteDevServer = undefined | ||||||||||||||||||||
| private runner?: ViteNodeRunner = undefined | ||||||||||||||||||||
| private env?: RunnableDevEnvironment = undefined | ||||||||||||||||||||
| private readonly customViteConfig: UserConfig | ||||||||||||||||||||
|
|
||||||||||||||||||||
| constructor(customViteConfig: UserConfig = {}) { | ||||||||||||||||||||
|
|
@@ -64,39 +62,21 @@ export class NodeRunner { | |||||||||||||||||||
|
|
||||||||||||||||||||
| async init() { | ||||||||||||||||||||
| this.viteServer = await createViteServer(this.customViteConfig) | ||||||||||||||||||||
| const nodeServer = new ViteNodeServer(this.viteServer, { | ||||||||||||||||||||
| transformMode: { | ||||||||||||||||||||
| ssr: [/.*/], | ||||||||||||||||||||
| web: [/\/web\//], | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| deps: { | ||||||||||||||||||||
| fallbackCJS: true, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| }) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // fixes stacktraces in Errors | ||||||||||||||||||||
| installSourcemapsSupport({ | ||||||||||||||||||||
| getSourceMap: (source) => nodeServer?.getSourceMap(source), | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| const env = this.viteServer.environments.nodeRunnerEnv | ||||||||||||||||||||
| if (!env || !isRunnableDevEnvironment(env)) { | ||||||||||||||||||||
| throw new Error('Vite environment is not runnable.') | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+66
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| this.runner = new ViteNodeRunner({ | ||||||||||||||||||||
| root: this.viteServer.config.root, | ||||||||||||||||||||
| base: this.viteServer.config.base, | ||||||||||||||||||||
| fetchModule(id) { | ||||||||||||||||||||
| return nodeServer.fetchModule(id) | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| resolveId(id, importer) { | ||||||||||||||||||||
| return nodeServer.resolveId(id, importer) | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| this.env = env | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| async importFile(filePath: string) { | ||||||||||||||||||||
| if (!this.runner) { | ||||||||||||||||||||
| if (!this.env) { | ||||||||||||||||||||
| await this.init() | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return this.runner?.executeFile(filePath) | ||||||||||||||||||||
| return this.env?.runner.import(filePath) | ||||||||||||||||||||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| async close() { | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.