Skip to content

Commit

Permalink
fix: don't define dependencies in the constructor. add version variab…
Browse files Browse the repository at this point in the history
…le for better comparison


chore: adjust eslint config to match the v8 specs:
  • Loading branch information
daenub authored Jul 16, 2024
1 parent da68c5e commit 14eda96
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"no-console": "off"
}
}
]
],
"globals": {
"__LEU_VERSION__": "readonly"
}
}
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-replace": "^5.0.7",
"@rollup/plugin-typescript": "^11.1.6",
"@storybook/addon-designs": "^7.0.9",
"@storybook/addon-essentials": "^7.6.17",
Expand Down
9 changes: 9 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import { fileURLToPath } from "url"
import postcss from "rollup-plugin-postcss"
import postcssLit from "rollup-plugin-postcss-lit"
import { babel } from "@rollup/plugin-babel"
import replace from "@rollup/plugin-replace"

export const plugins = [
{
plugin: replace,
args: [
{
__LEU_VERSION__: JSON.stringify(process.env.npm_package_version),
},
],
},
{
plugin: postcss,
args: [
Expand Down
30 changes: 19 additions & 11 deletions src/lib/LeuElement.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { LitElement } from "lit"

export class LeuElement extends LitElement {
static version = __LEU_VERSION__

static dependencies = {}

static define(name, constructor = this, options = {}) {
if (!customElements.get(name)) {
Object.entries(this.dependencies).forEach(([n, c]) => c.define(n))

const currentlyRegisteredConstructor = customElements.get(name)

if (currentlyRegisteredConstructor === undefined) {
customElements.define(name, constructor, options)
} else {
console.info(`${name} is already defined`)
return
}
}

constructor() {
super()
if (currentlyRegisteredConstructor !== constructor) {
console.warn(
`The custom element with the name <${name}> is already registered with a different constructor. This can happen when the same element has been loaded from different modules (e.g. multiple CDN requests or bundles).`
)
return
}

Object.entries(this.constructor.dependencies).forEach(
([name, component]) => {
this.constructor.define(name, component)
}
)
if (currentlyRegisteredConstructor.version !== constructor.version) {
console.warn(
`The custom element with the name <${name}> is already defined with the same constructor but a different version (${currentlyRegisteredConstructor.version}).`
)
}
}
}

0 comments on commit 14eda96

Please sign in to comment.