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

import-mapper

v0.0.4

Published

Map bundled classes to dynamic imports.

Downloads

5

Readme

import-mapper

Inject require()'d modules into dynamic imports()

import-mapper allows you to inject pretty much anything into dynamically import()ed modules.

Please report issues to https://github.com/seanmorris/import-mapper/issues

install

Install with npm:

$ npm install import-mapper

Or, include with unpkg:

<script src = "https://unpkg.com/commonjs-require-definition/require.js"></script>
<script src = "https://unpkg.com/import-mapper/dist/ImportMapper.js"></script>

Usage

Basic

Let's say you've got the following module, Bar.mjs. It exports one class, Bar, which extends Foo from module @foo/Foo.

Bar.mjs

import { Foo } from '@foo/Foo';

export class Bar extends Foo{}

The Foo class is brought into Bar via an ESM import {...}. However, the Foo class already exists in our namespace, possibly brought in via require().

The objective here is to inject the existing @foo/Foo, and prevent the need to pull the module via HTTP if it already exists in our bundled script.

Now, in main.js, we can create an ImportMapper to inject @foo/Foo into Bar.mjs:

main.js

const { ImportMapper } = require('import-mapper');

// pretend this came from elsewhere
class Foo{};

// Create the ImportMapper.
const importMapper = new ImportMapper({
    '@foo/Foo': { Foo } // "@foo/Foo" has one export: "Foo".
});

// Register it.
importMapper.register();

// Import Bar.
import('/Bar.mjs').then( module => {

    // Get the class...
    const { Bar } = module;

    // Create the instance
    const barInstance = new Bar;

    // Check that Bar extends Foo
    console.log(barInstance instanceof Foo);
});

Use with require()

Longhand

If the module @foo/Foo is available via require(), then we can declare our ImportMapper like so:

const importMapper = new ImportMapper({
    '@foo/Foo': require('@foo/Foo')
});

Shorthand

If require() is available in the global scope, and you're only importing stuff from require(), then you can just pass an array of module names:

const importMapper = new ImportMapper( ['@foo/Foo'] );

Short(er)hand

If your bundler is awesome like brunch is, then you can just call require.list() to get an array of all available module names:

const importMapper = new ImportMapper( globalThis.require.list() );

This will map all modules available to require() to dynamic imports.

Scalars

Bare scalar values will be returned as the default export of a module:

const importMapper = new ImportMapper({
    'someString': 'this is a string'
});

importMapper.register();

import('someString').then(module => console.log(module.default));

Wrap the scalar in an object if you need a named export:

const importMapper = new ImportMapper({
    'someString': {someString: 'this is a string'}
});

importMapper.register();

import('someString').then(module => console.log(module.someString));

Multiple Exports

You can pass an object with multiple keys to get a module with multiple exports.

// pretend this came from elsewhere
class Foo{};
class Baz{};

// Create the ImportMapper.
const importMapper = new ImportMapper({
    '@foo/Foo': { Foo, Baz } // "@foo/Foo" has two exports: "Foo" and "Baz".
});

Exporting a Default Object

Astute readers will notice that the above notation does not allow for the export of a default object. This is because the keys of the top level object will always be used as the named exports for the injected module.

const importMapper = new ImportMapper({
    'someObject': {label: 'this is an object'}
});

importMapper.register();

import('someObject').then(module => console.log(module.label));

Pass the module content into the static method ImportMapper.forceDefault() and use the return value to get this behavior:

const importMapper = new ImportMapper({
    'someObject': ImportMapper.forceDefault({label: 'this is an object'})
});

importMapper.register();

import('someObject').then(module => console.log(module.default));

Methods

ImportMapper.constructor(modules)

Create a new ImportMapper.

Parameters

  • modules - A list of modules to inject

    May be one of the following:

    • An object, keyed by module name, each value is an object where the keys are the module's exports.
    • An array of module names to require() and map automatically.

Returns

A newly constructed ImportMapper object

ImportMapper.register()

Register the imports.

Parameters

none

Returns

none

ImportMapper.forceDefault(object)

Static method, returns an object that will be treated as the default export from the module.

Parameters

  • object - The object to use as the default export

Returns

Returns a wrapped module object.