Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CommonJS compilation target #92

Closed
wants to merge 2 commits into from

Conversation

finkef
Copy link
Contributor

@finkef finkef commented Nov 18, 2021

This PR adds a new tsconfig.cjs.json that extends the default tsconfig, but changes compilation target to CommonJS. The CommonJS code is output to dist/cjs and thus doesn't interfere with the ESM code in dist.

For node to pick up the CJS code, I changed the main field in package.json to the CJS directory, but set both module and react-native to the ESM directory.

Note however, that this still doesn't work completely since react-native (which doesn't ship CommonJS code) is imported in ClassParser.ts and index.ts (through the useDeviceContext export). Thus, node errors when it reaches require("react-native").

In my specific use case, I want to import the create function in a Babel Plugin to statically extract and compile the Tailwind styles.To make this work, we would need to:

  1. Extract the create function from index.ts and re-export it from there (to get rid of the export { useDeviceContext } from "./hooks" in the same file)
  2. Get rid of the import { Platform as RnPlatform } from 'react-native' in ClassParser.ts. We could achieve this by passing the Platform in the constructor for example.

Then, we would be able to require("twrnc/create") from a node environment and benefit from all the magic you are doing around processing styles. 馃帀

PS: Also see #87

@jaredh159
Copy link
Owner

jaredh159 commented Nov 18, 2021

ha, looks like we submitted PR's within a few minutes of each other. can you take a look at my PR? i'm open to the file reorganization you propose... i debated injecting the platform in the ClassParser constructor -- i also debated not depending on react-native at all -- but that would mean the device context hook would involve some more boilerplate that I wanted to avoid.

maybe you could submit a PR to my cjs branch with your proposed file reorganization. also, check out my comment on the issue as well, thanks!

@finkef
Copy link
Contributor Author

finkef commented Nov 18, 2021

Sounds like a plan, closing in favor of #91.

@finkef finkef closed this Nov 18, 2021
@finkef finkef mentioned this pull request Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants