Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide json hint when importing a no export json file #4741

Merged
merged 3 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;