You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@esbuild-plugins/node-resolve - by default follow NodeJS module resolution if basedir is not passed, and start looking for node_modules from the caller's module
varbasedir=opts.basedir||path.dirname(caller());
basedir is default to the directory of the caller module when not provided
mdx-bundler should follow NodeJS module resolution, but it does not.
Consequently, as we migrate our bundler from webpack/docz to mdx-bundler, we observe many of the packages failed to run due to incorrect module resolution
Suggested solution:
For the scope of my project, the following workarounds works for me:
removing @esbuild-plugins/node-resolve, let esbuild perform the resolution
(mdx-bundler cannot load md file if plugin is removed. mdx loads fine)
passing our own @esbuild-plugins/node-resolve without the basedir option
constresult=awaitbundleMdx({file: "c:/project/document/document.mdx",cwd: "c:/project",esbuildOptions: (options)=>{constnodeResolvePluginIndex=options.plugins.findIndex(({ name })=>name==="node-resolve");if(nodeResolvePluginIndex>=0){// replace original plugin with our own without `resolveOptions.basedir` configuredoptions.plugins.splice(nodeResolvePluginIndex,1,NodeResolvePlugin({extensions: [".ts",".tsx",".js",".jsx"],});}returnoptions;},});
Earlier, I wanted to suggest removing basedir.
However basedir appear to be added intentionally in #68. I have yet to look into what problem #68 is trying to solve
The text was updated successfully, but these errors were encountered:
amoshydra
added a commit
to amoshydra/mdx-bundler
that referenced
this issue
Sep 1, 2024
mdx-bundler
version: 10.0.3node
version: 18.8.0, 18.14.1, 20.12.1npm
version: 8.18.0, 9.5.0, 10.5.0esbuild
version: 0.19.12, 0.23.1@esbuild-plugins/node-resolve
version: 0.2.2Relevant code or config
//
c:/project/index-c.mdx
I have identified that usage of
basedir
in NodeResolvePlugin causes the incorrect module resolution to occur for nestednode_modules
.mdx-bundler/src/index.js
Lines 147 to 150 in 616c5b0
https://github.com/kentcdodds/mdx-bundler/pull/68/files#diff-bfe9874d239014961b1ae4e89875a6155667db834a410aaaa2ebe3cf89820556R149
What you did:
Attempt to bundle a component using package (package-a) with 2 different versions, imagine the
node_modules
structure like this* exact version doesn't matter here, only the folder structure matter
index-b
importspackage-b
importspackage-a
package-a
should resolve tonode_modules/package-a/a.js
index-c
importspackage-c
importspackage-a
package-a
should resolve tonode_modules/package-c/node_modules/package-a/a.js
What happened:
The incorrect module is resolved.
mdx-bundler
always resolve module at${process.cwd()}/node_modules
, breaking NodeJS's / ESBuild node module resolution logic.index-b
importspackage-b
importspackage-a
package-a
resolved tonode_modules/package-a/a.js
index-c
importspackage-c
importspackage-a
package-a
resolved tonode_modules/package-a/a.js
<-- ❌ incorrect resolutionReference:
esbuild
- implements NodeJS module resolution@esbuild-plugins/node-resolve
- by default follow NodeJS module resolution ifbasedir
is not passed, and start looking fornode_modules
from the caller's moduleReproduction repository:
A basic reproduce repo is created to demonstrate this point using only
esbuild
and@esbuild-plugins/node-resolve
https://github.com/amoshydra/repro-kentcdodds-mdx-bundler/tree/repro-issue-233
Test setup
build.mjs contains 3 setup:
esbuild
without pluginesbuild
with@esbuild-plugins/node-resolve
using default optionesbuild
with@esbuild-plugins/node-resolve
passing project's working directory as basedirObservation
dist
folder contain the output for each buildProblem description:
mdx-bundler
should follow NodeJS module resolution, but it does not.Consequently, as we migrate our bundler from
webpack/docz
tomdx-bundler
, we observe many of the packages failed to run due to incorrect module resolutionSuggested solution:
For the scope of my project, the following workarounds works for me:
removing@esbuild-plugins/node-resolve
, let esbuild perform the resolution(mdx-bundler cannot load
md
file if plugin is removed.mdx
loads fine)@esbuild-plugins/node-resolve
without thebasedir
optionEarlier, I wanted to suggest removing
basedir
.However
basedir
appear to be added intentionally in #68. I have yet to look into what problem #68 is trying to solveThe text was updated successfully, but these errors were encountered: