magnetar/fe_calckey/frontend/client/vite.json5.ts

50 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-07 19:22:30 +00:00
// Original: https://github.com/rollup/plugins/tree/8835dd2aed92f408d7dc72d7cc25a9728e16face/packages/json
import JSON5 from "json5";
import { Plugin } from "rollup";
import { createFilter, dataToEsm } from "@rollup/pluginutils";
import { RollupJsonOptions } from "@rollup/plugin-json";
interface Json5SyntaxError extends SyntaxError {
2024-04-08 01:10:23 +00:00
lineNumber: number;
columnNumber: number;
2023-07-07 19:22:30 +00:00
}
export default function json5(options: RollupJsonOptions = {}): Plugin {
2024-04-08 01:10:23 +00:00
const filter = createFilter(options.include, options.exclude);
const indent = "indent" in options ? options.indent : "\t";
2023-07-07 19:22:30 +00:00
2024-04-08 01:10:23 +00:00
return {
name: "json5",
2023-07-07 19:22:30 +00:00
2024-04-08 01:10:23 +00:00
transform(json, id) {
if (id.slice(-6) !== ".json5" || !filter(id)) return null;
2023-07-07 19:22:30 +00:00
2024-04-08 01:10:23 +00:00
try {
const parsed = JSON5.parse(json);
return {
code: dataToEsm(parsed, {
preferConst: options.preferConst,
compact: options.compact,
namedExports: options.namedExports,
indent,
}),
map: { mappings: "" },
};
} catch (err) {
if (!(err instanceof SyntaxError)) {
throw err;
}
const message = "Could not parse JSON5 file";
const { lineNumber, columnNumber } = err as Json5SyntaxError;
this.warn({
message,
id,
loc: { line: lineNumber, column: columnNumber },
});
return null;
}
},
};
2023-07-07 19:22:30 +00:00
}