|
8 | 8 | import { extname, sep } from 'node:path'
|
9 | 9 | import { merge } from 'ts-deepmerge'
|
10 | 10 | import { configuration } from './configure'
|
11 |
| -import { asyncGeneratorRegex, debug, parsers, runtimePublicPath } from './constants' |
| 11 | +import { asyncGeneratorRegex, debug, parsers, runtimePublicPath, USE_OXC } from './constants' |
12 | 12 | import type { Options } from './types'
|
13 | 13 |
|
14 | 14 | const ignoreId = new RegExp(`node_modules\\${sep}(\\.vite|vite)\\${sep}`)
|
@@ -45,108 +45,116 @@ export async function transformSWC(
|
45 | 45 | !options.forceJSX &&
|
46 | 46 | !id.includes('node_modules')
|
47 | 47 |
|
48 |
| - const reactConfig = { |
49 |
| - refresh, |
50 |
| - development: !options.forceJSX && !options.production, |
51 |
| - runtime: 'automatic', |
52 |
| - importSource: 'react', |
53 |
| - ...(configuration.enableNativewind && !id.includes('node_modules') |
54 |
| - ? { |
55 |
| - importSource: 'nativewind', |
56 |
| - // pragma: 'createInteropElement', |
57 |
| - // pragmaFrag: '_InteropFragment', |
58 |
| - // swc doesnt actually change the import right |
59 |
| - // runtime: 'classic', |
60 |
| - } |
61 |
| - : {}), |
62 |
| - } satisfies TransformConfig['react'] |
63 |
| - |
64 |
| - const transformOptions = ((): SWCOptions => { |
65 |
| - if (options.environment === 'client' || options.environment === 'ssr') { |
66 |
| - return { |
67 |
| - sourceMaps: shouldSourceMap(), |
68 |
| - jsc: { |
69 |
| - target: 'es2020', |
70 |
| - parser, |
71 |
| - transform: { |
72 |
| - useDefineForClassFields: true, |
73 |
| - react: reactConfig, |
74 |
| - }, |
75 |
| - }, |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 |
| - const shouldEs5Transform = |
80 |
| - options.es5 || (!process.env.VXRN_USE_BABEL_FOR_GENERATORS && asyncGeneratorRegex.test(code)) |
81 |
| - |
82 |
| - const opts: SWCOptions = shouldEs5Transform |
83 |
| - ? { |
| 48 | + let result: Output |
| 49 | + |
| 50 | + if (USE_OXC) { |
| 51 | + const oxc = await import('oxc-transform') |
| 52 | + result = oxc.transform(id, code, {}) |
| 53 | + } else { |
| 54 | + const reactConfig = { |
| 55 | + refresh, |
| 56 | + development: !options.forceJSX && !options.production, |
| 57 | + runtime: 'automatic', |
| 58 | + importSource: 'react', |
| 59 | + ...(configuration.enableNativewind && !id.includes('node_modules') |
| 60 | + ? { |
| 61 | + importSource: 'nativewind', |
| 62 | + // pragma: 'createInteropElement', |
| 63 | + // pragmaFrag: '_InteropFragment', |
| 64 | + // swc doesnt actually change the import right |
| 65 | + // runtime: 'classic', |
| 66 | + } |
| 67 | + : {}), |
| 68 | + } satisfies TransformConfig['react'] |
| 69 | + |
| 70 | + const transformOptions = ((): SWCOptions => { |
| 71 | + if (options.environment === 'client' || options.environment === 'ssr') { |
| 72 | + return { |
| 73 | + sourceMaps: shouldSourceMap(), |
84 | 74 | jsc: {
|
| 75 | + target: 'es2020', |
85 | 76 | parser,
|
86 |
| - target: 'es5', |
87 | 77 | transform: {
|
88 | 78 | useDefineForClassFields: true,
|
89 | 79 | react: reactConfig,
|
90 | 80 | },
|
91 | 81 | },
|
92 | 82 | }
|
93 |
| - : { |
94 |
| - ...(!options.forceJSX && { env: SWC_ENV }), |
95 |
| - jsc: { |
96 |
| - ...(options.forceJSX && { target: 'esnext' }), |
97 |
| - parser, |
98 |
| - transform: { |
99 |
| - useDefineForClassFields: true, |
100 |
| - react: reactConfig, |
| 83 | + } |
| 84 | + |
| 85 | + const shouldEs5Transform = |
| 86 | + options.es5 || |
| 87 | + (!process.env.VXRN_USE_BABEL_FOR_GENERATORS && asyncGeneratorRegex.test(code)) |
| 88 | + |
| 89 | + const opts: SWCOptions = shouldEs5Transform |
| 90 | + ? { |
| 91 | + jsc: { |
| 92 | + parser, |
| 93 | + target: 'es5', |
| 94 | + transform: { |
| 95 | + useDefineForClassFields: true, |
| 96 | + react: reactConfig, |
| 97 | + }, |
101 | 98 | },
|
102 |
| - }, |
103 |
| - } |
| 99 | + } |
| 100 | + : { |
| 101 | + ...(!options.forceJSX && { env: SWC_ENV }), |
| 102 | + jsc: { |
| 103 | + ...(options.forceJSX && { target: 'esnext' }), |
| 104 | + parser, |
| 105 | + transform: { |
| 106 | + useDefineForClassFields: true, |
| 107 | + react: reactConfig, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
104 | 111 |
|
105 |
| - return { |
106 |
| - sourceMaps: shouldSourceMap(), |
107 |
| - module: { |
108 |
| - importInterop: 'none', |
109 |
| - type: 'nodenext', |
110 |
| - }, |
111 |
| - ...(options.mode === 'serve-cjs' && { |
| 112 | + return { |
| 113 | + sourceMaps: shouldSourceMap(), |
112 | 114 | module: {
|
113 | 115 | importInterop: 'none',
|
114 |
| - type: 'commonjs', |
115 |
| - strict: true, |
| 116 | + type: 'nodenext', |
116 | 117 | },
|
117 |
| - }), |
118 |
| - ...opts, |
119 |
| - } |
120 |
| - })() |
121 |
| - |
122 |
| - const finalOptions = merge( |
123 |
| - { |
124 |
| - filename: id, |
125 |
| - swcrc: false, |
126 |
| - configFile: false, |
127 |
| - ...transformOptions, |
128 |
| - }, |
129 |
| - swcOptions || {} |
130 |
| - ) satisfies SWCOptions |
131 |
| - |
132 |
| - const result: Output = await (async () => { |
133 |
| - try { |
134 |
| - debug?.(`transformSWC ${id} using options:\n${JSON.stringify(finalOptions, null, 2)}`) |
135 |
| - |
136 |
| - return await transform(code, finalOptions) |
137 |
| - } catch (e: any) { |
138 |
| - const message: string = e.message |
139 |
| - const fileStartIndex = message.indexOf('╭─[') |
140 |
| - if (fileStartIndex !== -1) { |
141 |
| - const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/) |
142 |
| - if (match) { |
143 |
| - e.line = match[1] |
144 |
| - e.column = match[2] |
| 118 | + ...(options.mode === 'serve-cjs' && { |
| 119 | + module: { |
| 120 | + importInterop: 'none', |
| 121 | + type: 'commonjs', |
| 122 | + strict: true, |
| 123 | + }, |
| 124 | + }), |
| 125 | + ...opts, |
| 126 | + } |
| 127 | + })() |
| 128 | + |
| 129 | + const finalOptions = merge( |
| 130 | + { |
| 131 | + filename: id, |
| 132 | + swcrc: false, |
| 133 | + configFile: false, |
| 134 | + ...transformOptions, |
| 135 | + }, |
| 136 | + swcOptions || {} |
| 137 | + ) satisfies SWCOptions |
| 138 | + |
| 139 | + result = await (async () => { |
| 140 | + try { |
| 141 | + debug?.(`transformSWC ${id} using options:\n${JSON.stringify(finalOptions, null, 2)}`) |
| 142 | + |
| 143 | + return await transform(code, finalOptions) |
| 144 | + } catch (e: any) { |
| 145 | + const message: string = e.message |
| 146 | + const fileStartIndex = message.indexOf('╭─[') |
| 147 | + if (fileStartIndex !== -1) { |
| 148 | + const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/) |
| 149 | + if (match) { |
| 150 | + e.line = match[1] |
| 151 | + e.column = match[2] |
| 152 | + } |
145 | 153 | }
|
| 154 | + throw e |
146 | 155 | }
|
147 |
| - throw e |
148 |
| - } |
149 |
| - })() |
| 156 | + })() |
| 157 | + } |
150 | 158 |
|
151 | 159 | if (configuration.enableNativeCSS) {
|
152 | 160 | if (result.code.includes(`createInteropElement(`)) {
|
|
0 commit comments