storybookjs/storybook

[Bug]: esbuild doesn't treeshake imports in `main.ts` and `preview.ts` (can lead to `ModuleNotFoundError`)

Open

#21,701 创建于 2023年3月20日

在 GitHub 查看
 (2 评论) (2 反应) (0 负责人)TypeScript (89,909 star) (10,058 fork)batch import
bugcorehelp wantednextjssev:S3windows

描述

Describe the bug

If you have a barrel export file @libs/utils like this:

// Uses `path` or `fs` or `webpack` or something else that needs node's built-ins
export {someHelperForNode} from "someHelperForNode";

// Doesn't use node's build-ins
export {someHelper} from "someHelper";

And then you use it like this in .storybook/main.ts:

import {someHelperForNode} from "@libs/utils";

export default {
  // Creates config using that helper
}

And like this in .storybook/preview.ts:

import {someHelper} from "@libs/utils";

export default {
  // Creates config using that helper
}

Build will fail, and console will contain a bunch of logs with messages like:

ModuleNotFoundError: Module not found: Error: Can't resolve 'https' in 'C:\...'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "https": require.resolve("https-browserify") }'
        - install 'https-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "https": false }

It seems that some imports of node built-ins from someHelperForNode.ts file are leaking into preview.ts file even though it doesn't need them.

System

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
  Binaries:
    Node: 16.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.3.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 111.0.5563.65
    Edge: Spartan (44.19041.1266.0), Chromium (111.0.1661.44)
  npmPackages:
    @storybook/addon-essentials: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/addon-interactions: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/addon-links: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/addons: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/blocks: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/nextjs: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/react: ^7.0.0-rc.4 => 7.0.0-rc.4
    @storybook/theming: ^7.0.0-rc.4 => 7.0.0-rc.4

Additional context

No response

贡献者指南