twin.examples icon indicating copy to clipboard operation
twin.examples copied to clipboard

Add further component library examples

Open JPrisk opened this issue 3 years ago • 6 comments

nwb & neutrinojs are two popular tools used by component library authors to create and publish react components for distribution.

I experimented with adding twin.macro but received the following errors:

in ./node_modules/path-type/index.js
Module not found: Error: Can't resolve 'fs' in '.../node_modules/path-type'

in ./node_modules/resolve-from/index.js
Module not found: Error: Can't resolve 'fs' in '.../node_modules/resolve-from

in ./node_modules/resolve/lib/async.js
Module not found: Error: Can't resolve 'fs' in '.../node_modules/resolve/lib'

in ./node_modules/resolve/lib/sync.js
Module not found: Error: Can't resolve 'fs' in '.../node_modules/resolve/lib'

in ./node_modules/twin.macro/macro.js
Module not found: Error: Can't resolve 'fs' in '.../node_modules/twin.macro'

in ./node_modules/twin.macro/node_modules/cosmiconfig/dist/readFile.js
Module not found: Error: Can't resolve 'fs' in '.../node_modules/twin.macro/node_modules/cosmiconfig/dist'

in ./node_modules/resolve-from/index.js
Module not found: Error: Can't resolve 'module' in '.../node_modules/resolve-from'

would love to help out but my knowledge is limited in this space

JPrisk avatar Dec 23 '20 22:12 JPrisk

I haven't used those frameworks before but I've fixed the same error in Next.js (fs isn't available in the client bundle). You may also need the same fix for module by the look of the last error.

I'm expanding the examples so perhaps I'll add neutrino to the list (not sure about nwb yet, it doesn't have much activity anymore).

Also, I've got an existing component example using Rollup using ES Modules as the output - check it out for some inspiration.

ben-rogerson avatar Dec 23 '20 23:12 ben-rogerson

Ah, thanks @ben-rogerson - had I seen the Rollup example it would have saved me a bit of time. I ended up using create-react-library which was compatible with the module structure that you are using with twin

JPrisk avatar Dec 24 '20 02:12 JPrisk

that's probably the better option anyway, more features :)

ben-rogerson avatar Dec 24 '20 03:12 ben-rogerson

I'm utterly stumped on trying get twin.macro + emotion + nx-workspaces to play nice with each other.

I've tried following the advice of @ben-rogerson to no avail:

The trick is to use Babel to compile typescript (rather than rollup-plugin-typescript and rely on typescript for type checking and generation of declaration files.

Here's my checklist

I seem to get the same error:

MacroError: The macro you imported from "undefined" is being executed outside the context of compilation with babel-plugin-macros.

Ideally, I'd like to contribute an nx workspace example to this repo for future reference so what's the best way to start?

  • discuss more?
  • Add a (broken) example as a PR and iterate there?

I'm in Twin's Discord too if that's easier: mmoli#0581

mmmoli avatar Feb 16 '21 02:02 mmmoli

Figured it out by following the advice here: https://github.com/kentcdodds/babel-plugin-macros/issues/135#issuecomment-761079720

Would you like me to add an example as I mentioned? twin.macro is super awesome – we should make it as useful to as many people as possible, no?

mmmoli avatar Feb 16 '21 14:02 mmmoli

@mmmoli Great to hear you got around that error! I'd really like your idea of adding a nx-workspace example!

Would you be happy to add a PR for twin.examples/nx-workspace-emotion with minimal code then I can finish it off with a readme?

ben-rogerson avatar Feb 16 '21 23:02 ben-rogerson