modernweb-dev / web

Guides, tools and libraries for modern web development.
https://modern-web.dev
MIT License
2.16k stars 271 forks source link

fix(storybook-builder): simplify and speed up the CommonJS to ESM transformation #2692

Closed bashmish closed 3 months ago

bashmish commented 3 months ago

What I did

  1. simplify and speed up the CommonJS to ESM transformation
  2. make React conditional reexports work in production
  3. remove @chialab/esbuild-plugin-commonjs and use low-level cjs-module-lexer

2nd caused a bug when I started working on MDX and created a static build of the storybook, which made React break with missing reexports like import { createContext } from 'react'; this was due to a lack of support in @chialab/esbuild-plugin-commonjs for conditional CommonJS reexports, specifically in this file https://www.runpkg.com/?react@18.2.0/index.js

// react@18.2.0/index.js
'use strict';

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./cjs/react.production.min.js');
} else {
  module.exports = require('./cjs/react.development.js');
}

after hours of debugging, seeing the output of @chialab/esbuild-plugin-commonjs and trying to make a patch for it, I gave up an decided to try to make smth similar myself with the help of low-level tools, where https://www.npmjs.com/package/cjs-module-lexer seems to be the most mature and was used by @chialab/esbuild-plugin-commonjs under-the-hood

I think the end result is better than the previous in all aspects (simplicity, speed and the readability of the output) and it solves the bug of MDX which I'm working on in another branch

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: c98a27982822ab20517c6ecc82a8ebfcd7b70414

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------------------- | ----- | | @web/storybook-builder | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR