Skip to content
Merged
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
6 changes: 3 additions & 3 deletions packages/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@
"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": {
"optional": true
}
},
"dependencies": {
"@rollup/pluginutils": "^5.1.0"
"@rollup/pluginutils": "^5.6.0"
},
"devDependencies": {
"@rollup/plugin-buble": "^1.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-node-resolve-earliest": "npm:@rollup/plugin-node-resolve@11.2.1",
"rollup": "catalog:",
"rollup": "catalog:rollup-next",
"rollup-earliest": "npm:rollup@1.20.0",
"source-map-support": "^0.5.21"
},
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(id)) 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);
20 changes: 19 additions & 1 deletion packages/json/test/snapshots/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`generates correct code with compact=true 1`] = `"export var validKey=true;export var nested={subKey:"ok"};export var array=[1,"2"];export default{validKey:validKey,"invalid-key":1,nested:nested,array:array,"function":"not used","null":null};"`;
exports[`generates correct code with compact=true 1`] = `"export var validKey=true;export var nested={subKey:"ok"};export var array=[1,"2"];var _arbitrary0="not used";var _arbitrary1=null;export{_arbitrary0 as function,_arbitrary1 as null};export default{validKey:validKey,"invalid-key":1,nested:nested,array:array,"function":"not used","null":null};"`;

exports[`generates correct code with namedExports=false 1`] = `
"export default {
Expand All @@ -27,6 +27,12 @@ export const array = [
1,
"2"
];
const _arbitrary0 = "not used";
const _arbitrary1 = null;
export {
_arbitrary0 as function,
_arbitrary1 as null
};
export default {
validKey: validKey,
"invalid-key": 1,
Expand All @@ -47,6 +53,12 @@ export var array = [
1,
"2"
];
var _arbitrary0 = "not used";
var _arbitrary1 = null;
export {
_arbitrary0 as function,
_arbitrary1 as null
};
export default {
validKey: validKey,
"invalid-key": 1,
Expand All @@ -67,6 +79,12 @@ export var array = [
1,
"2"
];
var _arbitrary0 = "not used";
var _arbitrary1 = null;
export {
_arbitrary0 as function,
_arbitrary1 as null
};
export default {
validKey: validKey,
"invalid-key": 1,
Expand Down
25 changes: 25 additions & 0 deletions packages/json/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const avaAssertions = {
};

const read = (file) => readFileSync(file, 'utf-8');
const idWithJsonAttributes = `input.json?attributes=${encodeURIComponent(
JSON.stringify({ type: 'json' })
)}`;
require('source-map-support').install();

process.chdir(__dirname);
Expand Down Expand Up @@ -171,3 +174,25 @@ test('handles empty keys', async () => {
'export var b = "c";\nexport default {\n\t"": "a",\n\tb: b\n};\n'
);
});
test.skipIf(rollupMajor < 5)('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('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('does not apply exclude patterns to the attribute-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');
});
39 changes: 32 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading