Skip to content

Commit ce51c11

Browse files
committed
feat: init
1 parent abc49a3 commit ce51c11

File tree

14 files changed

+398
-211
lines changed

14 files changed

+398
-211
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"picocolors": "^1.1.1",
6666
"playwright-chromium": "^1.56.1",
6767
"prettier": "3.6.2",
68-
"rolldown": "1.0.0-beta.51",
68+
"rolldown": "https://pkg.pr.new/rolldown@fa89561",
6969
"rollup": "^4.43.0",
7070
"simple-git-hooks": "^2.13.1",
7171
"tsx": "^4.20.6",

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"lightningcss": "^1.30.2",
8888
"picomatch": "^4.0.3",
8989
"postcss": "^8.5.6",
90-
"rolldown": "1.0.0-beta.51",
90+
"rolldown": "https://pkg.pr.new/rolldown@fa89561",
9191
"tinyglobby": "^0.2.15"
9292
},
9393
"optionalDependencies": {

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/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 >= 1) {
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 >= 1) {
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: 100 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 >= 1) {
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,61 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
506550
return cssBundleName
507551
}
508552

553+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 1) {
554+
const isLegacySet = new Set<string>()
555+
return perEnvironmentPlugin('native:css-post', (env) => {
556+
let libCssFilename: string | undefined
557+
if (env.config.build.lib) {
558+
const libOptions = env.config.build.lib
559+
if (typeof libOptions.cssFileName === 'string') {
560+
libCssFilename = `${libOptions.cssFileName}.css`
561+
} else if (typeof libOptions.fileName === 'string') {
562+
libCssFilename = `${libOptions.fileName}.css`
563+
}
564+
}
565+
return [
566+
{
567+
name: 'native:css-post-compat',
568+
...(config.isOutputOptionsForLegacyChunks
569+
? {
570+
renderChunk(_, __, opts) {
571+
const isLegacy =
572+
env.config.isOutputOptionsForLegacyChunks?.(opts) ?? false
573+
if (isLegacy) {
574+
isLegacySet.add(env.name)
575+
} else {
576+
isLegacySet.delete(env.name)
577+
}
578+
},
579+
}
580+
: {}),
581+
},
582+
viteCSSPostPlugin({
583+
root: env.config.root,
584+
isLib: !!env.config.build.lib,
585+
isSsr: !!env.config.build.ssr,
586+
isWorker: env.config.isWorker,
587+
isLegacy: env.config.isOutputOptionsForLegacyChunks
588+
? () => isLegacySet.has(env.name)
589+
: undefined,
590+
isClient: env.config.consumer === 'client',
591+
cssCodeSplit: env.config.build.cssCodeSplit,
592+
sourcemap: !!env.config.build.sourcemap,
593+
assetsDir: env.config.build.assetsDir,
594+
urlBase: env.config.base,
595+
decodedBase: env.config.decodedBase,
596+
libCssFilename,
597+
cssMinify: env.config.build.cssMinify
598+
? async (content, inline) => {
599+
return await minifyCSS(content, env.config, inline)
600+
}
601+
: undefined,
602+
renderBuiltUrl: env.config.experimental.renderBuiltUrl,
603+
}),
604+
]
605+
})
606+
}
607+
509608
return {
510609
name: 'vite:css-post',
511610

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 >= 1) {
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 >= 1) {
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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,24 +745,27 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
745745
}
746746

747747
if (config.nativePluginEnabledLevel >= 1) {
748-
delete plugin.transform
749-
delete plugin.resolveId
750-
delete plugin.load
751748
return [
752-
plugin,
753-
perEnvironmentPlugin('native:import-analysis-build', (environment) => {
749+
perEnvironmentPlugin('native:import-analysis-build', (env) => {
754750
const preloadCode = getPreloadCode(
755-
environment,
751+
env,
756752
!!renderBuiltUrl,
757753
isRelativeBase,
758754
)
759755
return nativeBuildImportAnalysisPlugin({
760756
preloadCode,
761-
insertPreload: getInsertPreload(environment),
757+
insertPreload: getInsertPreload(env),
762758
// this field looks redundant, put a dummy value for now
763759
optimizeModulePreloadRelativePaths: false,
764760
renderBuiltUrl: !!renderBuiltUrl,
765761
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+
},
766769
})
767770
}),
768771
]

0 commit comments

Comments
 (0)