| 97 | } |
| 98 | |
| 99 | export async function transformWithEsbuild( |
| 100 | code: string, |
| 101 | filename: string, |
| 102 | options?: EsbuildTransformOptions, |
| 103 | inMap?: object, |
| 104 | config?: ResolvedConfig, |
| 105 | watcher?: FSWatcher, |
| 106 | /** @internal */ |
| 107 | ignoreEsbuildWarning = false, |
| 108 | ): Promise<ESBuildTransformResult> { |
| 109 | let loader = options?.loader |
| 110 | |
| 111 | if (!loader) { |
| 112 | // if the id ends with a valid ext, use it (e.g. vue blocks) |
| 113 | // otherwise, cleanup the query before checking the ext |
| 114 | const ext = path |
| 115 | .extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename)) |
| 116 | .slice(1) |
| 117 | |
| 118 | if (ext === 'cjs' || ext === 'mjs') { |
| 119 | loader = 'js' |
| 120 | } else if (ext === 'cts' || ext === 'mts') { |
| 121 | loader = 'ts' |
| 122 | } else { |
| 123 | loader = ext as EsbuildLoader |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | let tsconfigRaw = options?.tsconfigRaw |
| 128 | |
| 129 | // if options provide tsconfigRaw in string, it takes highest precedence |
| 130 | if (typeof tsconfigRaw !== 'string') { |
| 131 | // these fields would affect the compilation result |
| 132 | // https://esbuild.github.io/content-types/#tsconfig-json |
| 133 | const meaningfulFields: Array<keyof TSCompilerOptions> = [ |
| 134 | 'alwaysStrict', |
| 135 | 'experimentalDecorators', |
| 136 | 'importsNotUsedAsValues', |
| 137 | 'jsx', |
| 138 | 'jsxFactory', |
| 139 | 'jsxFragmentFactory', |
| 140 | 'jsxImportSource', |
| 141 | 'preserveValueImports', |
| 142 | 'target', |
| 143 | 'useDefineForClassFields', |
| 144 | 'verbatimModuleSyntax', |
| 145 | ] |
| 146 | const compilerOptionsForFile: TSCompilerOptions = {} |
| 147 | if (loader === 'ts' || loader === 'tsx') { |
| 148 | const result = resolveTsconfig( |
| 149 | filename, |
| 150 | getTSConfigResolutionCache(config), |
| 151 | ) |
| 152 | if (result) { |
| 153 | const { tsconfig: loadedTsconfig, tsconfigFilePaths } = result |
| 154 | // tsconfig could be out of root, make sure it is watched on dev |
| 155 | if (watcher && config) { |
| 156 | for (const tsconfigFile of tsconfigFilePaths) { |