Skip to content

Commit 3909578

Browse files
committed
fix: use global registry for echarts functions to survive minification
The JS minifier renames local variables (e.g., 'fmt' -> 'r'), but string references like '$fn:fmt' in JSON config remain unchanged. This caused 'fmt is not defined' errors when eval() tried to find the renamed variable. Solution: Store functions in window._echartsFns global object with string keys that survive minification.
1 parent 2f8705e commit 3909578

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

layouts/shortcodes/echarts.html

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@
5858
{{- $content := .Inner -}}
5959
{{- $jsCode := "" -}}
6060
{{- $configContent := "" -}}
61+
{{- $fnNames := slice -}}
6162

62-
{{- /* 1. Extract ```js code blocks */ -}}
63+
{{- /* 1. Extract ```js code blocks and function names */ -}}
6364
{{- if findRE "```js[\\s\\S]*?```" $content -}}
6465
{{- range findRE "```js([\\s\\S]*?)```" $content -}}
65-
{{- $jsCode = printf "%s\n%s" $jsCode (replaceRE "```js([\\s\\S]*?)```" "$1" .) -}}
66+
{{- $block := replaceRE "```js([\\s\\S]*?)```" "$1" . -}}
67+
{{- $jsCode = printf "%s\n%s" $jsCode $block -}}
68+
{{- /* Extract function names like "var fmt = function" or "function fmt" */ -}}
69+
{{- range findRE "(?:var|let|const)\\s+(\\w+)\\s*=" $block -}}
70+
{{- $fnNames = $fnNames | append (replaceRE "(?:var|let|const)\\s+(\\w+)\\s*=.*" "$1" .) -}}
71+
{{- end -}}
6672
{{- end -}}
6773
{{- $content = replaceRE "```js[\\s\\S]*?```" "" $content -}}
6874
{{- end -}}
@@ -92,8 +98,14 @@
9298
var dom = document.getElementById('{{ $id }}');
9399
if (!dom) return;
94100

95-
{{- /* Output JavaScript function definitions */ -}}
101+
// Global function registry (survives minification)
102+
window._echartsFns = window._echartsFns || {};
103+
104+
{{- /* Output JavaScript function definitions and register them globally */ -}}
96105
{{ $jsCode | safeJS }}
106+
{{- range $fnNames }}
107+
window._echartsFns['{{ . }}'] = {{ . | safeJS }};
108+
{{- end }}
97109

98110
{{- /* Determine theme */ -}}
99111
{{- if $theme }}
@@ -105,11 +117,11 @@
105117
var chart = echarts.init(dom, theme);
106118
var optionsJson = {{ $options | jsonify | safeJS }};
107119

108-
// Replace $fn:xxx references with actual functions
120+
// Replace $fn:xxx references with actual functions from global registry
109121
function replaceFnRefs(obj) {
110122
if (typeof obj === 'string' && obj.startsWith('$fn:')) {
111123
var fnName = obj.substring(4);
112-
return eval(fnName);
124+
return window._echartsFns[fnName];
113125
}
114126
if (Array.isArray(obj)) {
115127
return obj.map(replaceFnRefs);

0 commit comments

Comments
 (0)