-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (33 loc) · 1018 Bytes
/
index.js
File metadata and controls
40 lines (33 loc) · 1018 Bytes
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
import chalk from 'chalk'
import { spawnSync } from "child_process";
import { getExePath } from "@pkl-community/pkl"
const fileRegex = /\.pkl$/;
export default function (conf) {
return {
name: "vite-plugin-pkl",
async transform(src, id) {
if (fileRegex.test(id)) {
let args = ['eval', '-f', 'json', id];
let result = await spawnSync(getExePath(), args, { encoding: 'utf8' })
if(conf?.debug){
console.log()
console.log("🥒", chalk.green("[PKL Compiler]"))
console.log(id);
console.log()
console.log(chalk.yellow("Raw PKL:"));
console.log(src)
console.log()
console.log(chalk.yellow("Transformed JSON:"));
console.log(JSON.parse(result.stdout))
console.log()
}
let json = result.stdout
const transformedCode = `const data = ${json}export default data`;
return {
code: json,
map: null,
};
}
},
};
}