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

@polymer/esm-amd-loader

v1.0.4

Published

Minimal AMD-style loader for replicating ES module behavior.

Downloads

17,822

Readme

NPM version

@polymer/esm-amd-loader

A JavaScript library which loads AMD-style modules in the browser in 1.4 KB.

Contents

Installation

If you are using Polymer CLI 1.7.0 or above, then no separate installation is needed. Polymer CLI will automatically transform your project to AMD modules using the Babel AMD transform plugin, and inject this loader into your HTML document.

For other use cases, this loader can be installed directly from NPM:

$ npm install --save @polymer/esm-amd-loader

Example usage

This loader is primarily intended to be used as the browser runtime component of an automatic transformation of a project from ES modules to AMD modules, as performed by tools like the Babel AMD transform plugin or TypeScript AMD generation. It is not expected that users would typically author code directly for this API.

index.html

<script src="./node_modules/@polymer/esm-amd-loader/lib/esm-amd-loader.min.js"></script>

<script>
  define(['./foo.js'], function(foo) {
    console.log('imported', foo.stuff, 'from foo.js');
  });
</script>

foo.js

define(['exports', 'require', 'meta'], function(exports, require, meta) {
  exports.stuff = 'neat stuff';

  require(['../bar.js'],
    function(bar) {
      console.log(meta.url, 'dynamically loaded bar.js:', bar);
    },
    function(error) {
      console.log(meta.url, 'failed to dynamically load bar.js:', error);
    });
});

window.define

window.define = function(
    dependencies: string[],
    moduleBody?: (...args: Array<{}>) => void

dependencies

An array of module paths, relative or absolute, which are dependencies of this module. Relative paths are resolved relative to the location of this module. Can also be one of the special dependencies listed below.

Dependencies are run in the same deterministic order as they would if they were ES modules.

moduleBody

A function which is invoked when all dependencies have resolved. The exports of each dependency is passed as an argument to this function, in the same order that they were specified in the dependencies array.

If any dependencies do not load (e.g. 404), or if their module bodies throw an error nothing later in the dependency graph will execute, and an Error will be thrown up to the window error event.

Special dependencies

"exports"

The exports object for this module. If another module depends on this module, this is the object that will be received.

"require"

function require(
    dependencies: string[],
    onResolve?: (...args: Array<{}>),
    onError?: (error: Error)) => void

A function which will load the given dependencies, with relative paths resolved relative to the current module. If successful, onResolve is called with the resolved dependencies. If a dependency fails to load, onError is called with the error from the first dependency which failed.

"meta"

A {url: string} object, where url is the fully qualified URL of this module. Corresponds to an ES module's import.meta.

Differences from AMD/RequireJS

  • Minified and compressed size is 1.4 KB, vs 6.6 KB for RequireJS.

  • Only supports specifying dependencies as paths, and does not support explicitly naming modules.

  • Does not include a global require function. Instead, modules created with define always execute immediately. RequireJS executes require calls immediately, but only executes define modules if they are a transitive dependency of a require call, or if they are named by the data-name bootstrap attribute.

  • Modules always resolve to an exports object, even if the module did not request it or assign any properties to it. RequireJS modules will resolve to undefined if the module did not request its exports object.

  • AMD does not specify the meta object. It does specify a similar object called module, which can contain id and uri. RequireJS provides module and sets uri to a path relative to the HTML document's base URL.

  • RequireJS contains a bug whereby relative path resolution for modules above the HTML document base URL can result in duplicate requests for the same module.

  • Module execution order happens according to the ES spec, including support for cyclical dependencies.

  • Top level define calls are also ordered, similar to the way that multiple <script type="module"> tags in an HTML document are.