Skip to content

Commit 3e406f5

Browse files
authored
fix: pass cwd to node-resolve (#68)
* fix: correct issue with cwd that was not letting relative imports work. see #66 * fix: remove absRoot plugin and change node-resolve config.
1 parent c9b2e26 commit 3e406f5

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ This module is distributed via [npm][npm] which is bundled with [node][node] and
206206
should be installed as one of your project's `dependencies`:
207207

208208
```
209-
npm install --save mdx-bundler
209+
npm install --save mdx-bundler esbuild
210210
```
211211

212212
One of mdx-bundler's dependancies requires a working [node-gyp][node-gyp] setup

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@
4242
"dependencies": {
4343
"@babel/runtime": "^7.14.6",
4444
"@esbuild-plugins/node-resolve": "^0.1.4",
45-
"@fal-works/esbuild-plugin-global-externals": "^2.1.1",
45+
"@fal-works/esbuild-plugin-global-externals": "^2.1.2",
4646
"gray-matter": "^4.0.3",
4747
"remark-frontmatter": "^3.0.0",
4848
"remark-mdx-frontmatter": "^1.0.1",
49-
"xdm": "^1.11.1"
49+
"xdm": "^1.12.1"
5050
},
5151
"peerDependencies": {
5252
"esbuild": "0.11.x || 0.12.x"
5353
},
5454
"devDependencies": {
5555
"@testing-library/react": "^12.0.0",
56-
"@types/jsdom": "^16.2.12",
57-
"@types/react": "^17.0.13",
58-
"@types/react-dom": "^17.0.8",
56+
"@types/jsdom": "^16.2.13",
57+
"@types/react": "^17.0.14",
58+
"@types/react-dom": "^17.0.9",
5959
"cross-env": "^7.0.3",
6060
"esbuild": "^0.12.15",
6161
"jsdom": "^16.6.0",
@@ -64,7 +64,7 @@
6464
"mdx-test-data": "^1.0.1",
6565
"react": "^17.0.2",
6666
"react-dom": "^17.0.2",
67-
"remark-mdx-images": "^1.0.2",
67+
"remark-mdx-images": "^1.0.3",
6868
"typescript": "^4.3.5",
6969
"uvu": "^0.5.1"
7070
},

src/__tests__/index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ import Demo from './demo'
152152
assert.equal(
153153
error.message,
154154
`Build failed with 1 error:
155-
__mdx_bundler_fake_dir__/_mdx_bundler_entry_point.mdx:3:17: error: Could not resolve "./demo"`,
155+
_mdx_bundler_entry_point.mdx:3:17: error: Could not resolve "./demo"`,
156156
)
157157
})
158158

@@ -174,7 +174,7 @@ import Demo from './demo'
174174
assert.equal(
175175
error.message,
176176
`Build failed with 1 error:
177-
__mdx_bundler_fake_dir__/demo.tsx:1:7: error: Could not resolve "./blah-blah"`,
177+
demo.tsx:1:7: error: Could not resolve "./blah-blah"`,
178178
)
179179
})
180180

@@ -196,7 +196,7 @@ import Demo from './demo.blah'
196196
assert.equal(
197197
error.message,
198198
`Build failed with 1 error:
199-
__mdx_bundler_fake_dir__/_mdx_bundler_entry_point.mdx:3:17: error: [plugin: inMemory] Invalid loader: "blah" (valid: js, jsx, ts, tsx, css, json, text, base64, dataurl, file, binary)`,
199+
_mdx_bundler_entry_point.mdx:3:17: error: [plugin: inMemory] Invalid loader: "blah" (valid: js, jsx, ts, tsx, css, json, text, base64, dataurl, file, binary)`,
200200
)
201201
})
202202

@@ -301,15 +301,15 @@ test('require from current directory', async () => {
301301
const mdxSource = `
302302
# Title
303303
304-
import {Sample} from './other/sample-component'
304+
import {Sample} from './sample-component'
305305
306306
<Sample />
307307
308-
![A Sample Image](./other/150.png)
308+
![A Sample Image](./150.png)
309309
`.trim()
310310

311311
const {code} = await bundleMDX(mdxSource, {
312-
cwd: process.cwd(),
312+
cwd: path.join(process.cwd(), 'other'),
313313
xdmOptions: options => {
314314
options.remarkPlugins = [remarkMdxImages]
315315

@@ -343,11 +343,11 @@ test('should output assets', async () => {
343343
const mdxSource = `
344344
# Sample Post
345345
346-
![Sample Image](./other/150.png)
346+
![Sample Image](./150.png)
347347
`.trim()
348348

349349
const {code} = await bundleMDX(mdxSource, {
350-
cwd: process.cwd(),
350+
cwd: path.join(process.cwd(), 'other'),
351351
xdmOptions: options => {
352352
options.remarkPlugins = [remarkMdxImages]
353353

@@ -374,7 +374,7 @@ test('should output assets', async () => {
374374

375375
const error = /** @type Error */ (
376376
await bundleMDX(mdxSource, {
377-
cwd: process.cwd(),
377+
cwd: path.join(process.cwd(), 'other'),
378378
xdmOptions: options => {
379379
options.remarkPlugins = [remarkMdxImages]
380380

@@ -452,7 +452,7 @@ import Demo from './demo'
452452
453453
<Demo />
454454
`.trim()
455-
455+
456456
const result = await bundleMDX(mdxSource, {
457457
files: {
458458
'./demo.tsx': `
@@ -466,13 +466,15 @@ function Demo() {
466466
}
467467
468468
export default Demo
469-
`.trim()
470-
}
469+
`.trim(),
470+
},
471471
})
472472

473473
const Component = getMDXComponent(result.code)
474474

475-
const {container} = render(React.createElement(Component), { container: document.body })
475+
const {container} = render(React.createElement(Component), {
476+
container: document.body,
477+
})
476478

477479
assert.match(container.innerHTML, 'Portal!')
478480
})

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ async function bundleMDX(
123123
const buildOptions = esbuildOptions({
124124
entryPoints: [entryPath],
125125
write: false,
126+
absWorkingDir: cwd,
126127
define: {
127128
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
128129
},
@@ -143,7 +144,10 @@ async function bundleMDX(
143144
},
144145
}),
145146
// eslint-disable-next-line @babel/new-cap
146-
NodeResolvePlugin({extensions: ['.js', '.ts', '.jsx', '.tsx']}),
147+
NodeResolvePlugin({
148+
extensions: ['.js', '.ts', '.jsx', '.tsx'],
149+
resolveOptions: {basedir: cwd},
150+
}),
147151
inMemoryPlugin,
148152
xdmESBuild(
149153
xdmOptions({
@@ -170,6 +174,7 @@ async function bundleMDX(
170174
return {
171175
code: `${code};return Component.default;`,
172176
frontmatter,
177+
errors: bundled.errors,
173178
}
174179
}
175180

@@ -187,6 +192,7 @@ async function bundleMDX(
187192
return {
188193
code: `${code};return Component.default;`,
189194
frontmatter,
195+
errors: bundled.errors,
190196
}
191197
}
192198

0 commit comments

Comments
 (0)