Skip to content

feat(json): Add extensions option to load JSON files with custom extensions #1857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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';
import { extname } from 'path';

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