forked from rollup/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·63 lines (53 loc) · 1.73 KB
/
Copy pathindex.js
File metadata and controls
executable file
·63 lines (53 loc) · 1.73 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import YAML from 'js-yaml';
import toSource from 'tosource';
import { createFilter, makeLegalIdentifier, suffixRegex } from '@rollup/pluginutils';
const defaults = {
documentMode: 'single',
transform: null,
extensions: ['.yaml', '.yml']
};
export default function yaml(opts = {}) {
const options = Object.assign({}, defaults, opts);
const { documentMode, extensions } = options;
const filter = createFilter(options.include, options.exclude);
let loadMethod = null;
if (documentMode === 'single') {
loadMethod = YAML.load;
} else if (documentMode === 'multi') {
loadMethod = YAML.loadAll;
} else {
this.error(
`plugin-yaml → documentMode: '${documentMode}' is not a valid value. Please choose 'single' or 'multi'`
);
}
const extensionsFilter = suffixRegex(extensions, 'i');
return {
name: 'yaml',
transform: {
filter: {
id: extensionsFilter
},
handler(content, id) {
if (extensions.length === 0 || !extensionsFilter.test(id)) return null;
if (!filter(id)) return null;
let data = loadMethod(content);
if (typeof options.transform === 'function') {
const result = options.transform(data, id);
// eslint-disable-next-line no-undefined
if (result !== undefined) {
data = result;
}
}
const keys = Object.keys(data).filter((key) => key === makeLegalIdentifier(key));
const code = `var data = ${toSource(data)};\n\n`;
const exports = ['export default data;']
.concat(keys.map((key) => `export var ${key} = data.${key};`))
.join('\n');
return {
code: code + exports,
map: { mappings: '' }
};
}
}
};
}