Skip to content

Commit 1864baf

Browse files
authored
fix: respect the loader set in esbuild config (#39)
* fix: respect the loader set in esbuild config, closes #38 * fix: update to xdm 1.9.0, resolves #35 * chore: fix linting and add assertion to test
1 parent e9bde42 commit 1864baf

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"validate": "kcd-scripts validate"
4141
},
4242
"dependencies": {
43-
"@babel/runtime": "^7.13.17",
43+
"@babel/runtime": "^7.14.0",
4444
"@esbuild-plugins/node-resolve": "^0.1.4",
4545
"@fal-works/esbuild-plugin-global-externals": "^2.1.1",
4646
"esbuild": "^0.11.16",
@@ -49,7 +49,7 @@
4949
"remark-frontmatter": "^3.0.0",
5050
"remark-mdx-frontmatter": "^1.0.1",
5151
"uvu": "^0.5.1",
52-
"xdm": "^1.8.0"
52+
"xdm": "^1.9.0"
5353
},
5454
"devDependencies": {
5555
"@testing-library/react": "^11.2.6",

src/__tests__/index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import './setup-tests.js'
2+
import path from 'path'
23
import {test} from 'uvu'
34
import * as assert from 'uvu/assert'
45
import React from 'react'
56
import rtl from '@testing-library/react'
67
import leftPad from 'left-pad'
78
import {remarkMdxImages} from 'remark-mdx-images'
8-
import path from 'path'
99
import {bundleMDX} from '../index.js'
1010
import {getMDXComponent} from '../client.js'
1111

@@ -252,6 +252,44 @@ import LeftPad from 'left-pad-js'
252252
assert.match(container.innerHTML, 'this is left pad')
253253
})
254254

255+
test('should respect the configured loader for files', async () => {
256+
const mdxSource = `
257+
# Title
258+
259+
import {Demo} from './demo'
260+
261+
<Demo />
262+
`.trim()
263+
264+
const files = {
265+
'./demo.ts': `
266+
import React from 'react'
267+
268+
export const Demo: React.FC = () => {
269+
return <p>Sample</p>
270+
}
271+
`.trim(),
272+
}
273+
274+
const {code} = await bundleMDX(mdxSource, {
275+
files,
276+
esbuildOptions: options => {
277+
options.loader = {
278+
...options.loader,
279+
'.ts': 'tsx',
280+
}
281+
282+
return options
283+
},
284+
})
285+
286+
const Component = getMDXComponent(code)
287+
288+
const {container} = render(React.createElement(Component))
289+
290+
assert.match(container.innerHTML, 'Sample')
291+
})
292+
255293
test('require from current directory', async () => {
256294
const mdxSource = `
257295
# Title

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,21 @@ async function bundleMDX(
104104
return {contents: vfile.toString(), loader: 'jsx'}
105105
}
106106
default: {
107+
/** @type import('esbuild').Loader */
108+
let loader
109+
110+
if (
111+
build.initialOptions.loader &&
112+
build.initialOptions.loader[`.${fileType}`]
113+
) {
114+
loader = build.initialOptions.loader[`.${fileType}`]
115+
} else {
116+
loader = /** @type import('esbuild').Loader */ (fileType)
117+
}
118+
107119
return {
108120
contents,
109-
loader: /** @type import('esbuild').Loader */ (fileType),
121+
loader,
110122
}
111123
}
112124
}

0 commit comments

Comments
 (0)