|
| 1 | +/** |
| 2 | + * @license MIT |
| 3 | + * @file improved Version of @rollup/plugin-json and @rollup/plugin-yaml that combines the two and |
| 4 | + * provides the option for JSON.stringify() |
| 5 | + * @copyright (c) 2019 RollupJS Plugin Contributors |
| 6 | + * (https://github.com/rollup/plugins/graphs/contributors), 2021 Luke Zhang |
| 7 | + * @see {@link https://github.com/rollup/plugins/tree/master/packages/json} |
| 8 | + * @see {@link https://github.com/rollup/plugins/tree/master/packages/yaml} |
| 9 | + */ |
| 10 | + |
| 11 | +import {FilterPattern, createFilter, dataToEsm} from "@rollup/pluginutils" |
| 12 | +import yaml from "js-yaml" |
| 13 | +import type {PluginFunc} from "./types" |
| 14 | + |
| 15 | +type ValidYamlType = |
| 16 | + | number |
| 17 | + | string |
| 18 | + | boolean |
| 19 | + | null |
| 20 | + | undefined |
| 21 | + | {[key: string]: ValidYamlType} |
| 22 | + | ValidYamlType[] |
| 23 | + |
| 24 | +type RollupJsonOptions = { |
| 25 | + /** |
| 26 | + * All JSON and YAML files will be parsed by default, but you can also specifically include files |
| 27 | + * |
| 28 | + * @default ["**/*.json", "**/*.y(a)?ml"] |
| 29 | + */ |
| 30 | + include?: FilterPattern |
| 31 | + /** |
| 32 | + * All JSON and YAML files will be parsed by default, but you can also specifically exclude files |
| 33 | + * |
| 34 | + * @default |
| 35 | + */ |
| 36 | + exclude?: FilterPattern |
| 37 | + /** |
| 38 | + * For tree-shaking, properties will be declared as variables, using either `var` or `const`. |
| 39 | + * |
| 40 | + * @default true |
| 41 | + */ |
| 42 | + preferConst?: boolean |
| 43 | + /** |
| 44 | + * Specify indentation for the generated default export |
| 45 | + * |
| 46 | + * @default |
| 47 | + */ |
| 48 | + indent?: string |
| 49 | + /** |
| 50 | + * Ignores indent and generates the smallest code |
| 51 | + * |
| 52 | + * @default false |
| 53 | + */ |
| 54 | + compact?: boolean |
| 55 | + /** |
| 56 | + * Generate a named export for every property of the JSON object |
| 57 | + * |
| 58 | + * @default true |
| 59 | + */ |
| 60 | + namedExports?: boolean |
| 61 | + /** |
| 62 | + * Character for when json should be stringified and then parsed at runtime |
| 63 | + * |
| 64 | + * @default 14 * 1024 (14kb) |
| 65 | + * @see {@link https://v8.dev/blog/cost-of-javascript-2019#json} |
| 66 | + */ |
| 67 | + stringifyLimit?: number |
| 68 | + /** |
| 69 | + * A function which can optionally mutate parsed YAML. The function should return the mutated |
| 70 | + * `object`, or `undefined` which will make no changes to the parsed YAML. |
| 71 | + * |
| 72 | + * @default undefined |
| 73 | + */ |
| 74 | + transform?: (data: ValidYamlType, filePath: string) => ValidYamlType | undefined |
| 75 | + /** |
| 76 | + * - If `single`, specifies that the target YAML documents contain only one document in the target file(s). |
| 77 | + * - If more than one [document stream](https://yaml.org/spec/1.2/spec.html#id2801681) exists in |
| 78 | + * the target YAML file(s), set `documentMode: 'multi'`. |
| 79 | + * |
| 80 | + * @default 'single' |
| 81 | + */ |
| 82 | + documentMode?: "single" | "multi" |
| 83 | +} |
| 84 | + |
| 85 | +const json: PluginFunc<RollupJsonOptions> = ({ |
| 86 | + include = ["**/*.y(a)?ml", "**/*.json"], |
| 87 | + exclude, |
| 88 | + preferConst = true, |
| 89 | + indent = " ", |
| 90 | + compact = false, |
| 91 | + namedExports = true, |
| 92 | + stringifyLimit = 14 * 1024, |
| 93 | + transform, |
| 94 | + documentMode = "single", |
| 95 | +} = {}) => { |
| 96 | + const filter = createFilter(include, exclude) |
| 97 | + |
| 98 | + let loadMethod = (documentMode === "multi" ? yaml.loadAll : yaml.load) as ( |
| 99 | + str: string, |
| 100 | + iterator?: (doc: any) => void, |
| 101 | + opts?: yaml.LoadOptions, |
| 102 | + ) => ValidYamlType |
| 103 | + |
| 104 | + const plugin: ReturnType<PluginFunc<RollupJsonOptions>> = { |
| 105 | + name: "json/yaml", |
| 106 | + transform(code, id) { |
| 107 | + if (!filter(id)) { |
| 108 | + return null |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + const parsed = transform?.(loadMethod(code), id) ?? loadMethod(code) |
| 113 | + const stringified = JSON.stringify(parsed, null, 0) |
| 114 | + |
| 115 | + return { |
| 116 | + code: |
| 117 | + stringified.length > stringifyLimit |
| 118 | + ? `export default JSON.parse(${JSON.stringify(stringified)})` |
| 119 | + : dataToEsm(parsed, { |
| 120 | + preferConst, |
| 121 | + compact, |
| 122 | + namedExports, |
| 123 | + indent, |
| 124 | + }), |
| 125 | + map: {mappings: ""}, |
| 126 | + } |
| 127 | + } catch (err) { |
| 128 | + const message = `Could not parse JSON file: ${ |
| 129 | + err instanceof Error ? err.toString() : JSON.stringify(err) |
| 130 | + }` |
| 131 | + |
| 132 | + this.error({message, id}) |
| 133 | + } |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + return plugin |
| 138 | +} |
| 139 | + |
| 140 | +export default json |
0 commit comments