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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add esbuild's define option #9527

Closed
4 tasks done
Chen-jj opened this issue Aug 4, 2022 · 2 comments
Closed
4 tasks done

Re-add esbuild's define option #9527

Chen-jj opened this issue Aug 4, 2022 · 2 comments

Comments

@Chen-jj
Copy link
Contributor

Chen-jj commented Aug 4, 2022

Description

#8606 removed esbuild' define option during prebundle,and it brings problems at dynamic imports case:

if (FRAMEWORK === 'preact') {
  const options = require('preact').options
 // ...
}

For example, when user are using react, we define FRAMEWORK: react here.

In Vite2, vite' define options pass to esbuild' define too.esbuild will had dead code eliminated and everything ok:

// Vite2 output
if (false) {
  const options = null.options
}

However, after removing esbuild' define. Vite only define FRAMEWORK at runtime.During deps scanning stage, esbuild will try to bundle preact which the user had not installed because they are using react.So esbuild throws an error that preact not found.

// Vite3 output
if (FRAMEWORK === 'preact') {
  const options = require('preact').options
 // ...
}

Besides, Webpack's output:

if (false) {}

And maybe #5676 related.

Suggested solution

Pass Vite's define option to esbuild's define option.

Alternative

No response

Additional context

No response

Validations

@sapphi-red
Copy link
Member

For a workaround, setting optimizeDeps.esbuildOptions.define might work.

const result = await build({
absWorkingDir: process.cwd(),
entryPoints: Object.keys(flatIdDeps),
bundle: true,
// We can't use platform 'neutral', as esbuild has custom handling
// when the platform is 'node' or 'browser' that can't be emulated
// by using mainFields and conditions
platform,
define,
format: 'esm',
// See https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694
banner:
platform === 'node'
? {
js: `import { createRequire } from 'module';const require = createRequire(import.meta.url);`
}
: undefined,
target: isBuild ? config.build.target || undefined : ESBUILD_MODULES_TARGET,
external,
logLevel: 'error',
splitting: true,
sourcemap: true,
outdir: processingCacheDir,
ignoreAnnotations: !isBuild,
metafile: true,
plugins,
...esbuildOptions,
supported: {
'dynamic-import': true,
'import-meta': true,
...esbuildOptions.supported
}
})

@sapphi-red
Copy link
Member

Closing as this could be done by optimizeDeps.esbuildOptions.define and the underlaying issue is #3715

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Nov 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants