| 118 | } |
| 119 | |
| 120 | export async function transformWithOxc( |
| 121 | code: string, |
| 122 | filename: string, |
| 123 | options?: OxcTransformOptions, |
| 124 | inMap?: object, |
| 125 | config?: ResolvedConfig, |
| 126 | watcher?: FSWatcher, |
| 127 | ): Promise<Omit<OxcTransformResult, 'errors'>> { |
| 128 | let lang = options?.lang |
| 129 | if (!lang) { |
| 130 | // if the id ends with a valid ext, use it (e.g. vue blocks) |
| 131 | // otherwise, cleanup the query before checking the ext |
| 132 | const ext = path |
| 133 | .extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename)) |
| 134 | .slice(1) |
| 135 | |
| 136 | if (ext === 'cjs' || ext === 'mjs') { |
| 137 | lang = 'js' |
| 138 | } else if (ext === 'cts' || ext === 'mts') { |
| 139 | lang = 'ts' |
| 140 | } else { |
| 141 | lang = ext as 'js' | 'jsx' | 'ts' | 'tsx' |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | const resolvedOptions = { |
| 146 | sourcemap: true, |
| 147 | ...options, |
| 148 | inputMap: inMap as SourceMap | undefined, |
| 149 | lang, |
| 150 | } |
| 151 | |
| 152 | const result = transformSync( |
| 153 | filename, |
| 154 | code, |
| 155 | resolvedOptions, |
| 156 | getTSConfigResolutionCache(config), |
| 157 | ) |
| 158 | if ( |
| 159 | watcher && |
| 160 | config && |
| 161 | result.tsconfigFilePaths && |
| 162 | result.tsconfigFilePaths.length > 0 |
| 163 | ) { |
| 164 | for (const tsconfigFile of result.tsconfigFilePaths) { |
| 165 | ensureWatchedFile(watcher, normalizePath(tsconfigFile), config.root) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (result.errors.length > 0) { |
| 170 | // Copy from rolldown's packages/rolldown/src/utils/errors.ts |
| 171 | let summary = `Transform failed with ${result.errors.length} error${result.errors.length < 2 ? '' : 's'}:\n` |
| 172 | for (let i = 0; i < result.errors.length; i++) { |
| 173 | summary += '\n' |
| 174 | if (i >= 5) { |
| 175 | summary += '...' |
| 176 | break |
| 177 | } |