forked from rollup/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
路32 lines (28 loc) 路 880 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { createFilter, dataToEsm } from '@rollup/pluginutils';
export default function json(options = {}) {
const filter = createFilter(options.include, options.exclude);
const indent = 'indent' in options ? options.indent : '\t';
return {
name: 'json',
// eslint-disable-next-line no-shadow
transform(code, id) {
if (id.slice(-5) !== '.json' || !filter(id)) return null;
try {
const parsed = JSON.parse(code);
return {
code: dataToEsm(parsed, {
preferConst: options.preferConst,
compact: options.compact,
namedExports: options.namedExports,
indent
}),
map: { mappings: '' }
};
} catch (err) {
const message = 'Could not parse JSON file';
this.error({ message, id, cause: err });
return null;
}
}
};
}