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: Resolver control by environment variable "LLRT_PLATFORM" #690

Merged
merged 4 commits into from
Nov 18, 2024

Conversation

nabetti1720
Copy link
Contributor

@nabetti1720 nabetti1720 commented Nov 18, 2024

Issue # (if available)

#680 (comment)

Description of changes

This PR adds the ability to toggle platform preferences in the Node.js package resolver.

// index19.js
import chalk from 'chalk';
console.log(chalk.blue('Hello world!'));

Use "browser":

% export LLRT_PLATFORM=browser
% llrt index19.js
Hello world!

Use "node":

% export LLRT_PLATFORM=node
% llrt index19.js
ReferenceError: Error resolving module 'node:tty' from '/Users/shinya/Workspaces/llrt-test/node_modules/chalk/./source/vendor/supports-color/index.js'

NOTE: This behavior is currently as expected, as the tty module has not yet been merged into mainline.

Non use:

% unset LLRT_PLATFORM
% llrt index19.js
Hello world!

Checklist

  • Created unit tests in tests/unit and/or in Rust for my feature if needed
  • Ran make fix to format JS and apply Clippy auto fixes
  • Made sure my code didn't add any additional warnings: make check
  • Added relevant type info in types/ directory
  • Updated documentation if needed (API.md/README.md/Other)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Contributor

@richarddavison richarddavison left a comment

Choose a reason for hiding this comment

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

Good idea! Just a minor suggestion

llrt_core/src/module_loader/mod.rs Show resolved Hide resolved
@nabetti1720 nabetti1720 mentioned this pull request Nov 18, 2024
5 tasks
@nabetti1720
Copy link
Contributor Author

@richarddavison , We have also fixed the issue where Windows paths were not being processed correctly, so please check it out.

Copy link
Contributor

@richarddavison richarddavison left a comment

Choose a reason for hiding this comment

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

We can avoid a few unwraps. I wonder if we should do this on all paths on after calling require_resolve instead of on a per function basis. Also make that function private to make sure it always return an abs path.

llrt_core/src/module_loader/resolver.rs Outdated Show resolved Hide resolved
llrt_core/src/module_loader/resolver.rs Outdated Show resolved Hide resolved
@nabetti1720
Copy link
Contributor Author

We can avoid a few unwraps. I wonder if we should do this on all paths on after calling require_resolve instead of on a per function basis. Also make that function private to make sure it always return an abs path.

Thinking about it, in this use case, we only need to worry about the package.json resolution process, and since that function can return a Result type, we can avoid using unwrap().

@richarddavison richarddavison merged commit 2c05335 into awslabs:main Nov 18, 2024
10 of 11 checks passed
@nabetti1720 nabetti1720 deleted the feat/llrt-platform-switch branch November 18, 2024 15:20
@nabetti1720 nabetti1720 mentioned this pull request Nov 19, 2024
5 tasks
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.

2 participants