( environment: Environment, filePath: string, )
| 1110 | } |
| 1111 | |
| 1112 | export async function extractExportsData( |
| 1113 | environment: Environment, |
| 1114 | filePath: string, |
| 1115 | ): Promise<ExportsData> { |
| 1116 | await init |
| 1117 | |
| 1118 | const { optimizeDeps } = environment.config |
| 1119 | |
| 1120 | const rolldownOptions = optimizeDeps.rolldownOptions ?? {} |
| 1121 | if (optimizeDeps.extensions?.some((ext) => filePath.endsWith(ext))) { |
| 1122 | // For custom supported extensions, build the entry file to transform it into JS, |
| 1123 | // and then parse with es-module-lexer. Note that the `bundle` option is not `true`, |
| 1124 | // so only the entry file is being transformed. |
| 1125 | const { plugins: pluginsFromConfig = [], ...remainingRolldownOptions } = |
| 1126 | rolldownOptions |
| 1127 | const plugins = await asyncFlatten(arraify(pluginsFromConfig)) |
| 1128 | plugins.unshift({ |
| 1129 | name: 'externalize', |
| 1130 | resolveId(id, importer) { |
| 1131 | if (importer !== undefined) { |
| 1132 | return { id, external: true } |
| 1133 | } |
| 1134 | }, |
| 1135 | }) |
| 1136 | const build = await rolldown({ |
| 1137 | ...remainingRolldownOptions, |
| 1138 | plugins, |
| 1139 | input: [filePath], |
| 1140 | // TODO: remove this and enable rolldown's CSS support later |
| 1141 | moduleTypes: { |
| 1142 | '.css': 'js', |
| 1143 | ...remainingRolldownOptions.moduleTypes, |
| 1144 | }, |
| 1145 | }) |
| 1146 | const result = await build.generate({ |
| 1147 | ...rolldownOptions.output, |
| 1148 | format: 'esm', |
| 1149 | sourcemap: false, |
| 1150 | }) |
| 1151 | const [, exports, , hasModuleSyntax] = parse(result.output[0].code) |
| 1152 | return { |
| 1153 | hasModuleSyntax, |
| 1154 | exports: exports.map((e) => e.n), |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | let parseResult: ReturnType<typeof parse> |
| 1159 | let usedJsxLoader = false |
| 1160 | |
| 1161 | const entryContent = fs.readFileSync(filePath, 'utf-8') |
| 1162 | try { |
| 1163 | parseResult = parse(entryContent) |
| 1164 | } catch { |
| 1165 | const lang = rolldownOptions.moduleTypes?.[path.extname(filePath)] || 'jsx' |
| 1166 | debug?.( |
| 1167 | `Unable to parse: ${filePath}.\n Trying again with a ${lang} transform.`, |
| 1168 | ) |
| 1169 | if (lang !== 'jsx' && lang !== 'tsx' && lang !== 'ts') { |
no test coverage detected