npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-import

v1.0.1

Published

[![NPM](https://nodei.co/npm/nw-autoupdater.png)](https://nodei.co/npm/html-import/)

Downloads

192

Readme

HTML Import

NPM

Sometimes when working on a big HTML we need to split it in blocks. For example I have a file structure where every folder represents a component keeping its HTML and assets. In the landing page of style guide I would like to include all the components. Well, in CSS we can import a module with @import, in JavaScript we can use import. In the past we applied Apache Server-Side Includes for importing HTML blocks. Now we can think of HTML Imports, but they are not yet really supported even in ever-green browsers. Anyway, it inspired me on writing a little library that implements a similar approach.

As soon as you load it on the page

<script async src="./src/html-import.js"></script>

If you intend to use the library with legacy browsers examine legacy.html that contains all the required polifills.

The library subscribes for DOM-ready event and processes the DOM the directives like that:

index.html

<link rel="html-import" href="./some-path/block.html" >

When any encountered it loads the HTML file specified in href attribute and replaces the directive with the loaded HTML. If it finds any imports in the loaded HTML it processes it recursively

./some-path/block.html

<link rel="html-import" href="./some-other-path/other-block.html" >

If you want to import a block more than once, you can declare it with repeat attribute:

<link rel="html-import" href="./some-path/block.html" repeat="5">

Running the demo

npm install
npm start

API

The library bundled as UMD meaning you access it as AMD (RequireJS) modules, as CommonJS module or in global variable `HTMLImport.

"html-imports-loaded" DOM event

It is fired when HTML Import finishes DOM processing (one started by its own on DOM ready event)

  <script>
    document.addEventListener( "html-imports-loaded", ( e ) => {
      e.detail.urls.forEach( url => console.log( `Loaded ${url}` ) );
    });
  </script>

HTMLImport.import()

Can be used to start manually processing the DOM

@returns {Promise}

HTMLImport.importForElements( arr )

Processes the elements of given array

@param {Node[]} imports
@returns {Promise}

HTMLImport.loadJs( scriptPath )

Loads JavaScript

@param {string} scriptPath
@returns {Promise}

Example:

 <script>
    document.addEventListener( "html-imports-loaded", ( e ) => {
      e.detail.urls.forEach( url => console.log( `Loaded ${url}` ) );
      HTMLImport.loadJs( "/assets/js/backbone/backbone.min.js" )
        .then(() => console.log( "Loaded /assets/js/backbone/backbone.min.js" ) )
        .then(() => HTMLImport.loadJs( "/assets/js/app.js" ) )
        .then(() => console.log( "Loaded /assets/js/app.js" ) );
    });
  </script>