evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.92k stars 1.13k forks source link

[lexical][prism] The code seems to have been incorrectly elevated while minimizing #3850

Open wszgrcy opened 1 month ago

wszgrcy commented 1 month ago

version: 0.21.5 I built it using Angular CLI Import Lexical package Everything is normal during development But there were problems with the packaging during production Finally, it was found that the position of the prism code introduced by Lexical was different from that during development During development:

image

During production: image image image

wszgrcy commented 1 month ago

demo: https://github.com/wszgrcy/angular-lexical-build-error

hyrious commented 1 month ago

This minimal case to trigger this error is when esbuild bundles @lexical/code and hits its weird dual export entry with --minify (which means defining process.env.NODE_ENV="production"):

// node_modules/@lexical/code/LexicalCode.mjs
import * as modDev from './LexicalCode.dev.mjs';
import * as modProd from './LexicalCode.prod.mjs';
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
export const $createCodeHighlightNode = mod.$createCodeHighlightNode;
// ...

So the reprodcution without angular cli could be like this:

// entry.js
import {CodeHighlightNode} '@lexical/code'
console.log(CodeHighlightNode)
$ esbuild entry.js --bundle --outdir=dist --define:process.env.NODE_ENV=\"production\"

Playground Link

I'm not quite sure what happened here, but here are some guesses:

To solve this error, at least as a workaround, is to set --conditions in esbuild options to select the prod entry file directly without touching the weird dual entry. I don't know if angular has the place to put this option but, here it is:

esbuild.build({
  conditions: ['production']
})