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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expo 52 + Full typescript #207

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Expo 52 + Full typescript #207

wants to merge 4 commits into from

Conversation

natew
Copy link
Collaborator

@natew natew commented Oct 25, 2024

Ok so went into this a bit, Expo is just publishing typescript directly in expo-modules-core, and not using type import/exports, so you can't just strip types. Rollup doesn't allow importing things that don't exist. One potential workaround would be to have some sort of custom thing that turns type exports into empty objects, but idk that seems hacky even if it would be significantly faster. Since this only will happen by people who are publishing typescript directly, its rare so we can probably do the "right" thing and use the official rollup typescript plugin.

I added the rollup typescript plugin as a test. It didn't work off the top so I added logs:

CleanShot 2024-10-25 at 10 49 52@2x

It does properly hit loading and gogog, but in final what happens is:

  • It assumes your tsconfig from the root of the project is what matters so it loads that tsconfig
  • It doesn't think that typescript file is relevant

So what we'll need to do is probably fork (eventually contribute back potentially) to enable supporting tsconfig.json that is inside a node_module. I think it shouldn't be too hard - basically just need to refactor the logic around loading the tsconfig so that:

  1. If we detect node_module in path, we setup a clean namespace for that
  2. If tsconfig in that node_module and its a ts file, we load that tsconfig
  3. Use that custom ts program to parse the files there

Copy link

railway-app bot commented Oct 25, 2024

🚅 Deployed to the one-pr-207 environment in onestack.dev

Service Status Web Updated (UTC)
one ✅ Success (View Logs) Web Oct 25, 2024 at 2:18 am

@natew
Copy link
Collaborator Author

natew commented Oct 25, 2024

Ok I have cooked - this plugin works:

      {
        name: 'one-node-module-typescript',

        transform: {
          order: 'pre',
          async handler(code: string, id: string) {
            if (!/\.tsx?$/.test(id)) return
            if (!id.includes('node_modules')) {
              return
            }

            // we need to keep fake objects for type exports
            const typeExportsMatch = code.match(/^\s*export\s+type\s+([^\s]+)/gi)

            let output =
              (
                await swcTransform(id, code, {
                  mode: mode === 'dev' ? 'serve' : 'build',
                })
              )?.code || ''

            // add back in export types as fake objects:

            if (typeExportsMatch) {
              for (const typeExport of Array.from(typeExportsMatch)) {
                const fakeExport = `${typeExport.replace(' type ', ' const ')} = {};`
                output += `\n${fakeExport}\n`
              }
            }

            return output
          },
        },
      },

For now at least, just use SWC, but add back in type exports as const exports. Hacky yes but should work for 99% of use cases for now and we can explore a robust solution in the meantime.

@natew
Copy link
Collaborator Author

natew commented Oct 25, 2024

Need to add interface as well probably. I see some errors to get running still
CleanShot 2024-10-25 at 11 14 11@2x

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.

1 participant