Skip to content

Commit

Permalink
fix: provide json hint when importing a no export json file (#4741)
Browse files Browse the repository at this point in the history
* fix: add json hint for missing export error

* fix: tweak logical & add test

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
TrickyPi and lukastaegert committed Dec 8, 2022
1 parent 156f4d1 commit cc54a24
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/utils/error.ts
Expand Up @@ -7,6 +7,7 @@ import type {
RollupWarning,
WarningHandler
} from '../rollup/types';
import { extname } from '../utils/path';
import getCodeFrame from './getCodeFrame';
import { printQuotedStringList } from './printStringList';
import relativeId from './relativeId';
Expand Down Expand Up @@ -528,14 +529,15 @@ export function errorMissingExport(
importingModule: string,
exporter: string
): RollupLog {
const isJson = extname(exporter) === '.json';
return {
binding,
code: MISSING_EXPORT,
exporter,
id: importingModule,
message: `"${binding}" is not exported by "${relativeId(exporter)}", imported by "${relativeId(
importingModule
)}".`,
)}".${isJson ? ' (Note that you need @rollup/plugin-json to import JSON files)' : ''}`,
url: `https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module`
};
}
Expand Down
@@ -0,0 +1,28 @@
const path = require('node:path');
const ID_MAIN = path.join(__dirname, 'main.js');
const ID_ARRAY_JSON = path.join(__dirname, 'array.json');

module.exports = {
description: 'should provide json hint when importing a no export json file',
error: {
binding: 'default',
code: 'MISSING_EXPORT',
exporter: ID_ARRAY_JSON,
id: ID_MAIN,
url: 'https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module',
pos: 7,
loc: {
column: 7,
file: ID_MAIN,
line: 1
},
frame: `
1: import theArray from './array.json';
^
2: export default theArray;
`,
watchFiles: [ID_ARRAY_JSON, ID_MAIN],
message:
'"default" is not exported by "array.json", imported by "main.js". (Note that you need @rollup/plugin-json to import JSON files)'
}
};
@@ -0,0 +1 @@
["foo", "bar"]
@@ -0,0 +1,2 @@
import theArray from './array.json';
export default theArray;

0 comments on commit cc54a24

Please sign in to comment.