Skip to content

Commit bd86242

Browse files
committed
feat: use esbuild to support ts for codegen
1 parent 29bf994 commit bd86242

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"echarts": "^5.4.3",
4949
"echarts-gl": "^2.0.9",
5050
"echarts-liquidfill": "^3.1.0",
51+
"esbuild-wasm": "^0.19.2",
5152
"eslint": "^7.32.0",
5253
"eslint-plugin-prettier": "^3.4.1",
5354
"eslint-plugin-vue": "^8.7.1",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/demo/CodeGen.vue

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import {
55
watch,
66
onBeforeUnmount,
77
defineProps,
8-
defineEmits
8+
defineEmits,
9+
onMounted
910
} from "vue";
1011
import { useLocalStorage } from "@vueuse/core";
1112
import "highlight.js/styles/github.css";
1213
import hljs from "highlight.js/lib/core";
1314
import javascript from "highlight.js/lib/languages/javascript";
1415
import typescript from "highlight.js/lib/languages/typescript";
1516
import hljsVuePlugin from "@highlightjs/vue-plugin";
17+
import { initialize, transform } from "esbuild-wasm";
1618
1719
import { getImportsFromOption } from "./utils/codegen";
1820
@@ -56,37 +58,76 @@ watch(
5658
}
5759
5860
setTimeout(() => {
61+
if (initializing.value) {
62+
return;
63+
}
5964
source.value?.focus();
6065
});
6166
}
6267
);
6368
69+
const initializing = ref(true);
6470
const optionCode = ref("");
65-
const importCode = computed(() => {
66-
if (optionCode.value.trim() === "") {
67-
return "/* Paste your option code first */";
68-
}
71+
const transformedCode = ref("");
72+
const transformErrors = ref([]);
73+
74+
onMounted(async () => {
75+
await initialize({
76+
wasmURL: "https://cdn.jsdelivr.net/npm/[email protected]/esbuild.wasm"
77+
});
78+
79+
initializing.value = false;
80+
source.value?.focus();
81+
});
6982
70-
let option = null;
83+
watch(optionCode, async val => {
7184
try {
72-
option = JSON.parse(optionCode.value);
85+
transformedCode.value = await transform(`(${val})`, { loader: "ts" });
86+
transformErrors.value = [];
7387
} catch (e) {
74-
try {
75-
option = eval(`(${optionCode.value})`);
76-
} catch (e) {
77-
return `/* Unable to parse \`option\` code */
78-
// ${e.message}
88+
transformErrors.value = e.errors;
89+
}
90+
});
91+
92+
function formatError(errors) {
93+
return errors
94+
.map(({ text, location: { lineText, line, column, length } }) => {
95+
const digit = Math.ceil(Math.log10(line)) || 1;
96+
lineText = line === 1 ? lineText.slice(1) : lineText;
97+
lineText =
98+
line === optionCode.value.split("\n").length
99+
? lineText.slice(0, -1)
100+
: lineText;
101+
column = line === 1 ? column - 1 : column;
102+
103+
return `/* ${text} */
104+
105+
// ${line} | ${lineText}
106+
// ${" ".repeat(digit)} | ${" ".repeat(column)}${"~".repeat(length)}
79107
`;
80-
}
108+
})
109+
.join("\n\n");
110+
}
111+
112+
const importCode = computed(() => {
113+
if (optionCode.value.trim() === "") {
114+
return "// Paste your option code first";
115+
}
116+
117+
if (transformErrors.value.length) {
118+
return formatError(transformErrors.value);
81119
}
82120
83121
try {
84-
return getImportsFromOption(option, {
122+
return getImportsFromOption(eval(transformedCode.value.code), {
85123
renderer: renderer.value,
86124
...codegenOptions.value
87125
});
88126
} catch (e) {
89-
return `/* Invalid ECharts option */`;
127+
return `/* Invalid ECharts option */
128+
129+
// ${e.message}
130+
`;
90131
}
91132
});
92133
@@ -169,7 +210,12 @@ onBeforeUnmount(() => {
169210
ref="source"
170211
class="option-code"
171212
v-model="optionCode"
172-
placeholder="Paste your option code here..."
213+
:placeholder="
214+
initializing
215+
? 'Initializing...'
216+
: 'Paste your option code (TS/JS literal) here...'
217+
"
218+
:disabled="initializing"
173219
autofocus
174220
></textarea>
175221
<code-highlight

0 commit comments

Comments
 (0)