Skip to content

Commit 9d2aa80

Browse files
authoredSep 8, 2022
Add file, positional info to crashes in webpack loader
Closes GH-2124. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 56d7066 commit 9d2aa80

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed
 

‎packages/loader/lib/index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('vfile').VFileCompatible} VFileCompatible
33
* @typedef {import('vfile').VFile} VFile
4+
* @typedef {import('vfile-message').VFileMessage} VFileMessage
45
* @typedef {import('@mdx-js/mdx').CompileOptions} CompileOptions
56
* @typedef {Pick<CompileOptions, 'SourceMapGenerator'>} Defaults
67
* @typedef {Omit<CompileOptions, 'SourceMapGenerator'>} Options
@@ -10,6 +11,7 @@
1011
*/
1112

1213
import {createHash} from 'node:crypto'
14+
import path from 'node:path';
1315
import {SourceMapGenerator} from 'source-map'
1416
import {createFormatAwareProcessors} from '@mdx-js/mdx/lib/util/create-format-aware-processors.js'
1517

@@ -62,10 +64,16 @@ export function loader(value, callback) {
6264
map.set(hash, process)
6365
}
6466

65-
process({value, path: this.resourcePath}).then((file) => {
66-
callback(null, file.value, file.map)
67-
return file
68-
}, callback)
67+
process({value, path: this.resourcePath}).then(
68+
(file) => {
69+
callback(null, file.value, file.map)
70+
},
71+
(/** @type VFileMessage */ e) => {
72+
const fpath = path.relative(this.context, this.resourcePath);
73+
e.message = `${fpath}:${e.name}: ${e.message}`;
74+
callback(e);
75+
}
76+
)
6977
}
7078

7179
/**

‎packages/loader/test/index.test.js

+46-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,49 @@ test('@mdx-js/loader', async () => {
2222
// Setup.
2323
const base = new URL('.', import.meta.url)
2424

25+
await fs.writeFile(
26+
new URL('webpack.mdx', base),
27+
'# Hello, {<Message />'
28+
)
29+
30+
// Errors.
31+
const failedResult = await promisify(webpack)({
32+
// @ts-expect-error To do: webpack types miss support for `context`.
33+
context: fileURLToPath(base),
34+
entry: './webpack.mdx',
35+
mode: 'none',
36+
module: {
37+
rules: [
38+
{
39+
test: /\.mdx$/,
40+
use: [fileURLToPath(new URL('../index.cjs', import.meta.url))]
41+
}
42+
]
43+
},
44+
output: {
45+
path: fileURLToPath(base),
46+
filename: 'react.cjs',
47+
libraryTarget: 'commonjs'
48+
}
49+
})
50+
51+
const error = failedResult?.toJson()?.errors?.[0]
52+
53+
assert.ok(error);
54+
assert.equal(
55+
error.message,
56+
`Module build failed (from ../index.cjs):
57+
webpack.mdx:1:22: Unexpected end of file in expression, expected a corresponding closing brace for \`{\``,
58+
'received expected error message'
59+
)
60+
2561
await fs.writeFile(
2662
new URL('webpack.mdx', base),
2763
'export const Message = () => <>World!</>\n\n# Hello, <Message />'
2864
)
2965

3066
// React.
31-
await promisify(webpack)({
67+
const reactBuild = await promisify(webpack)({
3268
// @ts-expect-error To do: webpack types miss support for `context`.
3369
context: fileURLToPath(base),
3470
entry: './webpack.mdx',
@@ -48,6 +84,8 @@ test('@mdx-js/loader', async () => {
4884
}
4985
})
5086

87+
assert.not.ok(reactBuild?.hasErrors())
88+
5189
// One for ESM loading CJS, one for webpack.
5290
const modReact = /** @type {{default: {default: MDXContent}}} */ (
5391
// @ts-expect-error file is dynamically generated
@@ -63,7 +101,7 @@ test('@mdx-js/loader', async () => {
63101
await fs.unlink(new URL('react.cjs', base))
64102

65103
// Preact and source maps
66-
await promisify(webpack)({
104+
const preactBuild = await promisify(webpack)({
67105
// @ts-expect-error To do: webpack types miss support for `context`.
68106
context: fileURLToPath(base),
69107
entry: './webpack.mdx',
@@ -89,6 +127,9 @@ test('@mdx-js/loader', async () => {
89127
}
90128
})
91129

130+
assert.not.ok(preactBuild?.hasErrors())
131+
132+
92133
// One for ESM loading CJS, one for webpack.
93134
const modPreact = /** @type {{default: {default: PreactComponent}}} */ (
94135
// @ts-expect-error file is dynamically generated.
@@ -110,7 +151,7 @@ test('@mdx-js/loader', async () => {
110151
await fs.unlink(new URL('preact.cjs', base))
111152

112153
// Vue.
113-
await promisify(webpack)({
154+
const vueBuild = await promisify(webpack)({
114155
// @ts-expect-error To do: webpack types miss support for `context`.
115156
context: fileURLToPath(base),
116157
entry: './webpack.mdx',
@@ -140,6 +181,8 @@ test('@mdx-js/loader', async () => {
140181
}
141182
})
142183

184+
assert.not.ok(vueBuild?.hasErrors())
185+
143186
// One for ESM loading CJS, one for webpack.
144187
const modVue = /** @type {{default: {default: VueComponent}}} */ (
145188
// @ts-expect-error file is dynamically generated

1 commit comments

Comments
 (1)

vercel[bot] commented on Sep 8, 2022

@vercel[bot]

Successfully deployed to the following URLs:

mdx – ./

mdx-git-main-mdx.vercel.app
mdxjs.com
mdx-mdx.vercel.app
v2.mdxjs.com

Please sign in to comment.