Skip to content
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

feat: preCloseBundle hook #9326

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,8 @@ export interface Plugin extends RollupPlugin {
id: string,
options?: { ssr?: boolean }
) => Promise<TransformResult> | TransformResult
/**
* Invoked between `writeBundle` and `closeBundle` hooks.
*/
preCloseBundle?: (this: PluginContext) => void | Promise<void>
}
13 changes: 12 additions & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ensureWatchPlugin } from './ensureWatch'
import { metadataPlugin } from './metadata'
import { dynamicImportVarsPlugin } from './dynamicImportVars'
import { importGlobPlugin } from './importMetaGlob'
import { preCloseBundlePlugin } from './preCloseBundle'

export async function resolvePlugins(
config: ResolvedConfig,
Expand All @@ -37,7 +38,7 @@ export async function resolvePlugins(
? (await import('../build')).resolveBuildPlugins(config)
: { pre: [], post: [] }

return [
const plugins: Plugin[] = [
isWatch ? ensureWatchPlugin() : null,
isBuild ? metadataPlugin() : null,
preAliasPlugin(config),
Expand Down Expand Up @@ -98,4 +99,14 @@ export async function resolvePlugins(
? []
: [clientInjectionsPlugin(config), importAnalysisPlugin(config)])
].filter(Boolean) as Plugin[]

if (!isBuild) return plugins

const preClosePlugins = plugins.filter(
(p) => typeof (p as any).preCloseBundle === 'function'
)

return preClosePlugins.length > 0
? preCloseBundlePlugin(plugins, preClosePlugins)
: plugins
}
28 changes: 28 additions & 0 deletions packages/vite/src/node/plugins/preCloseBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Plugin as RollupPlugin } from 'rollup'
import type { Plugin } from '../plugin'

export function preCloseBundlePlugin(
plugins: RollupPlugin[],
preClosePlugins: RollupPlugin[]
): RollupPlugin[] {
plugins
.filter((p) => typeof p.closeBundle === 'function')
.forEach((p) => {
const _closeBundle = p.closeBundle
delete p.closeBundle
;(p as any)._closeBundle = _closeBundle
})
plugins.push(<Plugin>{
name: 'vite:pre-close-bundle',
apply: 'build',
async closeBundle() {
await Promise.all(
preClosePlugins.map((p) => (p as any).preCloseBundle?.apply(this))
)
await Promise.all(
plugins.map((p) => (p as any)._closeBundle?.apply(this))
)
}
})
return plugins
}
5 changes: 5 additions & 0 deletions playground/pre-close-bundle-hook/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Hello</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions playground/pre-close-bundle-hook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-pre-close-bundle-hook",
"private": true,
"version": "0.0.0",
"scripts": {
"build": "vite build"
}
}
70 changes: 70 additions & 0 deletions playground/pre-close-bundle-hook/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { resolve } = require('path')

async function awaiting(timeout = 1000) {
await new Promise((resolve) => setTimeout(resolve, timeout))
}

let called1 = false
let called2 = false
let called3 = false

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
base: './',
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html')
}
}
},
plugins: [
{
name: 'pre-close-bundle-1',
enforce: 'pre',
apply: 'build',
async preCloseBundle() {
await awaiting(1200)
called1 = true
}
},
{
name: 'pre-close-bundle-2',
async preCloseBundle() {
await awaiting(1100)
called2 = true
}
},
{
name: 'pre-close-bundle-2',
enforce: 'post',
apply: 'build',
async preCloseBundle() {
await awaiting()
called3 = true
}
},
{
name: 'close-bundle-pre',
enforce: 'pre',
apply: 'build',
closeBundle() {
if (!called1 || !called2 || !called3) {
throw new Error('some pre-close-bundle not being called!')
}
}
},
{
name: 'close-bundle-post',
enforce: 'post',
apply: 'build',
closeBundle() {
if (!called1 || !called2 || !called3) {
throw new Error('some pre-close-bundle not being called!')
}
}
}
]
}
4 changes: 2 additions & 2 deletions scripts/patchCJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if (matchMixed) {
writeFileSync(indexPath, lines.join('\n'))

console.log(colors.bold(`${indexPath} CJS patched`))
process.exit()
process.exit(0)
}

const matchDefault = code.match(/\nmodule.exports = (\w+);/)
Expand All @@ -50,7 +50,7 @@ if (matchDefault) {
code += `module.exports["default"] = ${matchDefault[1]};\n`
writeFileSync(indexPath, code)
console.log(colors.bold(`${indexPath} CJS patched`))
process.exit()
process.exit(0)
}

console.error(colors.red(`${indexPath} CJS patch failed`))
Expand Down