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

fix: loading esm-register via full path from a different cwd #805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ChALkeR
Copy link

@ChALkeR ChALkeR commented Jul 7, 2024

Assuming you want to resolve to adjacent esm/esm.mjs from the package exports

Using --import /home/whatever/something/node_modules/@swc-node/register/esm/esm-register.mjs now should work as well

Which is needed e.g. for global tools that operate in a different cwd but want to spawn node with this argument

Also, simpler code

Docs: https://nodejs.org/docs/latest/api/esm.html#esm_import_meta_url

Fixes: #748 (comment)

@CLAassistant
Copy link

CLAassistant commented Jul 7, 2024

CLA assistant check
All committers have signed the CLA.

@yeliex
Copy link
Contributor

yeliex commented Jul 7, 2024

cannot understand the use case. actually, using import loader with package name should be enough.

how the global tools be invoked or call the register?

@Brooooooklyn Brooooooklyn force-pushed the master branch 2 times, most recently from 6736910 to ba15455 Compare July 12, 2024 15:59
@simonbuchan
Copy link
Contributor

simonbuchan commented Jul 29, 2024

A trivial example is what I just ran into, running swc-node on uncompiled ts scripts with their own dependencies from the project root, without duplicating a whole bunch of config:

// package.json
{
  "name": "foo-workspace",
   // ...
  "type": "module",
  "scripts": {
    "serve": "script serve",
    "build": "script build",
    "deploy-test": "script deploy --env test",
    // ...
  },
  "dependencies": {
    "foo-scripts": "workspace:^"
  }
}

// scripts/package.json
{
  "name": "foo-scripts",
  // ...
  "type": "module",
  "bin": { "script": "bin.js" },
  "dependencies": {
    "@swc-node/register": "^1.10.9",
    "typescript": "~5.5.4",
    // script-specific dependencies...
  }
}

// scripts/bin.js
import { execFileSync } from "node:child_process";
const args = process.argv.slice();
const execPath = args.shift();
args.splice(0, 1, "--enable-source-maps", "--import", "@swc-node/register/esm-register");
execFileSync(execPath, args, { stdio: "inherit" });

The workaround is to instead use a register script to the scripts package that replicates @swc-node/register/esm-register:

// scripts/register.js
import { register } from "node:module";

register("@swc-node/register/esm", import.meta.url);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants