Skip to content

Commit 001cc1a

Browse files
authored
refactor: use Web Components without native class support detection (#836)
1 parent f7ed1e1 commit 001cc1a

File tree

5 files changed

+14
-36
lines changed

5 files changed

+14
-36
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
},
4242
"devDependencies": {
4343
"@highlightjs/vue-plugin": "^2.1.0",
44-
"@rollup/plugin-replace": "^6.0.2",
4544
"@types/node": "^22.15.21",
4645
"@typescript-eslint/utils": "^8.32.1",
4746
"@vercel/analytics": "^1.3.1",

pnpm-lock.yaml

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

rollup.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import replace from "@rollup/plugin-replace";
21
import esbuild from "rollup-plugin-esbuild";
32
import { dts } from "rollup-plugin-dts";
43
import css from "rollup-plugin-import-css";
@@ -15,7 +14,6 @@ function configBuild(options, csp) {
1514
const { plugins, output } = result;
1615

1716
result.plugins = [
18-
...(csp ? [replace({ __CSP__: `${csp}`, preventAssignment: true })] : []),
1917
...plugins,
2018
css({
2119
...(csp ? { output: "style.css" } : { inject: true }),

src/ECharts.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ import type { EChartsElement } from "./wc";
4141

4242
import "./style.css";
4343

44-
const __CSP__ = false;
45-
const wcRegistered = __CSP__ ? false : register();
44+
const wcRegistered = register();
4645

4746
export const THEME_KEY: InjectionKey<ThemeInjection> = Symbol();
4847
export const INIT_OPTIONS_KEY: InjectionKey<InitOptionsInjection> = Symbol();

src/wc.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ export function register(): boolean {
1919
}
2020

2121
try {
22-
// Class definitions cannot be transpiled to ES5
23-
// so we are doing a little trick here to ensure
24-
// we are using native classes. As we use this as
25-
// a progressive enhancement, it will be fine even
26-
// if the browser doesn't support native classes.
27-
const reg = new Function(
28-
"tag",
29-
// Use esbuild repl to keep build process simple
30-
// https://esbuild.github.io/try/#dAAwLjIzLjAALS1taW5pZnkAY2xhc3MgRUNoYXJ0c0VsZW1lbnQgZXh0ZW5kcyBIVE1MRWxlbWVudCB7CiAgX19kaXNwb3NlID0gbnVsbDsKCiAgZGlzY29ubmVjdGVkQ2FsbGJhY2soKSB7CiAgICBpZiAodGhpcy5fX2Rpc3Bvc2UpIHsKICAgICAgdGhpcy5fX2Rpc3Bvc2UoKTsKICAgICAgdGhpcy5fX2Rpc3Bvc2UgPSBudWxsOwogICAgfQogIH0KfQoKaWYgKGN1c3RvbUVsZW1lbnRzLmdldCh0YWcpID09IG51bGwpIHsKICBjdXN0b21FbGVtZW50cy5kZWZpbmUodGFnLCBFQ2hhcnRzRWxlbWVudCk7Cn0K
31-
"class EChartsElement extends HTMLElement{__dispose=null;disconnectedCallback(){this.__dispose&&(this.__dispose(),this.__dispose=null)}}customElements.get(tag)==null&&customElements.define(tag,EChartsElement);",
32-
);
33-
reg(TAG_NAME);
22+
class ECElement extends HTMLElement implements EChartsElement {
23+
__dispose: (() => void) | null = null;
24+
25+
disconnectedCallback() {
26+
if (this.__dispose) {
27+
this.__dispose();
28+
this.__dispose = null;
29+
}
30+
}
31+
}
32+
if (customElements.get(TAG_NAME) == null) {
33+
customElements.define(TAG_NAME, ECElement);
34+
}
3435
} catch {
3536
return (registered = false);
3637
}

0 commit comments

Comments
 (0)