parcel
parcel copied to clipboard
Transforming node:builtin to built-in when targeting commonjs
🙋 feature request
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")
😯 Current Behavior
const { execFileSync } = require("node:fs")
💁 Possible Solution
🔦 Context
💻 Examples
Another place where node:*
specifiers aren't handled yet:
https://github.com/parcel-bundler/parcel/blob/bb923d69f02612d65bc5f430a1cd2f7b21895252/packages/transformers/js/core/src/fs.rs#L54
Where is the code that handles this?
The resolver is in https://github.com/parcel-bundler/parcel/blob/v2/packages/utils/node-resolver-core/src/NodeResolver.js, search for node:
in that file.
But I think there's currently no way for a resolver to say "don't bundle this dependency (so isExcluded: true
), but rewrite it from node:fs
to fs
".
Which option specifies that the target is commonjs? This line should be replaced with something like:
filename = ifTargetCommonjs ? builtin.replace(/^node:/, "") : builtin;
https://github.com/parcel-bundler/parcel/blob/f2d0a3a27d6e493b23ddc2edbc8a4c0053ff34ab/packages/utils/node-resolver-core/src/NodeResolver.js?rgh-link-date=2022-05-26T07%3A12%3A32Z#L273
Found a workaround using Babel using @upleveled/babel-plugin-remove-node-prefix
. Add this to the babel config, and it should be transformed automatically:
babel.config.json
{
"plugins": ["@upleveled/babel-plugin-remove-node-prefix"],
"sourceMap": "inline"
}
Unfortunately, Parcel doesn't run babel on the dependencies. I have to use something like
cross-env NODE_ENV=production babel ./dist/*.js --out-dir ./dist --retain-lines --source-maps true --plugins @upleveled/babel-plugin-remove-node-prefix --minified
This also makes the code size bloat. Babel does more than it needs to
I noticed that this also doesn't work when targeting the browser. I expect Parcel use the web polyfil for such dependencies, but it instead errors out.