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

Add support for babel transform in monorepos #8651

Closed
wants to merge 1 commit into from

Conversation

jhsware
Copy link

@jhsware jhsware commented Nov 21, 2022

If you have a monorepos with the following setup:

root/
  packages/
    app/
      package.json
      App.tsx
    component/
      package.json
      Component.tsx (requires babel transpile)
  • app depends on component
  • app and component need to be transpiled with Babeljs

You are currently limited to running Parceljs from a common parent folder with a shared Babeljs config.

However there are several cases where this might be undesirable. Especially if you have multiple application projects sharing the same development packages that are under continuous development.

This limitation doesn't exist when using Parceljs without requiring Babel transpile of development packages.

Background

I ran into this problem when using Parceljs in an Infernojs-project. When using Reactjs I could use this project structure. However, since Infernojs requires babel-plugin-inferno to create optimised transpiled code, Parceljs fails and transpiles the JSX-code of development packages to React.createElement calls.

The Problem

What happens is that Parceljs won't pass projectRoot to babel when the asset being transpiled is outside projectRoot. This causes babel to fail to pick up a babel config file and thus skipping whatever transpilation is required by that package.

The Solution

This PR checks if the asset to be transpiled is actually inside the project root. If not it checks for a package root (closest ancestor folder containing package.json) and passes this as current working directory to Babeljs. This allows babel to find the package specific babel config file and allows the asset to be transpiled properly.

Tests

I have implemented tests for .js, .jsx, .ts and .tsx based on the babel-custom integration test.

@mischnic
Copy link
Member

Babel already has a mechanism for monorepos and package-specific overrides: https://babeljs.io/docs/en/config-files#monorepos.

An example from the Parcel test cases: https://github.com/parcel-bundler/parcel/tree/v2/packages/core/integration-tests/test/integration/babel-config-monorepo

Have you tried that already?

@jhsware
Copy link
Author

jhsware commented Nov 22, 2022

@mischnic I totally missed that even though I saw the test case. I will check it out, thanks for the links!

@jhsware
Copy link
Author

jhsware commented Nov 22, 2022

@mischnic From what I can tell it is working both with development server and build. Thank you for pushing me in the right direction! I tried a similar setup earlier but failed. I may have had caching issues or just bad luck... for what it is worth, it was interesting poking around in the project :)

@jhsware jhsware closed this Nov 22, 2022
@jhsware jhsware deleted the fix-babel-monorepos branch November 22, 2022 19:35
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