diff --git a/packages/json/src/index.js b/packages/json/src/index.js index dcb8f3333..6148ac7a9 100755 --- a/packages/json/src/index.js +++ b/packages/json/src/index.js @@ -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); diff --git a/packages/json/test/test.js b/packages/json/test/test.js index 3dfa8b039..694bc247f 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -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(); +});