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

Feat: Automatically Discover StyleX Aliases from Configuration Files #810

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

Conversation

p0nch000
Copy link
Contributor

@p0nch000 p0nch000 commented Dec 16, 2024

[Feature] Automatically Discover StyleX Aliases from Configuration Files

Linked Issues

Fixes #765


Overview

This PR enhances the StyleX Babel plugin to automatically discover alias configurations from Node.js native imports (package.json), tsconfig.json, and deno.json. It ensures normalized paths for cross-platform compatibility and maintains backward compatibility with existing manual configurations.


Implementation Details

  • JSON5 Parser: Handles configuration files with comments or trailing commas.
  • Alias Discovery:
    • Parses package.json for Node.js native subpath imports (aliases with # prefix).
    • Parses tsconfig.json for compilerOptions.paths and resolves paths relative to baseUrl.
    • Parses deno.json for the imports field.
  • Merge Configurations:
    • Combines configurations from all sources:
      Manual > package.json > tsconfig.json > deno.json.
  • Code Reuse:
    • Replaced redundant findProjectRoot with getPackageNameAndPath for project root discovery.
  • Normalized Paths:
    • Standardizes path separators for cross-platform compatibility.
  • Error Handling:
    • Gracefully handles missing or malformed configuration files.

Test Coverage

The following scenarios are tested:

1. Alias Discovery:

  • Node.js native imports from package.json (subpath imports).
  • Aliases from tsconfig.json paths.
  • Aliases from deno.json imports.

2. Configuration Merging:

  • Merges aliases from all sources with correct priority.

3. Manual Overrides:

  • Ensures manual alias configurations take precedence over discovered aliases.

4. Error Handling:

  • Handles missing or invalid configuration files gracefully.

Code Changes

1. state-manager.js (Alias Discovery)

  • Updated Functionality:
    • loadAliases:
      • Now supports Node.js native imports and deno.json.
      • Removed custom stylex.aliases support.
      • Simplified project root discovery using getPackageNameAndPath.
  • Added support for deno.json imports.

2. stylex-transform-alias-config-test.js

  • Updated Unit Tests:
    • Tests for Node.js native imports discovery.
    • Tests for deno.json imports.
    • Tests for merged configuration from all sources.
    • Tests for proper handling of invalid configurations.

Potential Future Improvements

  1. Enhanced Test Cases:

    • Complex alias patterns with wildcards.
    • Nested directory structures.
    • Edge cases in path resolution.
  2. Additional Features:

    • Support for other configuration sources like Webpack config.
    • More granular control over alias resolution priorities.
  3. Performance Optimizations:

    • Caching of discovered configurations.
    • Smarter file system traversal.

Pre-flight Checklist

  • I have read the Contribution Guidelines.
  • Performed a self-review of my code.
  • Added tests for the new functionality.
  • Verified cross-platform compatibility.
  • Ensured backward compatibility with existing configurations.

Feedback

Feedback is welcome, particularly for:

  • Additional test cases to consider.
  • Improvements to performance or alias resolution edge cases.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 16, 2024
@p0nch000
Copy link
Contributor Author

Hey @nmn, this is my initial implementation for the issue. Let me know if there are any additional test cases or improvements you'd like me to include. I’m happy to iterate further based on your feedback

@p0nch000 p0nch000 changed the title Feat/auto aliases Feat: Automatically Discover StyleX Aliases from Configuration Files Dec 16, 2024
@nmn
Copy link
Contributor

nmn commented Dec 17, 2024

Parses package.json for stylex.aliases.

We don't want to add a new stylex.aliases option to the package.json. Instead, please look for the native aliases configuration supported by Node.js:

Copy link
Contributor

@nmn nmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! Please fix the failing tests and address the comments.

packages/babel-plugin/src/utils/state-manager.js Outdated Show resolved Hide resolved
packages/babel-plugin/src/utils/state-manager.js Outdated Show resolved Hide resolved
@p0nch000
Copy link
Contributor Author

Hey @nmn fixed all the changes you requested, i just couldnt pass the size test but apart from that i think its all ready, also updated the PR description and the unit tests, let me know if more changes are needed :)

Copy link
Contributor

@nmn nmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly there. I'll do some more testing before I merge, but I don't see any issues in the code review.

package.json Outdated Show resolved Hide resolved
Copy link
Contributor

@nmn nmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good in general. I will test a bit more before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[compiler] Automatically pick up alias configuration from package.json or tsconfig
3 participants