vanilla-extract-css / vanilla-extract

Zero-runtime Stylesheets-in-TypeScript
https://vanilla-extract.style
MIT License
9.61k stars 293 forks source link

[Reproducible] Storybook with vanilla-extract doesn't work inside monorepo (npm + turbo repo) #1037

Closed gabrieloureiro closed 1 year ago

gabrieloureiro commented 1 year ago

Describe the bug

I've tried to run the storybook using vite, webpack4 and webpack5., but all of them are returning error.

I'm using npm and turbo repo to manage the monorepo.

My objective is create a design-system using vanilla-extract.

Screen Shot 2023-03-10 at 12 34 18

Screen Shot 2023-03-10 at 12 36 20

To reproduce access the below link and run the commands from README.md

Reproduction

https://github.com/gabrieloureiro/monorepo-storybook-vanilla-extract

System Info

System:
    OS: macOS 12.5
    CPU: (8) arm64 Apple M2
    Memory: 77.27 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.npm-global/bin/yarn
    npm: 8.19.2 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 110.1.48.171
    Safari: 15.6


### Used Package Manager

npm

### Logs

❯ npm run dev                                                                                                

> dev
> turbo run dev --parallel

• Packages in scope: @loureiro/docs, @loureiro/eslint-config, @loureiro/react, @loureiro/ts-config, @loureiro/web
• Running dev in 5 packages
• Remote caching disabled
@loureiro/docs:dev: cache bypass, force executing 2b8547223a1f554c
@loureiro/web:dev: cache bypass, force executing 162ba07f4a4d8627
@loureiro/react:dev: cache bypass, force executing eb164199a886e8f7
@loureiro/docs:dev: 
@loureiro/docs:dev: > @loureiro/docs@1.0.0 dev
@loureiro/docs:dev: > start-storybook -p 6006
@loureiro/docs:dev: 
@loureiro/react:dev: 
@loureiro/react:dev: > @loureiro/react@0.0.1 dev
@loureiro/react:dev: > tsup src/index.ts --format esm,cjs --dts --external react --watch
@loureiro/react:dev: 
@loureiro/web:dev: 
@loureiro/web:dev: > @loureiro/web@0.0.1 dev
@loureiro/web:dev: > tsup src/index.ts --format esm,cjs --dts --watch
@loureiro/web:dev: 
@loureiro/react:dev: CLI Building entry: src/index.ts
@loureiro/web:dev: CLI Building entry: src/index.ts
@loureiro/react:dev: CLI Using tsconfig: tsconfig.json
@loureiro/react:dev: CLI tsup v6.6.3
@loureiro/react:dev: CLI Running in watch mode
@loureiro/web:dev: CLI Using tsconfig: tsconfig.json
@loureiro/web:dev: CLI tsup v6.6.3
@loureiro/web:dev: CLI Running in watch mode
@loureiro/web:dev: CLI Target: node14
@loureiro/react:dev: CLI Target: es6
@loureiro/react:dev: ESM Build start
@loureiro/react:dev: CJS Build start
@loureiro/web:dev: ESM Build start
@loureiro/web:dev: CJS Build start
@loureiro/web:dev: CJS dist/index.js 8.08 KB
@loureiro/web:dev: CJS ⚡️ Build success in 66ms
@loureiro/web:dev: ESM dist/index.mjs 6.09 KB
@loureiro/web:dev: ESM ⚡️ Build success in 66ms
@loureiro/web:dev: CLI Watching for changes in "."
@loureiro/web:dev: CLI Ignoring changes in "**/{.git,node_modules}/**" | "dist"
@loureiro/react:dev: ESM dist/index.mjs 403.43 KB
@loureiro/react:dev: ESM ⚡️ Build success in 120ms
@loureiro/react:dev: CJS dist/index.js 403.60 KB
@loureiro/react:dev: CJS ⚡️ Build success in 123ms
@loureiro/react:dev: CLI Watching for changes in "."
@loureiro/react:dev: CLI Ignoring changes in "**/{.git,node_modules}/**" | "dist"
@loureiro/react:dev: DTS Build start
@loureiro/web:dev: DTS Build start
@loureiro/docs:dev: info @storybook/react v6.5.16
@loureiro/docs:dev: info 
@loureiro/docs:dev: info => Loading presets
@loureiro/web:dev: DTS ⚡️ Build success in 1425ms
@loureiro/react:dev: DTS ⚡️ Build success in 2080ms
@loureiro/docs:dev: info => Loading custom manager config
@loureiro/docs:dev: info => Loading custom manager config
@loureiro/docs:dev: 
@loureiro/docs:dev: info => Using cached manager
@loureiro/docs:dev: ╭────────────────────────────────────────────────────╮
@loureiro/docs:dev: │                                                    │
@loureiro/docs:dev: │   Storybook 6.5.16 for React started               │
@loureiro/docs:dev: │   1.12 s for preview                               │
@loureiro/docs:dev: │                                                    │
@loureiro/docs:dev: │    Local:            http://localhost:6006/        │
@loureiro/docs:dev: │    On your network:  http://192.168.15.10:6006/    │
@loureiro/docs:dev: │                                                    │
@loureiro/docs:dev: ╰────────────────────────────────────────────────────╯

### Validations

- [X] Check that there isn't [already an issue](https://github.com/vanilla-extract-css/vanilla-extract/issues) that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
AngeloCastro9 commented 1 year ago

I have the same issue

lucastorress commented 1 year ago

I have the same issue. 😢

askoufis commented 1 year ago

I think your issue is that your react and web packages are not being bundled correctly. They're using tsup, which uses esbuild for bundling packages, so you'll need to use the vanilla extract esbuild plugin in order to properly extract the generated CSS for the bundle.

I added a tsup.config.ts to both your web and react packages in your reproduction and storybook started working correctly.

// tsup.config.ts
import { vanillaExtractPlugin } from '@vanilla-extract/esbuild-plugin'
import { defineConfig } from 'tsup'

export default defineConfig({
  esbuildPlugins: [vanillaExtractPlugin()],
})
gabrieloureiro commented 1 year ago

You are a genius man @askoufis. Thank you so much, now i can work at Design system.

gabrieloureiro commented 1 year ago

The problem now is that when I change the style, it doesn't change anything =/

gabrieloureiro commented 1 year ago

Nothing from this file (Text.css.ts) are changing the styles.

code

I've tried reproduce this outside from monorepo and its work:

Screen Shot 2023-03-11 at 08 44 58
askoufis commented 1 year ago

The built packages contain separate js and css files, but the js files do not import the css files, so when storybook imports your component, it doesn't load the CSS.

I'm guessing it works outside your monorepo because your story is using the source code of the component, rather than the compiled package code.

Some options to get this to work depend on how you want to distribute your package. You could distribute your package as plain typescript (i.e. don't use tsup), so consumers would end up compiling it on their end.

You could also just use tsc to compile your files to javascript (and minify them with something else if you want). Haven't tried this approach myself.

Getting the tsup/esbuild to work is a bit of effort, there's an old discord message about it.

Alternatively, another bundler like rollup might do a better job of this. Here's some examples of this being done:

If you need more help on this, feel free to ask in the discord. There's lots of helpful people there.

Going to close this issue as it's not actually an issue with the library itself.

askoufis commented 1 year ago

If you don't mind having to import a separate CSS file, you can make it an entrypoint of your package, like this person did, so you don't have to import from your-package/dist, and instead import from your-package/css or something.

gabrieloureiro commented 1 year ago

Hi @askoufis.

My code is finally working, you can see here.

First add this properties in your package.json of project that use tsup to compile the sprinkles, css, tokens etc.

"exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "require": "./dist/index.js",
      "main": "./dist/index.mjs",
      "module": "./dist/index.mjs",
      "import": "./dist/index.mjs"
    },
    "./*": {
      "types": "./dist/*.d.ts",
      "require": "./dist/*.js",
      "main": "./dist/*.js",
      "module": "./dist/*.mjs",
      "import": "./dist/*.mjs"
    },
    "./dist/index.css": {
      "import": "./dist/index.css",
      "require": "./dist/index.css"
    }

Storybook:

Basically I've create a alias on resolve property inside my .storybook/main.js

 resolve: {
        alias: [
          {
            find: '@loureiro/react',
            replacement: path.resolve(__dirname, '../../react'),
          },
          {
            find: '@loureiro/css',
            replacement: path.resolve(__dirname, '../../web/dist/index.css'),
          },
          {
            find: '@loureiro/web',
            replacement: path.resolve(__dirname, '../../web'),
          },
        ],
      }

So in the each file with stories that use vanilla-exract, you must import you new alias (from previous step) that contains the css . Example: import '@loureiro/css'