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
4 changes: 2 additions & 2 deletions packages/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
Expand Down
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
3 changes: 3 additions & 0 deletions packages/json/test/fixtures/with-type/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"answer": 42
}
3 changes: 3 additions & 0 deletions packages/json/test/fixtures/with-type/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from './config.json' with { type: 'json' };

t.is(config.answer, 42);
32 changes: 31 additions & 1 deletion packages/json/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
Expand Down Expand Up @@ -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');
});
Loading
Loading