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

babel-plugin-proxy-import

v1.0.4

Published

Transform any member style `import { foo as bar } from 'module'` to default direct style `import bar from '{any}/foo'` with ease to enable tree shaking with any module.

Downloads

8

Readme

babel-plugin-proxy-import

Transform any member style import { foo as bar } from 'module' to default direct style import bar from '{any}/foo' with ease to enable direct submodule importing in any module.

Motivation

Tree shaking is awesome, but most library does not directly support it yet and this plugin is made to enable tree shaking on any library that we need.

But its not just performance, this library is made to improve the developer experience of big projects that has many modules that may be confusing to read due to the big overhead in learning the project structure and the bad discoverability of the right module for the job. This module is aimed to give a simple singular member-style import statement (import { a } from 'b') that will be proxied/transformed to multiple default style direct import (import a from 'b/a') to bridge the ease of development and performance issue when importing a single module.

Setup

  npm install --save-dev babel-plugin-proxy-import

Simply install this package as a devDependency to your babel app and use it as a plugin on your babel configuration.

Configuration

Pass down an option when using this plugin on your babel configuration as such:

  {
    plugins: [
      ...
      [
        'babel-plugin-proxy-import',
        {
          rules: [
            {
              ...proxyConfiguration
            },
          ],
        }
      ],
      ...
    ],
  }

this plugin only accepts an array of proxy configuration objects as an options and will throw an error when given a bad option. (refer to src/lib/validate.js for clarity)

Proxy Configuration

To configure a proxy for your module, simply add an object with the shape:

  {
    // your target module name to be replaced, this is required
    module: 'my-module',
    // template of your target module import location, must contain ${target} that will be replaced with the import target,
    // must not have / before ${target}
    // this example proxy will transform import a from 'my-module' to import a from '@myModule.container' and
    // import { a } from 'my-module' to import a from '@myModule/a.container', refer to src/lib/resolveBaseTargetModule.js for clarity
    proxy: '@myModule${target}.container',
    // this option will determine wether target module location should be transformed to one of the three case structure
    // e.g. import { a_module } from '@myLib' to import a_module from '@myLib/aModule', will default to camel case
    targetCase: 'camel' || 'snake' || 'kebab',
    // this config will determine wether outright full import of your module is not allowed or not
    // i.e. import a from 'your-module' or import * as a from 'your-module'
    blockFullImport: true,
    // this config will enable specific file targeting instead of the root folder targeting when importing a module
    // i.e. import { a } from 'your-module' to import a from 'your-module/a/a' instead of 'your-module/a'
    // this is useful when your project doesnt use index.js as the root of your module
    noIndex: true,
  }

Escape Hatch for noIndex option

Wait, what if i want to import another file inside of a folder of my module, i.e. i want to target module B inside folder A that also contains module A? Well, for this specific utility you can use an escape hatch FOLDER___MODULE in your import, preferably you would alias that as such:

import { MyFolder_MyModule as myModule } from '@myLib'

that import line will be transformed to:

import myModule from '@myLib/MyFolder/MyModule

of course when a target case configuration is given, it will respect that, the example above is just using the default camel case configuration.

Credit

This plugin is heavily inspired by the babel-plugin-transform-imports.