Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"picocolors": "^1.1.1",
"playwright-chromium": "^1.58.2",
"prettier": "3.8.1",
"rolldown": "1.0.0-rc.3",
"rolldown": "1.0.0-rc.4",
"rollup": "^4.43.0",
"simple-git-hooks": "^2.13.1",
"tsx": "^4.21.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@
},
"//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!",
"dependencies": {
"@oxc-project/runtime": "0.112.0",
"@oxc-project/runtime": "0.113.0",
"fdir": "^6.5.0",
"lightningcss": "^1.31.1",
"picomatch": "^4.0.3",
"postcss": "^8.5.6",
"rolldown": "1.0.0-rc.3",
"rolldown": "1.0.0-rc.4",
"tinyglobby": "^0.2.15"
},
"optionalDependencies": {
Expand All @@ -88,7 +88,7 @@
"@babel/parser": "^7.29.0",
"@jridgewell/remapping": "^2.3.5",
"@jridgewell/trace-mapping": "^0.3.31",
"@oxc-project/types": "0.112.0",
"@oxc-project/types": "0.113.0",
"@polka/compression": "^1.0.0-next.25",
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-commonjs": "^29.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/rolldown.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ const identifierReplacements: Record<string, Record<string, string>> = {
Plugin$1: 'Rolldown.Plugin',
TransformResult$1: 'Rolldown.TransformResult',
},
'rolldown/experimental': {
TransformOptions$1: 'rolldown_experimental_TransformOptions',
TransformResult$2: 'rolldown_experimental_TransformResult',
'rolldown/utils': {
TransformOptions$1: 'rolldown_utils_TransformOptions',
TransformResult$2: 'rolldown_utils_TransformResult',
},
'node:http': {
Server$1: 'http.Server',
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/__tests__/plugins/define.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe.skipIf(!process.env._VITE_TEST_JS_PLUGIN)('definePlugin', () => {
// assert that the default behavior is to replace import.meta.hot with undefined
const transform = await createDefinePluginTransform()
expect(await transform('export const hot = import.meta.hot;')).toBe(
'export const hot = void 0;\n',
'export const hot = undefined;\n',
)
// assert that we can specify a user define to preserve import.meta.hot
const overrideTransform = await createDefinePluginTransform({
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import { performance } from 'node:perf_hooks'
import { scan, transformSync } from 'rolldown/experimental'
import { scan } from 'rolldown/experimental'
import { transformSync } from 'rolldown/utils'
import type { PartialResolvedId, Plugin } from 'rolldown'
import colors from 'picocolors'
import { glob } from 'tinyglobby'
Expand Down Expand Up @@ -386,7 +387,10 @@ function rolldownScanPlugin(
let transpiledContents: string
// transpile because `transformGlobImport` only expects js
if (loader !== 'js') {
const result = transformSync(id, contents, { lang: loader })
const result = transformSync(id, contents, {
lang: loader,
tsconfig: false,
})
if (result.errors.length > 0) {
throw new AggregateError(result.errors, 'oxc transform error')
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformSync } from 'rolldown/experimental'
import { transformSync } from 'rolldown/utils'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import { escapeRegex, isCSSRequest } from '../utils'
Expand Down Expand Up @@ -221,6 +221,7 @@ export async function replaceDefine(
environment.config.command === 'build'
? !!environment.config.build.sourcemap
: true,
tsconfig: false,
})

if (result.errors.length > 0) {
Expand Down
120 changes: 94 additions & 26 deletions packages/vite/src/node/plugins/oxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import path from 'node:path'
import type {
TransformOptions as OxcTransformOptions,
TransformResult as OxcTransformResult,
} from 'rolldown/experimental'
import {
viteTransformPlugin as nativeTransformPlugin,
transformSync,
} from 'rolldown/experimental'
} from 'rolldown/utils'
import { transformSync } from 'rolldown/utils'
import { viteTransformPlugin as nativeTransformPlugin } from 'rolldown/experimental'
import type { RawSourceMap } from '@jridgewell/remapping'
import type { RollupError, SourceMap } from 'rolldown'
import type { RolldownError, RolldownLog, SourceMap } from 'rolldown'
import { TSConfckParseError } from 'tsconfck'
import colors from 'picocolors'
import { prefixRegex } from 'rolldown/filter'
Expand All @@ -17,7 +15,6 @@ import {
combineSourcemaps,
createFilter,
ensureWatchedFile,
generateCodeFrame,
normalizePath,
} from '../utils'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -212,14 +209,64 @@ function setOxcTransformOptionsFromTsconfigOptions(
!useDefineForClassFields
}

// Copy from rolldown's packages/rolldown/src/utils/errors.ts
function joinNewLine(s1: string, s2: string): string {
// ensure single new line in between
return s1.replace(/\n+$/, '') + '\n' + s2.replace(/^\n+/, '')
}

// Copy from rolldown's packages/rolldown/src/utils/errors.ts
function getErrorMessage(e: RolldownError): string {
// If the `kind` field is present, we assume it represents
// a custom error defined by rolldown on the Rust side.
if (Object.hasOwn(e, 'kind')) {
return e.message
}

let s = ''
if (e.plugin) {
s += `[plugin ${e.plugin}]`
}
const id = e.id ?? e.loc?.file
if (id) {
s += ' ' + id
if (e.loc) {
s += `:${e.loc.line}:${e.loc.column}`
}
}
if (s) {
s += '\n'
}
const message = `${e.name ?? 'Error'}: ${e.message}`
s += message
if (e.frame) {
s = joinNewLine(s, e.frame)
}
// copy stack since it's important for js plugin error
if (e.stack) {
s = joinNewLine(s, e.stack.replace(message, ''))
}
if (e.cause) {
s = joinNewLine(s, 'Caused by:')
s = joinNewLine(
s,
getErrorMessage(e.cause as any)
.split('\n')
.map((line) => ' ' + line)
.join('\n'),
)
}
return s
}

export async function transformWithOxc(
code: string,
filename: string,
options?: OxcTransformOptions,
inMap?: object,
config?: ResolvedConfig,
watcher?: FSWatcher,
): Promise<Omit<OxcTransformResult, 'errors'> & { warnings: string[] }> {
): Promise<Omit<OxcTransformResult, 'errors'>> {
const warnings: string[] = []
let lang = options?.lang

Expand All @@ -243,6 +290,7 @@ export async function transformWithOxc(
sourcemap: true,
...options,
lang,
tsconfig: false,
}

if (lang === 'ts' || lang === 'tsx') {
Expand Down Expand Up @@ -272,23 +320,33 @@ export async function transformWithOxc(
const result = transformSync(filename, code, resolvedOptions)

if (result.errors.length > 0) {
const firstError = result.errors[0]
const error: RollupError = new Error(firstError.message)
let frame = ''
frame += firstError.labels
.map(
(l) =>
(l.message ? `${l.message}\n` : '') +
generateCodeFrame(code, l.start, l.end),
)
.join('\n')
if (firstError.helpMessage) {
frame += '\n' + firstError.helpMessage
// Copy from rolldown's packages/rolldown/src/utils/errors.ts
let summary = `Transform failed with ${result.errors.length} error${result.errors.length < 2 ? '' : 's'}:\n`
for (let i = 0; i < result.errors.length; i++) {
summary += '\n'
if (i >= 5) {
summary += '...'
break
}
summary += getErrorMessage(result.errors[i])
}
error.frame = frame
error.pos =
firstError.labels.length > 0 ? firstError.labels[0].start : undefined
throw error

const wrapper = new Error(summary)
// expose individual errors as getters so that
// `console.error(wrapper)` doesn't expand unnecessary details
// when they are already presented in `wrapper.message`
Object.defineProperty(wrapper, 'errors', {
configurable: true,
enumerable: true,
get: () => result.errors,
set: (value) =>
Object.defineProperty(wrapper, 'errors', {
configurable: true,
enumerable: true,
value,
}),
})
throw wrapper
}

let map: SourceMap
Expand All @@ -305,7 +363,6 @@ export async function transformWithOxc(
return {
...result,
map,
warnings,
}
}

Expand All @@ -319,6 +376,15 @@ function resolveTsconfigTarget(target: string | undefined): number | 'next' {
return parseInt(targetLowered.slice(2))
}

const warnedMessages = new Set<string>()
function shouldSkipWarning(warning: RolldownLog): boolean {
if (warning.code === 'UNSUPPORTED_TSCONFIG_OPTION') {
if (warnedMessages.has(warning.message)) return true
warnedMessages.add(warning.message)
}
return false
}

export function oxcPlugin(config: ResolvedConfig): Plugin {
if (config.isBundled && config.nativePluginEnabledLevel >= 1) {
return perEnvironmentPlugin('native:transform', (environment) => {
Expand Down Expand Up @@ -458,7 +524,9 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
result.code = jsxInject + ';' + result.code
}
for (const warning of result.warnings) {
this.environment.logger.warnOnce(warning)
if (!shouldSkipWarning(warning)) {
this.warn(warning)
}
}
return {
code: result.code,
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"convert-source-map": "^2.0.0",
"css-color-names": "^1.0.1",
"kill-port": "^1.6.1",
"rolldown": "1.0.0-rc.3"
"rolldown": "1.0.0-rc.4"
}
}
Loading