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

esm-loader

v0.1.0

Published

A ESM Loader for NodeJS and Browsers Allows to import modules from any source is extendible via modules.

Downloads

1,249

Readme

esm-loader

Allows to load esm from any file formart can be seen as loader construction kit while offering precoded methods to do generic loader stuff in cross environment applications. It also can be seen as Pollyfill for importScript and dynamic import from url for NodeJS.

CHANGELOG

breaking change is deprecating strToESM its successor is ESMLoader and moduleString

Usage

Host a local or remote server with scripts or use https://unpkg directly inside nodeJS via "esm-loader" The Steps to form a Module Instance with livebindings!

  • Getting the Module Code
  • Optional Transform it
  • Construction Instantiation Evaluation via ESMLoader

Examples

String to ESModule Examples Cross Environment

import { ESMLoader } from 'esm-loader/loader.mjs'

const module = ESMLoader('export const myModule = { yellow: \'snowflakes\'}')
    .then(({ myModule })=>console.log(myModule.yellow)) // Logs: snowflakes
import { moduleString } from 'esm-loader/loader.mjs'

const module = import(moduleString('export const myModule = { yellow: \'snowflakes\'}'))
    .then(({ myModule })=>console.log(myModule.yellow)) // Logs: snowflakes

Inside NodeJS with modules support

ESMLoader used inside NodeJS supports NodeJS Module Resolve but not relative resolve you can support it via a transformation step after loading your code you can simply replace any ./ ../ urls with correct resolved values relative to your current loaded module or the module loading path of the scipt you plan to instantiate and evaluate to ESM via ESMLoader.

ESMLoader ships with a example method to get code from a remote file via a isomorpic fetch This allows to use import with dynamic content.

// The exported fetch is cross environment Nodejs and Browser
import { ESMLoader, fetch ,fetchImport, dynamicImport } from 'esm-loader/loader.mjs'

// Creates a dynamic import from a string
// Returns a ESM Module with exports from string has resovle for Bare and Absolut Specifiers
// can easy be rewritten for your own extensions
fetchImport('url') //is a shortHand helper for fetch('https://url.to/your/js.mjs').then(ESMLoader)
    .then(mod=>console.log(mod))

dynamicImport('url') // uses import in the browser and fetchImport in nodejs

CJS

const fs = require('fs')
//ESM-LOADER is ES so you need to use dynamic import
import('esm-loader/esm-loader.mjs') // NodeJS Resolve used here
  .then(({ ESMLoader })=>fs.promises.readFile('path/to/file.txt,ts,any').then(ESMLoader)) // NodeJS Resolve used here 
  //no relativ urls can use transformations befor then(ESMLoader)
  .then(myModule=>console.log(myModule)) //=> returns a real ESModule

inside browser for custom elements or scripts

import { ESMLoader, dynamicImport } from 'esm-loader/loader.mjs'

// Creates a dynamic import from a string

// can easy be rewritten for your own extensions
fetch('https://url.to/your/js.mjs')
    .then(ESMLoader)
    /**
     *  Returns a ESM Module with exports from string 
     *  has resolve for Bare and Absolut Specifiers
     *  Your Can Easy rewrite the String that you get from 
     *  fetch befor you use it as input to ESMLoader.
     */
    .then(mod=>console.log(mod))

dynamicImport('url') // uses import in the browser and fetchImport in nodejs

rollup support landed

https://github.com/rollup/plugins/pull/150

Advanced Stuff

Lets Import a mjs file from a source and rewrite it to resolve to the right locations if relativ specifiers are used

// TODO:
  • https://github.com/guybedford/es-module-lexer used with webworkers is powerfull.
    • can be used to build webworker integrations eg moduleString transformed for webworkers
    • setup a proxy object based on the lexer result in main and run it with ESMLoader while hand of a transformed webworker version to new Worker()