vanilla-extract icon indicating copy to clipboard operation
vanilla-extract copied to clipboard

Using with create-react-app

Open Dagur opened this issue 3 years ago • 8 comments

Hi

I just created a new create-app-app app (with typescript) and want to use vanilla-extract. There isn't much info about how to do that except https://tsh.io/blog/vanilla-extract-library/ and that doesn't work with the latest CRA.

Can you add something to the documentation on how to do this? I'm sure a lot of people would appreciate it.

Dagur avatar Dec 17 '21 20:12 Dagur

The official documentation shows how to install it with Next.js: https://vanilla-extract.style/documentation/setup/#nextjs

You can bypass the babel plugin if you want to run it with SWC. However, there is a bug somewhere in Vanilla Extract that causes a PostCSS error in Next beyond [email protected], waiting for some help to get it all cleared up!

Check here for updates on the error caused by Next: https://github.com/seek-oss/vanilla-extract/issues/522

calebpdx avatar Dec 19 '21 21:12 calebpdx

The Next.js instructions can't be used with CRA.

Anyway, since this was a new project I opted for Vite instead. That worked without issues.

Dagur avatar Dec 20 '21 09:12 Dagur

You can't use vanilla-extract with plain create-react-app, however, you can use it with Craco. It allows you to override webpack config, and vanilla-extract webpack plugin seems to run without issues there.

Dremora avatar Dec 20 '21 18:12 Dremora

I tried that first using instructions that I found on Google but there's version conflicts with the latest version of create-react-app

Dagur avatar Dec 20 '21 20:12 Dagur

If you use yarn, you no longer need craco. You can patch react-scripts config using yarn patch command.

Spaubleit avatar Apr 30 '22 06:04 Spaubleit

Not sure there's anything to resolve here outside of providing some docs on how to setup VE with create-react-app. Happy to accept a PR adding setup docs for create-react-app if someone is keen to contribute.

mattcompiles avatar May 03 '22 12:05 mattcompiles

I installed VE in a [email protected] app and configured the webpack and babel plugin with Craco. The dev server and the tests are working fine but the production build fails because VE imports a file from node_modules:

You attempted to import node_modules/@vanilla-extract/webpack-plugin/extracted.js 
which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

~~I configured a path to this folder in tsconfig using react-app-alias and it works now.~~ Edit: I registered it as an allowed path via webpack config. Is this the recommended solution or is there a better way to use VE with CRA?

~~Edit: Images don't work. The webpack config for ".vanilla.css" files mentioned in the docs doesn't work at all :/~~ Edit: Reusing the existing webpack rule but with url: false works.

rothsandro avatar Aug 31 '22 19:08 rothsandro

For anybody else who runs into the problem mentioned by @rothsandro, this blog explains how to handle it: https://sandroroth.com/blog/vanilla-extract-cra

scotthaley avatar Oct 06 '22 16:10 scotthaley

Closing as CRA is getting deprecated. Refer to the blog post if you still need it.

graup avatar Jul 17 '23 01:07 graup