From 001cc1ae323fee38fc803652350c17e0831ddb64 Mon Sep 17 00:00:00 2001 From: Yue JIN <40021217+kingyue737@users.noreply.github.com> Date: Sun, 1 Jun 2025 17:43:06 +0800 Subject: [PATCH] refactor: use Web Components without native class support detection (#836) --- package.json | 1 - pnpm-lock.yaml | 19 ------------------- rollup.config.js | 2 -- src/ECharts.ts | 3 +-- src/wc.ts | 25 +++++++++++++------------ 5 files changed, 14 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 7748e0e..e11bc79 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ }, "devDependencies": { "@highlightjs/vue-plugin": "^2.1.0", - "@rollup/plugin-replace": "^6.0.2", "@types/node": "^22.15.21", "@typescript-eslint/utils": "^8.32.1", "@vercel/analytics": "^1.3.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ba0a282..192fa13 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,9 +11,6 @@ importers: '@highlightjs/vue-plugin': specifier: ^2.1.0 version: 2.1.0(highlight.js@11.10.0)(vue@3.5.13(typescript@5.8.3)) - '@rollup/plugin-replace': - specifier: ^6.0.2 - version: 6.0.2(rollup@4.41.1) '@types/node': specifier: ^22.15.21 version: 22.15.21 @@ -395,15 +392,6 @@ packages: resolution: {integrity: sha512-S+9ANAvUmjutrshV4jZjaiG8XQyuJIZ8a4utWmN/vW1sgQ9IfBnPndwkmQYw53QmouOIytT874u65HEmu6H5jw==} engines: {node: '>=18'} - '@rollup/plugin-replace@6.0.2': - resolution: {integrity: sha512-7QaYCf8bqF04dOy7w/eHmJeNExxTYwvKAmlSAH/EaWWUzbT0h5sbF6bktFoX/0F/0qwng5/dWFMyf3gzaM8DsQ==} - engines: {node: '>=14.0.0'} - peerDependencies: - rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 - peerDependenciesMeta: - rollup: - optional: true - '@rollup/pluginutils@5.1.4': resolution: {integrity: sha512-USm05zrsFxYLPdWWq+K3STlWiT/3ELn3RcV5hJMghpeAIhxfsUIg6mt12CBJBInWMV4VneoV7SfGv8xIwo2qNQ==} engines: {node: '>=14.0.0'} @@ -1739,13 +1727,6 @@ snapshots: '@publint/pack@0.1.2': {} - '@rollup/plugin-replace@6.0.2(rollup@4.41.1)': - dependencies: - '@rollup/pluginutils': 5.1.4(rollup@4.41.1) - magic-string: 0.30.17 - optionalDependencies: - rollup: 4.41.1 - '@rollup/pluginutils@5.1.4(rollup@4.41.1)': dependencies: '@types/estree': 1.0.7 diff --git a/rollup.config.js b/rollup.config.js index 0128e24..6f8d507 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,3 @@ -import replace from "@rollup/plugin-replace"; import esbuild from "rollup-plugin-esbuild"; import { dts } from "rollup-plugin-dts"; import css from "rollup-plugin-import-css"; @@ -15,7 +14,6 @@ function configBuild(options, csp) { const { plugins, output } = result; result.plugins = [ - ...(csp ? [replace({ __CSP__: `${csp}`, preventAssignment: true })] : []), ...plugins, css({ ...(csp ? { output: "style.css" } : { inject: true }), diff --git a/src/ECharts.ts b/src/ECharts.ts index 137116d..66e2646 100644 --- a/src/ECharts.ts +++ b/src/ECharts.ts @@ -41,8 +41,7 @@ import type { EChartsElement } from "./wc"; import "./style.css"; -const __CSP__ = false; -const wcRegistered = __CSP__ ? false : register(); +const wcRegistered = register(); export const THEME_KEY: InjectionKey = Symbol(); export const INIT_OPTIONS_KEY: InjectionKey = Symbol(); diff --git a/src/wc.ts b/src/wc.ts index f388727..424397c 100644 --- a/src/wc.ts +++ b/src/wc.ts @@ -19,18 +19,19 @@ export function register(): boolean { } try { - // Class definitions cannot be transpiled to ES5 - // so we are doing a little trick here to ensure - // we are using native classes. As we use this as - // a progressive enhancement, it will be fine even - // if the browser doesn't support native classes. - const reg = new Function( - "tag", - // Use esbuild repl to keep build process simple - // https://esbuild.github.io/try/#dAAwLjIzLjAALS1taW5pZnkAY2xhc3MgRUNoYXJ0c0VsZW1lbnQgZXh0ZW5kcyBIVE1MRWxlbWVudCB7CiAgX19kaXNwb3NlID0gbnVsbDsKCiAgZGlzY29ubmVjdGVkQ2FsbGJhY2soKSB7CiAgICBpZiAodGhpcy5fX2Rpc3Bvc2UpIHsKICAgICAgdGhpcy5fX2Rpc3Bvc2UoKTsKICAgICAgdGhpcy5fX2Rpc3Bvc2UgPSBudWxsOwogICAgfQogIH0KfQoKaWYgKGN1c3RvbUVsZW1lbnRzLmdldCh0YWcpID09IG51bGwpIHsKICBjdXN0b21FbGVtZW50cy5kZWZpbmUodGFnLCBFQ2hhcnRzRWxlbWVudCk7Cn0K - "class EChartsElement extends HTMLElement{__dispose=null;disconnectedCallback(){this.__dispose&&(this.__dispose(),this.__dispose=null)}}customElements.get(tag)==null&&customElements.define(tag,EChartsElement);", - ); - reg(TAG_NAME); + class ECElement extends HTMLElement implements EChartsElement { + __dispose: (() => void) | null = null; + + disconnectedCallback() { + if (this.__dispose) { + this.__dispose(); + this.__dispose = null; + } + } + } + if (customElements.get(TAG_NAME) == null) { + customElements.define(TAG_NAME, ECElement); + } } catch { return (registered = false); }