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

express-transform-bare-module-specifiers

v1.0.4

Published

Express middleware to transform bare module specifiers on-the-fly.

Downloads

1,637

Readme

express-transform-bare-module-specifiers Build Status codecov

Express middleware to transform bare module specifiers on-the-fly.

Usage

  1. Install the middleware:

    npm i express-transform-bare-module-specifiers
  2. Import (or require) this package:

    // ES Modules
    import transformMiddleware from 'express-transform-bare-module-specifiers';
    
    // CommonJS
    const transformMiddleware = require('express-transform-bare-module-specifiers').default;
  3. Configure and apply the middleware:

    // Using defaults:
    app.use('*', transformMiddleware());
       
    // Using a custom rootDir and modulesUrl:
    app.use('*', transformMiddleware({
    	rootDir: path.resolve(__dirname, '/bundles/my-bundle'),
    	modulesUrl: '/bundles/my-bundle/node_modules'
    }))
  • rootDir: the project base directory. This should contain the package.json and node_modules of the application. It defaults to process.cwd().
  • modulesUrl: is the route that you will be serving your node_modules directory from. It defaults to /node_modules.

Motivation

ES Modules are great. However, it can be difficult to incorporate existing npm packages, because you have to specify the fully-qualified path to the entrypoint of each and every npm package you wish to use. That is to say: you can't do this:

import * as noop from 'noop3';

... you instead must do this (for example):

import * as noop from '../node_modules/noop3/index.js';

You can see how this would rapidly become very hard to maintain.

This limitation is present because the ES Modules spec currently does not support so-called "bare module specifiers". That is: any module specifier which does not start with a relative or absolute path, such as /, ./, ../, etc.

This middleware uses a single babel transform to convert these "bare module specifiers" in your code to fully-qualified relative paths. This means that you can just write code which references npm packages installed in your node_modules, and this middleware will handle translating those package names to fully-qualified paths on-the-fly.

Acknowledgements

This middleware is based entirely on the implementation found in polyserve. Except, it uses the babel-plugin-bare-import-rewrite babel plugin instead of the one built into polymer-build.