Skip to content

Commit c423ee3

Browse files
committed
feat: init
1 parent abc49a3 commit c423ee3

File tree

11 files changed

+289
-16
lines changed

11 files changed

+289
-16
lines changed

packages/vite/src/node/__tests__/plugins/hooks.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('supports plugin context', () => {
210210
})
211211

212212
test('transformIndexHtml hook in build', async () => {
213-
expect.assertions(4)
213+
expect.assertions(3)
214214

215215
await buildWithPlugin({
216216
name: 'test',
@@ -223,7 +223,7 @@ describe('supports plugin context', () => {
223223
meta: expect.any(Object),
224224
})
225225
expect(this.meta.rollupVersion).toBeTypeOf('string')
226-
expect(this.meta.viteVersion).toBeTypeOf('string')
226+
// expect(this.meta.viteVersion).toBeTypeOf('string')
227227
expect(this.meta.watchMode).toBe(false)
228228
},
229229
})

packages/vite/src/node/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ const configDefaults = Object.freeze({
769769
importGlobRestoreExtension: false,
770770
renderBuiltUrl: undefined,
771771
hmrPartialAccept: false,
772-
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v1',
772+
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : true,
773773
},
774774
future: {
775775
removePluginHookHandleHotUpdate: undefined,
@@ -2061,8 +2061,9 @@ function resolveNativePluginEnabledLevel(
20612061
case 'resolver':
20622062
return 0
20632063
case 'v1':
2064-
case true:
20652064
return 1
2065+
case true:
2066+
return 2
20662067
case false:
20672068
return -1
20682069
default:

packages/vite/src/node/plugins/asset.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path'
22
import fsp from 'node:fs/promises'
33
import { Buffer } from 'node:buffer'
44
import * as mrmime from 'mrmime'
5+
import { viteAssetPlugin as nativeAssetPlugin } from 'rolldown/experimental'
56
import type {
67
NormalizedOutputOptions,
78
PluginContext,
@@ -15,7 +16,7 @@ import {
1516
createToImportMetaURLBasedRelativeRuntime,
1617
toOutputFilePathInJS,
1718
} from '../build'
18-
import type { Plugin } from '../plugin'
19+
import { type Plugin, perEnvironmentPlugin } from '../plugin'
1920
import type { ResolvedConfig } from '../config'
2021
import { checkPublicFile } from '../publicDir'
2122
import {
@@ -148,6 +149,23 @@ export function renderAssetUrlInJS(
148149
* Also supports loading plain strings with import text from './foo.txt?raw'
149150
*/
150151
export function assetPlugin(config: ResolvedConfig): Plugin {
152+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 2) {
153+
return perEnvironmentPlugin('native:asset', (env) => {
154+
return nativeAssetPlugin({
155+
root: env.config.root,
156+
isLib: !!env.config.build.lib,
157+
isSsr: !!env.config.build.ssr,
158+
isWorker: env.config.isWorker,
159+
urlBase: env.config.base,
160+
publicDir: env.config.publicDir,
161+
decodedBase: env.config.decodedBase,
162+
isSkipAssets: !env.config.build.emitAssets,
163+
assetInlineLimit: env.config.build.assetsInlineLimit,
164+
assetsInclude: env.config.rawAssetsInclude,
165+
})
166+
})
167+
}
168+
151169
registerCustomMime()
152170

153171
return {

packages/vite/src/node/plugins/assetImportMetaUrl.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import path from 'node:path'
22
import MagicString from 'magic-string'
33
import { stripLiteral } from 'strip-literal'
44
import { exactRegex } from '@rolldown/pluginutils'
5-
import type { Plugin } from '../plugin'
5+
import { viteAssetImportMetaUrlPlugin } from 'rolldown/experimental'
6+
import { type Plugin, perEnvironmentPlugin } from '../plugin'
67
import type { ResolvedConfig } from '../config'
78
import {
89
injectQuery,
@@ -44,6 +45,30 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
4445
asSrc: true,
4546
}
4647

48+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 2) {
49+
return perEnvironmentPlugin('vite:native-asset-import-meta-url', (env) => {
50+
if (env.config.consumer === 'client') {
51+
return viteAssetImportMetaUrlPlugin({
52+
root: env.config.root,
53+
isLib: !!env.config.build.lib,
54+
publicDir: env.config.publicDir,
55+
clientEntry: CLIENT_ENTRY,
56+
assetInlineLimit: env.config.build.assetsInlineLimit,
57+
tryFsResolve: (file) => tryFsResolve(file, fsResolveOptions),
58+
assetResolver: (url, importer) => {
59+
assetResolver ??= createBackCompatIdResolver(env.config, {
60+
extensions: [],
61+
mainFields: [],
62+
tryIndex: false,
63+
preferRelative: true,
64+
})
65+
return assetResolver(env, url, importer)
66+
},
67+
})
68+
}
69+
})
70+
}
71+
4772
return {
4873
name: 'vite:asset-import-meta-url',
4974

packages/vite/src/node/plugins/css.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type {
2727
TransformAttributeResult as LightningCssTransformAttributeResult,
2828
TransformResult as LightningCssTransformResult,
2929
} from 'lightningcss'
30+
import { viteCSSPlugin, viteCSSPostPlugin } from 'rolldown/experimental'
3031
import type { LightningCSSOptions } from '#types/internal/lightningcssOptions'
3132
import type {
3233
LessPreprocessorBaseOptions,
@@ -89,7 +90,7 @@ import type { ResolveIdFn } from '../idResolver'
8990
import { PartialEnvironment } from '../baseEnvironment'
9091
import type { TransformPluginContext } from '../server/pluginContainer'
9192
import { searchForWorkspaceRoot } from '../server/searchRoot'
92-
import { type DevEnvironment } from '..'
93+
import { type DevEnvironment, perEnvironmentPlugin } from '..'
9394
import type { PackageCache } from '../packages'
9495
import { findNearestMainPackageData } from '../packages'
9596
import { nodeResolveWithVite } from '../nodeResolve'
@@ -311,6 +312,49 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
311312
})
312313
}
313314

315+
if (isBuild && config.nativePluginEnabledLevel >= 2) {
316+
return perEnvironmentPlugin('vite:native-css', (env) => {
317+
return [
318+
{
319+
name: 'vite:css-compat',
320+
buildStart() {
321+
preprocessorWorkerController = createPreprocessorWorkerController(
322+
normalizeMaxWorkers(config.css.preprocessorMaxWorkers),
323+
)
324+
preprocessorWorkerControllerCache.set(
325+
config,
326+
preprocessorWorkerController,
327+
)
328+
},
329+
330+
buildEnd() {
331+
preprocessorWorkerController?.close()
332+
},
333+
},
334+
viteCSSPlugin({
335+
root: env.config.root,
336+
isLib: !!env.config.build.lib,
337+
publicDir: env.config.publicDir,
338+
async compileCSS(url, importer, resolver) {
339+
return compileCSS(
340+
env,
341+
url,
342+
importer,
343+
preprocessorWorkerController!,
344+
(url, importer) => {
345+
return resolver.call(url, importer)
346+
},
347+
)
348+
},
349+
resolveUrl(url, importer) {
350+
return idResolver(env, url, importer)
351+
},
352+
assetInlineLimit: env.config.build.assetsInlineLimit,
353+
}),
354+
]
355+
})
356+
}
357+
314358
return {
315359
name: 'vite:css',
316360

@@ -506,6 +550,43 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
506550
return cssBundleName
507551
}
508552

553+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 2) {
554+
return perEnvironmentPlugin('native:css-post', (env) => {
555+
let libCssFilename: string | undefined
556+
if (env.config.build.lib) {
557+
const libOptions = env.config.build.lib
558+
if (typeof libOptions.cssFileName === 'string') {
559+
libCssFilename = `${libOptions.cssFileName}.css`
560+
} else if (typeof libOptions.fileName === 'string') {
561+
libCssFilename = `${libOptions.fileName}.css`
562+
}
563+
}
564+
return [
565+
viteCSSPostPlugin({
566+
root: env.config.root,
567+
isLib: !!env.config.build.lib,
568+
isSsr: !!env.config.build.ssr,
569+
isWorker: env.config.isWorker,
570+
isClient: env.config.consumer === 'client',
571+
cssCodeSplit: env.config.build.cssCodeSplit,
572+
sourcemap: !!env.config.build.sourcemap,
573+
assetsDir: env.config.build.assetsDir,
574+
urlBase: env.config.base,
575+
decodedBase: env.config.decodedBase,
576+
libCssFilename,
577+
cssMinify: env.config.build.cssMinify
578+
? async (content, inline) => {
579+
return await minifyCSS(content, env.config, inline)
580+
}
581+
: undefined,
582+
renderBuiltUrl: env.config.experimental.renderBuiltUrl,
583+
isOutputOptionsForLegacyChunks:
584+
env.config.isOutputOptionsForLegacyChunks,
585+
}),
586+
]
587+
})
588+
}
589+
509590
return {
510591
name: 'vite:css-post',
511592

packages/vite/src/node/plugins/html.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ import type {
1717
} from 'parse5'
1818
import { stripLiteral } from 'strip-literal'
1919
import escapeHtml from 'escape-html'
20-
import type { MinimalPluginContextWithoutEnvironment, Plugin } from '../plugin'
20+
import {
21+
viteHtmlInlineProxyPlugin as nativeHtmlInlineProxyPlugin,
22+
viteHtmlPlugin,
23+
} from 'rolldown/experimental'
24+
import {
25+
type MinimalPluginContextWithoutEnvironment,
26+
type Plugin,
27+
perEnvironmentPlugin,
28+
} from '../plugin'
2129
import type { ViteDevServer } from '../server'
2230
import {
2331
decodeURIIfPossible,
@@ -101,6 +109,12 @@ export const htmlProxyMap: WeakMap<
101109
export const htmlProxyResult: Map<string, string> = new Map()
102110

103111
export function htmlInlineProxyPlugin(config: ResolvedConfig): Plugin {
112+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 2) {
113+
return nativeHtmlInlineProxyPlugin({
114+
root: config.root,
115+
})
116+
}
117+
104118
// Should do this when `constructor` rather than when `buildStart`,
105119
// `buildStart` will be triggered multiple times then the cached result will be emptied.
106120
// https://github.com/vitejs/vite/issues/6372
@@ -366,6 +380,33 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
366380
// Same reason with `htmlInlineProxyPlugin`
367381
isAsyncScriptMap.set(config, new Map())
368382

383+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 2) {
384+
return perEnvironmentPlugin('native:vite-html', (env) => {
385+
return viteHtmlPlugin({
386+
root: env.config.root,
387+
isLib: !!env.config.build.lib,
388+
isSsr: !!env.config.build.ssr,
389+
urlBase: env.config.base,
390+
publicDir: env.config.publicDir,
391+
decodedBase: env.config.decodedBase,
392+
modulePreload: env.config.build.modulePreload,
393+
cssCodeSplit: env.config.build.cssCodeSplit,
394+
assetInlineLimit: env.config.build.assetsInlineLimit,
395+
preHooks,
396+
normalHooks,
397+
postHooks,
398+
async applyHtmlTransforms(html, hooks, pluginContext, ctx) {
399+
return applyHtmlTransforms(
400+
html,
401+
hooks as IndexHtmlTransformHook[],
402+
pluginContext,
403+
ctx,
404+
)
405+
},
406+
})
407+
})
408+
}
409+
369410
return {
370411
name: 'vite:build-html',
371412

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,33 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
744744
},
745745
}
746746

747+
if (config.nativePluginEnabledLevel >= 2) {
748+
return [
749+
perEnvironmentPlugin('native:import-analysis-build', (env) => {
750+
const preloadCode = getPreloadCode(
751+
env,
752+
!!renderBuiltUrl,
753+
isRelativeBase,
754+
)
755+
return nativeBuildImportAnalysisPlugin({
756+
preloadCode,
757+
insertPreload: getInsertPreload(env),
758+
// this field looks redundant, put a dummy value for now
759+
optimizeModulePreloadRelativePaths: false,
760+
renderBuiltUrl: !!renderBuiltUrl,
761+
isRelativeBase,
762+
v2: {
763+
isSsr: !!env.config.build.ssr,
764+
urlBase: env.config.base,
765+
decodedBase: env.config.decodedBase,
766+
modulePreload: env.config.build.modulePreload,
767+
renderBuiltUrl: env.config.experimental.renderBuiltUrl,
768+
},
769+
})
770+
}),
771+
]
772+
}
773+
747774
if (config.nativePluginEnabledLevel >= 1) {
748775
delete plugin.transform
749776
delete plugin.resolveId

0 commit comments

Comments
 (0)