mirror of
https://github.com/unjs/unbuild.git
synced 2025-12-08 19:25:11 +00:00
27 lines
757 B
TypeScript
27 lines
757 B
TypeScript
import type { Plugin, TransformHook } from "rollup";
|
|
import type { RollupJsonOptions } from "@rollup/plugin-json";
|
|
import rollupJSONPlugin from "@rollup/plugin-json";
|
|
|
|
const EXPORT_DEFAULT = "export default ";
|
|
|
|
export function JSONPlugin(options: RollupJsonOptions): Plugin {
|
|
const plugin = rollupJSONPlugin(options);
|
|
return <Plugin>{
|
|
...plugin,
|
|
name: "unbuild-json",
|
|
transform(code, id) {
|
|
const res = (plugin.transform as TransformHook)!.call(this, code, id);
|
|
if (
|
|
res &&
|
|
typeof res !== "string" &&
|
|
"code" in res &&
|
|
res.code &&
|
|
res.code.startsWith(EXPORT_DEFAULT)
|
|
) {
|
|
res.code = res.code.replace(EXPORT_DEFAULT, "module.exports = ");
|
|
}
|
|
return res;
|
|
},
|
|
};
|
|
}
|