Skip to content
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
23 changes: 17 additions & 6 deletions packages/vite/src/node/plugins/oxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { ViteDevServer } from '../server'
import { JS_TYPES_RE, VITE_PACKAGE_DIR } from '../constants'
import type { Logger } from '../logger'
import { type ESBuildOptions, getTSConfigResolutionCache } from './esbuild'
import { getRealPath } from './resolve'

// IIFE content looks like `var MyLib = (function() {` or `this.nested.myLib = (function() {`.
export const IIFE_BEGIN_RE: RegExp =
Expand Down Expand Up @@ -299,8 +300,9 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {

return result
}
const runtimeResolveBase = normalizePath(
path.join(VITE_PACKAGE_DIR, 'package.json'),
const runtimePackageDir = getRealPath(
path.join(VITE_PACKAGE_DIR, 'node_modules', '@oxc-project', 'runtime'),
config.resolve.preserveSymlinks,
Comment on lines +303 to +305
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this will work with yarn pnp (☹️). I think I'd prefer if this is fixed on @oxc-project/runtime side since we have control of it. The exports should just have "import" and "default" without fallback arrays.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yarn pnp... I'll check if we can remove node & require condition and make it ESM only.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with removing node & require condition from @oxc-project/runtime is that @oxc-project/runtime can no longer support older Node versions. This means packages that support older Node versions cannot use Oxc to transform the package and externalize @oxc-project/runtime (so that it can be deduped).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably @oxc-project/runtime can add module-sync and module condition as it doesn't have the problem of babel/babel#12865.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that @oxc-project/runtime has the node engine set as ^20.19.0 || >=22.12.0, so I think it's ok? (if that's what you're referring to). With the package being new, I think (IMO) it can break off some of babel's old decisions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that range wasn't meant to be there. It doesn't make sense to require a newer version of Node when the job of Oxc is to allow running in older version of Node (and other runtimes) that at least supports ES2015.

)

let server: ViteDevServer
Expand All @@ -315,13 +317,22 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
? {
resolveId: {
filter: {
id: prefixRegex('@oxc-project/runtime/'),
id: prefixRegex('@oxc-project/runtime/helpers/'),
},
async handler(id, _importer, opts) {
async handler(id, _importer, _opts) {
// @oxc-project/runtime imports will be injected by Oxc transform
// since it's injected by the transform, @oxc-project/runtime should be resolved to the one Vite depends on
const resolved = await this.resolve(id, runtimeResolveBase, opts)
return resolved
const helperName = id.slice(
'@oxc-project/runtime/helpers/'.length,
)
// Rolldown always uses the esm version of @oxc-project/runtime
// https://github.com/rolldown/rolldown/blob/v1.0.0-rc.9/crates/rolldown_plugin_oxc_runtime/src/lib.rs#L27
const helperId = path.posix.join(
runtimePackageDir,
'src/helpers/esm',
helperName + (id.endsWith('.js') ? '' : '.js'),
)
return helperId
},
order: 'pre',
},
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,10 @@ function tryResolveRealFileOrType(
return
}

function getRealPath(resolved: string, preserveSymlinks?: boolean): string {
export function getRealPath(
resolved: string,
preserveSymlinks?: boolean,
): string {
if (!preserveSymlinks) {
resolved = safeRealpathSync(resolved)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function html(strings: TemplateStringsArray, ...values: unknown[]) {
return strings.join('')
}
export const result = html`<script>
console.log('hi')
</script>`
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('module runner initialization', async () => {
const it = await createModuleRunnerTester({
resolve: {
external: ['tinyglobby'],
noExternal: ['@oxc-project/runtime'],
},
})

Expand Down Expand Up @@ -396,6 +397,15 @@ describe('module runner initialization', async () => {
)
})

it('oxc runtime helpers are loadable', async ({ runner }) => {
const mod = await runner.import('/fixtures/oxc-runtime-helper.ts')
expect(mod.result).toMatchInlineSnapshot(`
"<script>
console.log('hi')
</script>"
`)
})

it(`handle Object variable`, async ({ runner }) => {
const mod = await runner.import('/fixtures/top-level-object.js')
expect(mod).toMatchInlineSnapshot(`
Expand Down
Loading