parcel icon indicating copy to clipboard operation
parcel copied to clipboard

fix: transforming node:builtin when targeting commonjs

Open aminya opened this issue 3 years ago • 4 comments

↪️ Pull Request

Fixes #8049

Currently, Parceljs silently uses node:builtin inside commonjs although this doesn't work. There should be a transformer that converts this to normal nodejs imports when targeting commonjs.

import { execFileSync } from "node:fs"

🤔 Expected Behavior

const { execFileSync } = require("fs")

😯 Previous Behavior

const { execFileSync } = require("node:fs")

💻 Examples

🚨 Test instructions

✔️ PR Todo

  • [ ] Added/updated unit tests for this change
  • [ ] Filled out test instructions (In case there aren't any unit tests)
  • [x] Included links to related issues/PRs

aminya avatar May 26 '22 07:05 aminya

Can you please add a test?

devongovett avatar May 28 '22 01:05 devongovett

I did, but I realized that this doesn't fix the issue fully. I added an alias property. The architecture of the code does not allow to do alias transformation inside the resolving code. It also seems that AssertGroup doesn't accept something like an alias.

aminya avatar May 28 '22 09:05 aminya

One cruel workaround is to create simple built-in Node packages that export the module, and then use them for resolving node: when the output is commonjs:

For example,

node:fs can be resolved to this package:

@parcel/node-builtin-fs

exports = require("fs")

aminya avatar May 28 '22 09:05 aminya

@devongovett do you want to go with the workaround route, or do you want to change the API of the resolver?

aminya avatar May 30 '22 19:05 aminya