
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
custom-element-import
Advanced tools
Imports (native) web components, when scrolled into view. This way, only scripts are loaded that are really needed, which saves data to be transferred and improves performance.
Imports (native) web components, based on custom elements only, when scrolled into view. This way, only scripts are loaded that are really needed, which saves data to be transferred and improves performance.
Custom elements can be set up in your original source code or be added dynamically. Loading of a custom element, placed inside of another web component will only be working if it is using light DOM, elements inside of shadow DOM won't.
Place your (first) web component in a public directory components inside of a directory with the same name as of the web component relative to the current url or pick a different name if you want, so:
components, to be available on top levelblendy-birdblendy-bird.jsBlendyBirdcustomElements.define instruction to register your class as custom element with the same name as used for the file name, e.g. blendy-birdIn a very basic version, your blendy-bird/blendy-bird.js could look like this:
class BlendyBird extends HTMLElement {
static get tagName() {
return "blendy-bird";
}
constructor() {
super();
this.shadow = this.attachShadow({mode: "open"});
this.shadow.innerHTML = `<h3>Blendy Bird</h3>`;
}
}
customElements.define(BlendyBird.tagName, BlendyBird);
Now you are ready to setup the rendering.
Install as dependency first:
npm install --save custom-element-import
Import custom-element-import and call it to start the rendering with web components in a default directory ./components relative to the current url:
import customElementImport from 'custom-element-import';
customElementImport();
To specify another directory where your web components have been placed, provide a config with a param dir:
import { customElementImport } from 'custom-element-import';
customElementImport({ dir: "../components", log: true });
It's also possible to render present DOM only as it is on runtime by calling parseDOM.
Calling observeDOM starts a watcher to observe DOM changes to render web components being added to the DOM dynamically. Added components won't be loaded until they are in the viewport.
As with customElementImport use an option configuration to specify a non default folder for your web components.
import { parseDOM, observeDOM } from 'custom-element-import';
document.addEventListener('DOMContentLoaded', () => {
// renders custom elements in DOM at runtime
parseDOM({ dir: "../components", log: true });
// watches for DOM changes
observeDOM({ dir: "../components", log: true });
});
Given param log is set, it will log to console which component will be loaded as well as the expected location.
custom-element-import can be added from CDN. The minified version will not include logs.
<script src="https://unpkg.com/custom-element-import@latest/dist/ce-import.min.js" crossorigin></script>
Use the non minified version to see logs.
<script src="https://unpkg.com/custom-element-import@latest/dist/ce-import.js" crossorigin></script>
It's possible to use a copy of the script from dist folder in your project. Make sure to place your web components in a directory called components.
Load script from dist/ce-import.js or a minified version from dist/ce-import.min.js in your document header. To optimize your performance, also consider to setup the content of the minified version into an inline script element.
Don't forget to provide every web component by a separate file in components/[custom-element].js as described above.
At least put your custom element into your HTML.
Custom elements can have a specific attribute ce-import to define its loading behaviour, by setting one of these values:
auto (default) to request web component, when it will be scrolled into view.true to force the request immediately.false to not request a JS resource, which might make sense for web components using declarative shadow DOM.interact to load a JS resource on user interaction, e.g. on click.<blendy-bird ce-import="true"></blendy-bird>
Alternatively, there are short-hand directives to adjust the control of the imports. Use
@ignore to ignore the loading@interact to request the web component only on user interaction, e.g. on click@immediate to force to load the web component immediatelyExamples:
<blendy-bird @ignore></blendy-bird>
<fragmented-frog @interact></fragmented-frog>
<dirty-dog @immediate></dirty-dog>
@ignore could be used to ignore the loading.
<blendy-bird @ignore></blendy-bird>
<fragmented-frog @interact></fragmented-frog>
The minified dist version of custom-element-import uses this uglifier: https://skalman.github.io/UglifyJS-online/
FAQs
Imports (native) web components, when scrolled into view. This way, only scripts are loaded that are really needed, which saves data to be transferred and improves performance.
We found that custom-element-import demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.