Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove unused exports data props #12740

Merged
merged 1 commit into from Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 3 additions & 20 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -43,16 +43,11 @@ const isDebugEnabled = _debug('vite:deps').enabled

const jsExtensionRE = /\.js$/i
const jsMapExtensionRE = /\.js\.map$/i
const reExportRE = /export\s+\*\s+from/

export type ExportsData = {
hasImports: boolean
// exported names (for `export { a as b }`, `b` is exported name)
exports: readonly string[]
facade: boolean
// es-module-lexer has a facade detection but isn't always accurate for our
// use case when the module has default export
hasReExports?: boolean
// hint if the dep requires loading as jsx
jsxLoader?: boolean
}
Expand Down Expand Up @@ -731,7 +726,7 @@ async function prepareEsbuildOptimizerRun(
const src = depsInfo[id].src!
const exportsData = await (depsInfo[id].exportsData ??
extractExportsData(src, config, ssr))
if (exportsData.jsxLoader) {
if (exportsData.jsxLoader && !esbuildOptions.loader?.['.js']) {
// Ensure that optimization won't fail by defaulting '.js' to the JSX parser.
// This is useful for packages such as Gatsby.
esbuildOptions.loader = {
Expand Down Expand Up @@ -1152,11 +1147,10 @@ export async function extractExportsData(
write: false,
format: 'esm',
})
const [imports, exports, facade] = parse(result.outputFiles[0].text)
const [imports, exports] = parse(result.outputFiles[0].text)
return {
hasImports: imports.length > 0,
exports: exports.map((e) => e.n),
facade,
}
}

Expand All @@ -1174,25 +1168,14 @@ export async function extractExportsData(
const transformed = await transformWithEsbuild(entryContent, filePath, {
loader,
})
// Ensure that optimization won't fail by defaulting '.js' to the JSX parser.
// This is useful for packages such as Gatsby.
esbuildOptions.loader = {
'.js': 'jsx',
...esbuildOptions.loader,
}
Comment on lines -1177 to -1182
Copy link
Member Author

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 it's intentional, but this code mutates the global esbuildOptions config when using this function and hitting this code path. I've removed it as it should already be handled at

if (exportsData.jsxLoader && !esbuildOptions.loader?.['.js']) {
// Ensure that optimization won't fail by defaulting '.js' to the JSX parser.
// This is useful for packages such as Gatsby.
esbuildOptions.loader = {
'.js': 'jsx',
...esbuildOptions.loader,
}
}

parseResult = parse(transformed.code)
usedJsxLoader = true
}

const [imports, exports, facade] = parseResult
const [imports, exports] = parseResult
const exportsData: ExportsData = {
hasImports: imports.length > 0,
exports: exports.map((e) => e.n),
facade,
hasReExports: imports.some(({ ss, se }) => {
const exp = entryContent.slice(ss, se)
return reExportRE.test(exp)
}),
jsxLoader: usedJsxLoader,
}
return exportsData
Expand Down