Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/json/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export default function json(options = {}) {
name: 'json',

// eslint-disable-next-line no-shadow
transform(code, id) {
if (id.slice(-5) !== '.json' || !filter(id)) return null;
transform(code, id, transformOptions) {
const rawId = (transformOptions && transformOptions.rawId) || id;
if (rawId.slice(-5) !== '.json' || !filter(rawId)) return null;

try {
const parsed = JSON.parse(code);
Expand Down
22 changes: 22 additions & 0 deletions packages/json/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,25 @@ test('handles empty keys', async () => {
'export var b = "c";\nexport default {\n\t"": "a",\n\tb: b\n};\n'
);
});
test('handles transform options without rawId as passed by Rollup 4', async () => {
expect(json().transform(`{"a":"b"}`, 'input.json', { attributes: {} }).code).toEqual(
'export var a = "b";\nexport default {\n\ta: a\n};\n'
);
});
const idWithJsonAttributes = `input.json?attributes=${encodeURIComponent(
JSON.stringify({ type: 'json' })
)}`;
test('includes json files with import attributes', async () => {
expect(
json({ include: /\.json$/ }).transform(`{"a":"b"}`, idWithJsonAttributes, {
rawId: 'input.json'
}).code
).toEqual('export var a = "b";\nexport default {\n\ta: a\n};\n');
});
test('excludes json files with import attributes', async () => {
expect(
json({ exclude: /\.json$/ }).transform(`{"a":"b"}`, idWithJsonAttributes, {
rawId: 'input.json'
})
).toBeNull();
});
Comment on lines +180 to +186

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test currently does not trigger out fix because it filters out the file. What about this

Suggested change
test('excludes json files with import attributes', async () => {
expect(
json({ exclude: /\.json$/ }).transform(`{"a":"b"}`, idWithJsonAttributes, {
rawId: 'input.json'
})
).toBeNull();
});
test('applies exclude patterns to the raw id instead of the encoded id', async () => {
expect(
json({ exclude: /attributes/ }).transform(`{"a":"b"}`, idWithJsonAttributes, {
rawId: 'input.json'
}).code
).toEqual('export var a = "b";\nexport default {\n\ta: a\n};\n');
});

Loading