Skip to content

Commit

Permalink
Merge pull request #2 from vite-plugin/v0.2.0
Browse files Browse the repository at this point in the history
V0.2.0
  • Loading branch information
caoxiemeihao authored Feb 18, 2023
2 parents b6a8bf6 + 5bf2ba7 commit da1dbd6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0 (2023-02-18)

- 221cb24 feat: support specify run command

## 0.1.1 (2023-01-17)

- 6b31cd6 fix: correct types
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ multiple(
* Vite config file path.
*/
config: string
/**
* Explicitly specify the run command.
*/
command?: 'build' | 'serve'
}[],
options: {
/**
* Called when all builds are complete.
*/
callback?: (command: ResolvedConfig['command']) => void,
callback?: () => void,
} = {},
)
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-multiple",
"version": "0.1.1",
"version": "0.2.0",
"description": "Allow multiple Vite to run simultaneously",
"type": "module",
"main": "index.js",
Expand Down
60 changes: 34 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ export default function multiple(
* Vite config file path.
*/
config: string
/**
* Explicitly specify the run command.
*/
command?: 'build' | 'serve'
}[],
options: {
/**
* Called when all builds are complete.
*/
callback?: (command: ResolvedConfig['command']) => void,
callback?: () => void,
} = {},
): Plugin {
let config: ResolvedConfig
Expand All @@ -41,23 +45,23 @@ export default function multiple(
},
configureServer(server) {
if (server.httpServer) {
server.httpServer.once('listening', () => serve(config, apps).then(() => options.callback?.('serve')))
server.httpServer.once('listening', () => run(config, apps, 'serve').then(() => options.callback?.()))
} else {
serve(config, apps).then(() => options.callback?.('serve'))
run(config, apps, 'serve').then(() => options.callback?.())
}
},
async closeBundle() {
if (config.command === 'build') {
await build(config, apps)
options.callback?.(config.command)
await run(config, apps, config.command)
options.callback?.()
}
},
}
}

export async function resolveConfig(config: ResolvedConfig, app: AppConfig): Promise<UserConfig> {
const { config: userConfig } = (await loadConfigFromFile({
command: config.command,
command: app.command!,
mode: config.mode,
ssrBuild: !!config.build?.ssr,
}, app.config)) ?? { path: '', config: {}, dependencies: [] };
Expand All @@ -74,30 +78,34 @@ export async function resolveConfig(config: ResolvedConfig, app: AppConfig): Pro
return mergeConfig(defaultConfig, userConfig);
}

export async function build(config: ResolvedConfig, apps: AppConfig[]) {
for (const app of apps) {
const userConfig = await resolveConfig(config, app)
await viteBuild({
// 🚧 Avoid recursive build caused by load default config file.
configFile: false,
...userConfig,
})
}
}

export async function serve(config: ResolvedConfig, apps: AppConfig[]) {
export async function run(
config: ResolvedConfig,
apps: AppConfig[],
mainAppCommand: ResolvedConfig['command'],
): Promise<void> {
let port = 5174 // The port of main App is 5173

for (const app of apps) {
app.command ??= mainAppCommand

const userConfig = await resolveConfig(config, app)

userConfig.server ??= {}
userConfig.server.port ??= port++
if (app.command === 'serve') {
userConfig.server ??= {}
userConfig.server.port ??= port++

const viteDevServer = await createServer({
configFile: false,
...userConfig,
})
await viteDevServer.listen()
viteDevServer.printUrls()
const viteDevServer = await createServer({
configFile: false,
...userConfig,
})
await viteDevServer.listen()
viteDevServer.printUrls()
} else {
await viteBuild({
// 🚧 Avoid recursive build caused by load default config file.
configFile: false,
...userConfig,
})
}
}
}
2 changes: 1 addition & 1 deletion test/serve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ beforeAll(async () => new Promise(async resolve => {
config: path.join(root, 'bar/vite.config.mjs'),
},
], {
callback(command) {
callback() {
resolve()
},
})],
Expand Down

0 comments on commit da1dbd6

Please sign in to comment.