From aabcb546896b3ec84b43787fc9505e14d6a229e0 Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Sun, 26 Jul 2026 19:37:30 +0800 Subject: [PATCH 1/2] feat: add Rollup 5 compatibility --- packages/json/src/index.js | 7 +++++-- packages/json/test/test.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/json/src/index.js b/packages/json/src/index.js index dcb8f3333..80a3733d7 100755 --- a/packages/json/src/index.js +++ b/packages/json/src/index.js @@ -8,8 +8,11 @@ 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) { + if (!filter(id)) return null; + if (transformOptions) { + if (transformOptions.rawId.slice(-5) !== '.json') return null; + } else if (id.slice(-5) !== '.json') return null; try { const parsed = JSON.parse(code); diff --git a/packages/json/test/test.js b/packages/json/test/test.js index 3dfa8b039..ca9008df4 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -162,3 +162,13 @@ test('handles empty keys', async () => { 'export var b = "c";\nexport default {\n\t"": "a",\n\tb: b\n};\n' ); }); +test('handles json files with import attributes', async () => { + const result = json().transform( + `{"a":"b"}`, + `input.json?attributes=${encodeURIComponent(JSON.stringify({ type: 'json' }))}`, + { + rawId: 'input.json' + } + ); + expect(result).not.toBeNull(); +}); From 0f2fd7fb32c5ef495307242d5ee37570251f93dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E7=AC=91?= <530257315@qq.com> Date: Wed, 28 Oct 2026 22:51:06 +0800 Subject: [PATCH 2/2] fix(json): use rawId for the extension check and filters --- packages/json/src/index.js | 6 ++---- packages/json/test/test.js | 28 ++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/json/src/index.js b/packages/json/src/index.js index 80a3733d7..6148ac7a9 100755 --- a/packages/json/src/index.js +++ b/packages/json/src/index.js @@ -9,10 +9,8 @@ export default function json(options = {}) { // eslint-disable-next-line no-shadow transform(code, id, transformOptions) { - if (!filter(id)) return null; - if (transformOptions) { - if (transformOptions.rawId.slice(-5) !== '.json') return null; - } else if (id.slice(-5) !== '.json') return null; + 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 ca9008df4..694bc247f 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -162,13 +162,25 @@ test('handles empty keys', async () => { 'export var b = "c";\nexport default {\n\t"": "a",\n\tb: b\n};\n' ); }); -test('handles json files with import attributes', async () => { - const result = json().transform( - `{"a":"b"}`, - `input.json?attributes=${encodeURIComponent(JSON.stringify({ type: 'json' }))}`, - { - rawId: 'input.json' - } +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' ); - expect(result).not.toBeNull(); +}); +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(); });