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

nomodule

v0.0.18

Published

Enhance script tag functionality for modules.

Downloads

56

Readme

no-module

Purpose

ES6 modules are a great leap forward over the legacy JavaScript browsers support. But there are two gaps where legacy JavaScript provided some advantages.

Referencing outside the script tag.

JS references, pre-ES6 modules, allowed for global variables / functions, which in turn saw such libraries as JQuery thrive. jQuery consumers could just assume $ was ever present and ready to serve the developer.

Over time, this aspect of JavaScript came to be viewed more as a bug than a feature, leading to such initiatives as require.js and eventually ES6 modules.

On the other hand, ES6 modules provide export capabilities, but the keyword "export" symbol becomes meaningless in the context of a script tag:

<script type=module>
export const h = 'hello'; // nothing outside this module can access h, and the export keyword is meaningless.
</script>

Access to the script tag instance.

Another important feature ES6 modules lost that "legacy" script tags supported was access to the script tag from which the script derived - document.currentScript.

no-module provides some support to overcome these limitations.

Syntax

nomodule.js provides a mechanism where the exported symbols can be accessed. Script tags must have attribute nomodule=ish:

<script nomodule=ish id=myScriptTag>
export const h = 'hello';
throw 'new Error'; 
</script>

<script>
    myScriptTag.addEventListener('loaded', e =>{
        console.log(myScriptTag._modExport);
        // { h: "hello" }
    });
    myScriptTag.addEventListener('err', e => {
        console.log(e.detail.message);
        // "new Error"
    })
</script>

NBs:

  1. The "nomodule" attribute tells modern browsers to ignore the tag, so there is no wasted CPU processing something that isn't fully complete.

  2. This library, no-module.js, then, can inject some special instructions to produce the desired effects. But the JS processor which is used after the special instructions are inserted is in fact the ES6 (module) processor. So ES6 imports are allowed, for example (though support for import maps will require turning on the Chrome flag for import maps, or using a polyfill, like es-module-shim or es-dev-server).

  3. The pattern matching is precise / inflexible, and is limited to "export[space]const[space][symbol to export]".

no-module.js also works for script references to ES6 modules, i.e.:

<script nomodule=ish src=myModule.js></script>

document.currentScript replacement

To access the script tag which references or contains the script, use the magic string: "selfish":

const scriptTag = selfish;
console.log(scriptTag);
//<script nomodule="ish" src="test.js" id="myScriptTag" data-found="true" data-loaded="true"></script>

NB: Greedy code will break.

Simply including a reference to no-module.js will allow any "nomodule=ish" script tags outside any shadow DOM to load as described above.

To achieve the same behavior within a shadow DOM realm, include a single no-module tag somewhere:

<my-custom-element>
    #shadow
        ...
        <script nomodule=ish src=myModule.js></script>
        ...
        <script nomodule=ish src=yourModule.js></script>
        <no-module></no-module>
</my-custom-element>

** Working like it's '95 [TODO]

<div on-click="myScript.yawnAndStretch()">Tumble out of bed</div>
...
<script nomodule=ish id=myScript>
export function yawnAndStretch(){
    event.target.textContent = 'Try to come to life';
}
</script>