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

Native build plugin #45

Merged
merged 17 commits into from
Sep 12, 2024
20 changes: 16 additions & 4 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ModuleFormat,
OutputOptions,
Plugin,
RolldownPlugin,
RollupBuild,
RollupError,
RollupLog,
Expand All @@ -17,6 +18,10 @@ import type {
// RollupWatcher,
// WatcherOptions,
} from 'rolldown'
import {
loadFallbackPlugin as nativeLoadFallbackPlugin,
manifestPlugin as nativeManifestPlugin,
} from 'rolldown/experimental'
import type { RollupCommonJSOptions } from 'dep-types/commonjs'
import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
import type { TransformOptions } from 'esbuild'
Expand Down Expand Up @@ -425,7 +430,7 @@ export function resolveBuildOptions(

export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
pre: Plugin[]
post: Plugin[]
post: RolldownPlugin[]
}> {
const options = config.build
// Note: The rolldown internal support commonjs
Expand All @@ -434,6 +439,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
// !Array.isArray(commonjsOptions?.include) ||
// commonjsOptions?.include.length !== 0
const rollupOptionsPlugins = options.rollupOptions.plugins
const enableNativePlugin = config.experimental.enableNativePlugin
return {
pre: [
completeSystemWrapPlugin(),
Expand All @@ -445,17 +451,23 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
...(config.isWorker ? [webWorkerPostPlugin()] : []),
],
post: [
buildImportAnalysisPlugin(config),
...buildImportAnalysisPlugin(config),
...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []),
...(options.minify ? [terserPlugin(config)] : []),
...(!config.isWorker
? [
...(options.manifest ? [manifestPlugin(config)] : []),
...(options.manifest
? [
enableNativePlugin
? nativeManifestPlugin()
: manifestPlugin(config),
]
: []),
...(options.ssrManifest ? [ssrManifestPlugin(config)] : []),
buildReporterPlugin(config),
]
: []),
loadFallbackPlugin(),
enableNativePlugin ? nativeLoadFallbackPlugin() : loadFallbackPlugin(),
],
}
}
Expand Down
24 changes: 22 additions & 2 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { init, parse as parseImports } from 'es-module-lexer'
import type { SourceMap } from 'rolldown'
import type { RawSourceMap } from '@ampproject/remapping'
import convertSourceMap from 'convert-source-map'
import { buildImportAnalysisPlugin as nativeBuildImportAnalysisPlugin } from 'rolldown/experimental'
import {
combineSourcemaps,
generateCodeFrame,
Expand Down Expand Up @@ -162,9 +163,10 @@ function preload(
/**
* Build only. During serve this is performed as part of ./importAnalysis.
*/
export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
export function buildImportAnalysisPlugin(config: ResolvedConfig): [Plugin] {
const ssr = !!config.build.ssr
const isWorker = config.isWorker
const enableNativePlugin = config.experimental.enableNativePlugin
const insertPreload = !(ssr || !!config.build.lib || isWorker)

const renderBuiltUrl = config.experimental.renderBuiltUrl
Expand Down Expand Up @@ -194,7 +196,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
`function(dep) { return ${JSON.stringify(config.base)}+dep }`
const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}`

return {
const jsPlugin = {
name: 'vite:build-import-analysis',
resolveId(id) {
if (id === preloadHelperId) {
Expand Down Expand Up @@ -710,5 +712,23 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}
}
},
} as Plugin
if (enableNativePlugin) {
delete jsPlugin.transform
delete jsPlugin.resolveId
delete jsPlugin.load
}
return [
jsPlugin,
enableNativePlugin
? nativeBuildImportAnalysisPlugin({
preloadCode: preloadCode,
insertPreload: insertPreload,
/// this field looks redundant, put a dummy value for now
optimizeModulePreloadRelativePaths: false,
renderBuiltUrl: Boolean(renderBuiltUrl),
isRelativeBase: isRelativeBase,
})
: null,
].filter(Boolean) as [Plugin]
}
2 changes: 1 addition & 1 deletion playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ describe.runIf(isServe)('invalid', () => {

test('should reload when fixed', async () => {
await page.goto(viteTestUrl + '/invalid.html')
await editFile('invalid.html', (content) => {
editFile('invalid.html', (content) => {
return content.replace('<div Bad', '<div> Good')
})
const content = await page.waitForSelector('text=Good HTML')
Expand Down