storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
83.95k stars 9.21k forks source link

[Bug]: `babelModeV7` dose not refer to the root `.babelrc` #20527

Closed kodai3 closed 1 year ago

kodai3 commented 1 year ago

Describe the bug

I have nextjs app trying to migrate storybook v6 to v7.

I have .babelrc for nextjs and want babelModeV7 to use this file to compile.

.babelrc ``` { "presets": [ // refs: https://nextjs.org/docs/advanced-features/customizing-babel-config [ "next/babel", { "preset-react": { "runtime": "automatic", "importSource": "@emotion/react" } } ] ], "plugins": ["@emotion/babel-plugin", "babel-plugin-twin", "babel-plugin-macros"] } ```

but, storybook fails not being able to compile macros. If I look at the debug command BABEL_SHOW_CONFIG_FOR=.storybook/preview.js npm run storybook, we can see the root .babelrc is not respected. It happens both at v7 and v6 with flags on.

log from debug command ``` { "assumptions": {}, "filename": "//.storybook/preview.js", "targets": {}, "cloneInputAst": true, "babelrc": false, "configFile": false, "browserslistConfigFile": false, "passPerPreset": false, "envName": "development", "cwd": "/", "root": "/", "rootMode": "root", "plugins": [ "//node_modules/babel-plugin-add-react-displayname/index.js", "//node_modules/babel-plugin-react-docgen/lib/index.js", "[Function: function TransformFontImports({ types }) {\n retur ... ]" ], "presets": [ "next/babel" ], "sourceMaps": true, "sourceFileName": "//.storybook/preview.js", "caller": { "name": "babel-loader", "target": "web", "supportsStaticESM": true, "supportsDynamicImport": true, "supportsTopLevelAwait": true } } -----End Babel configs----- ```

To Reproduce

I am trying to make repro repo. bare minimum repo with Next, Emotion and twin macro works fine.

System

System:
    OS: macOS 12.6
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.12.1/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
  Browsers:
    Chrome: 108.0.5359.124
    Firefox: 102.0
    Safari: 16.0
  npmPackages:
    @storybook/addon-a11y: 7.0.0-beta.19 => 7.0.0-beta.19
    @storybook/addon-essentials: 7.0.0-beta.19 => 7.0.0-beta.19
    @storybook/addon-interactions: 7.0.0-beta.19 => 7.0.0-beta.19
    @storybook/addon-links: 7.0.0-beta.19 => 7.0.0-beta.19
    @storybook/addon-postcss: 2.0.0 => 2.0.0
    @storybook/jest: 0.0.11-next.0 => 0.0.11-next.0
    @storybook/nextjs: 7.0.0-beta.19 => 7.0.0-beta.19
    @storybook/react: 7.0.0-beta.19 => 7.0.0-beta.19
    @storybook/testing-library: 0.0.14-next.1 => 0.0.14-next.1
    @storybook/testing-react: 2.0.0-next.0 => 2.0.0-next.0

Additional context

Tried renaming .babelrc to .babelrc.json and nothing changed.

When I write the babel config on the main.js, the debug log shows as epected and works fine.

main.js ``` module.exports = { babel: () => { return { presets: [ // refs: https://nextjs.org/docs/advanced-features/customizing-babel-config [ 'next/babel', { 'preset-react': { runtime: 'automatic', importSource: '@emotion/react', }, }, ], ], plugins: ['@emotion/babel-plugin', 'babel-plugin-twin', 'babel-plugin-macros'], }; }, } ```
log ``` { "assumptions": {}, "filename": "//.storybook/preview.js", "sourceMaps": true, "sourceFileName": "//.storybook/preview.js", "caller": { "name": "babel-loader", "target": "web", "supportsStaticESM": true, "supportsDynamicImport": true, "supportsTopLevelAwait": true }, "targets": {}, "cloneInputAst": true, "babelrc": false, "configFile": false, "browserslistConfigFile": false, "passPerPreset": false, "envName": "development", "cwd": "/", "root": "/", "rootMode": "root", "plugins": [ "@emotion/babel-plugin", "babel-plugin-twin", "babel-plugin-macros" ], "presets": [ [ "next/babel", { "preset-react": { "runtime": "automatic", "importSource": "@emotion/react" } } ] ] } -----End Babel configs----- ```
ndelangen commented 1 year ago

@kodai3 in babelModeV7 we just let the babel-loader use the default heuristic for figuring our what the config is, including where the load config from.

I think this page might hold some answers for you: https://babeljs.io/docs/en/config-files