diff --git a/packages/json/package.json b/packages/json/package.json index 41576afcf..29709313d 100755 --- a/packages/json/package.json +++ b/packages/json/package.json @@ -54,7 +54,7 @@ "rollup-plugin" ], "peerDependencies": { - "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0" + "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0||^5.0.0" }, "peerDependenciesMeta": { "rollup": { @@ -67,7 +67,7 @@ "devDependencies": { "@rollup/plugin-buble": "^1.0.0", "@rollup/plugin-node-resolve": "^15.0.0", - "rollup": "^4.0.0-24", + "rollup": "catalog:rollup-next", "source-map-support": "^0.5.21" }, "types": "./types/index.d.ts" 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/fixtures/with-type/config.json b/packages/json/test/fixtures/with-type/config.json new file mode 100644 index 000000000..0a6e8769b --- /dev/null +++ b/packages/json/test/fixtures/with-type/config.json @@ -0,0 +1,3 @@ +{ + "answer": 42 +} diff --git a/packages/json/test/fixtures/with-type/main.js b/packages/json/test/fixtures/with-type/main.js new file mode 100644 index 000000000..9292f9b29 --- /dev/null +++ b/packages/json/test/fixtures/with-type/main.js @@ -0,0 +1,3 @@ +import config from './config.json' with { type: 'json' }; + +t.is(config.answer, 42); diff --git a/packages/json/test/test.js b/packages/json/test/test.js index 3dfa8b039..e46a871e8 100755 --- a/packages/json/test/test.js +++ b/packages/json/test/test.js @@ -100,7 +100,7 @@ test('handles garbage', async () => { }).catch((error) => error); expect(err.code).toBe('PLUGIN_ERROR'); expect(err.plugin).toBe('json'); - expect(err.message).toBe('Could not parse JSON file'); + expect(err.message).toBe('[plugin json] fixtures/garbage/bad.json: Could not parse JSON file'); expect(err.name).toBe('RollupError'); expect(err.cause.name).toBe('SyntaxError'); expect(err.id).toMatch(/(.*)bad.json$/); @@ -162,3 +162,33 @@ test('handles empty keys', async () => { 'export var b = "c";\nexport default {\n\t"": "a",\n\tb: b\n};\n' ); }); +test('converts json imported with import attributes', async () => { + const bundle = await rollup({ + input: 'fixtures/with-type/main.js', + plugins: [json()] + }); + expect.assertions(1); + return testBundle(avaAssertions, bundle); +}); +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('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'); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 22bc03048..31ac5021d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -463,17 +463,17 @@ importers: dependencies: '@rollup/pluginutils': specifier: ^5.1.0 - version: 5.1.0(rollup@4.0.0-24) + version: 5.1.0(rollup@5.0.0-3) devDependencies: '@rollup/plugin-buble': specifier: ^1.0.0 - version: 1.0.0(rollup@4.0.0-24) + version: 1.0.0(rollup@5.0.0-3) '@rollup/plugin-node-resolve': specifier: ^15.0.0 - version: 15.0.0(rollup@4.0.0-24) + version: 15.0.0(rollup@5.0.0-3) rollup: - specifier: ^4.0.0-24 - version: 4.0.0-24 + specifier: catalog:rollup-next + version: 5.0.0-3 source-map-support: specifier: ^0.5.21 version: 0.5.21 @@ -6585,6 +6585,14 @@ snapshots: optionalDependencies: rollup: 4.0.0-24 + '@rollup/plugin-buble@1.0.0(rollup@5.0.0-3)': + dependencies: + '@rollup/pluginutils': 4.2.1 + '@types/buble': 0.19.2 + buble: 0.20.0 + optionalDependencies: + rollup: 5.0.0-3 + '@rollup/plugin-commonjs@23.0.0(rollup@4.0.0-24)': dependencies: '@rollup/pluginutils': 4.2.1 @@ -6699,6 +6707,14 @@ snapshots: optionalDependencies: rollup: 4.0.0-24 + '@rollup/pluginutils@5.1.0(rollup@5.0.0-3)': + dependencies: + '@types/estree': 1.0.0 + estree-walker: 2.0.2 + picomatch: 2.3.1 + optionalDependencies: + rollup: 5.0.0-3 + '@rollup/pluginutils@5.3.0(rollup@4.0.0-24)': dependencies: '@types/estree': 1.0.8