Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 6 additions & 0 deletions packages/json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Default: `false`

If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking.

### `extensions`

Type: `Array[...String]`<br>
Default: `['.json']`
A list of file extensions to be handled by the plugin. By default, only `.json` files are handled.

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)
Expand Down
6 changes: 5 additions & 1 deletion packages/json/src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { createFilter, dataToEsm } from '@rollup/pluginutils';

Check warning on line 1 in packages/json/src/index.js

View workflow job for this annotation

GitHub Actions / Node v18

There should be at least one empty line between import groups

Check warning on line 1 in packages/json/src/index.js

View workflow job for this annotation

GitHub Actions / Node v20

There should be at least one empty line between import groups
import { extname } from 'path';

Check warning on line 2 in packages/json/src/index.js

View workflow job for this annotation

GitHub Actions / Node v18

`path` import should occur before import of `@rollup/pluginutils`

Check warning on line 2 in packages/json/src/index.js

View workflow job for this annotation

GitHub Actions / Node v20

`path` import should occur before import of `@rollup/pluginutils`

export default function json(options = {}) {
const filter = createFilter(options.include, options.exclude);
const indent = 'indent' in options ? options.indent : '\t';
const extensions = new Set('extensions' in options ? options.extensions : ['.json']);

return {
name: 'json',

// eslint-disable-next-line no-shadow
transform(code, id) {
if (id.slice(-5) !== '.json' || !filter(id)) return null;
const extension = extname(id);

if (!extensions.has(extension) || !filter(id)) return null;

try {
const parsed = JSON.parse(code);
Expand Down
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/custom-file-extension/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from './config.customjson';

t.is(config.answer, 42);
21 changes: 21 additions & 0 deletions packages/json/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,24 @@ test('handles empty keys', async (t) => {
'export var b = "c";\nexport default {\n\t"": "a",\n\tb: b\n};\n'
);
});

test('uses extensions option', async (t) => {
const bundle = await rollup({
input: 'fixtures/custom-file-extension/main.js',
plugins: [
json({
extensions: ['.customjson']
})
]
});
t.plan(1);
return testBundle(t, bundle);
});

test('does not process non-custom extensions', async (t) => {
const content = 'some content';
const id = 'testfile.json';
const plugin = json({ extensions: ['.custom'] });
const output = plugin.transform(content, id);
t.is(output, null, 'Should return null for files without custom extensions');
});
5 changes: 5 additions & 0 deletions packages/json/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export interface RollupJsonOptions {
* @default true
*/
namedExports?: boolean;
/**
* Specify the file extensions to be parsed as JSON
* @default ['.json']
*/
extensions?: string[];
}

/**
Expand Down
Loading