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

@pitrix/babel-plugin-lego-imports

v0.0.7

Published

Transforms member style imports (import {x} from 'y') into default style imports (import x from 'y/lib/x') for lego-ui

Downloads

11

Readme

babel-plugin-lego-imports

Transforms member style imports:

import { Icon, Button as Btn } from '@QCFE/lego-ui';

...into default style imports:

import Icon from '@QCFE/lego-ui/lib/components/Icon/Icon.js';
import Btn from '@QCFE/lego-ui/lib/components/Button/Button.js';

Note: this plugin is not restricted to the lego-ui. You may use it with any library.

That's stupid, why would you do that?

When Babel encounters a member style import such as:

import { Icon, Button } from '@QCFE/lego-ui';

it will generate something similarish to:

var legoUI = require('@QCFE/lego-ui');
var Icon = legoUI.Icon;
var Button = legoUI.Button;

And, the more pieces we need, the more this sucks. This plugin will allow you to pull in just the pieces you need, without a separate import for each item. Additionally, it can be configured to throw when somebody accidentally writes an import which would cause the entire module to resolve, such as:

import Bootstrap, { Grid } from 'react-bootstrap';
// -- or --
import * as Bootstrap from 'react-bootstrap';

Installation

npm install --save-dev @QCFE/babel-plugin-lego-imports

Usage

In .babelrc:

{
    "plugins": [
        ["@QCFE/babel-plugin-lego-imports", {
            "@QCFE/lego-ui": {
                "transform": "defaultRule",
                "preventFullImport": true
            }
        }]
    ]
}

The default rule is a JavaScript config file, within the lego-ui repository.

Advanced Transformations

Using regular expressions

Sometimes you may enforce the same convention in all folder levels on the structure of your libraries. For achieving this dynamism, you may use regexp to cover all tranformations.

.babelrc:

{
    "plugins": [
        ["@QCFE/babel-plugin-lego-imports", {
            "my-library\/?(((\\w*)?\/?)*)": {
                "transform": "my-library/${1}/${member}",
                "preventFullImport": true
            }
        }]
    ]
}

For instance, the previous configuration will solve properly the next scenarios:

import { MyModule } from 'my-library';
import { App } from 'my-library/components';
import { Header, Footer } from 'my-library/components/App';

becomes:

import MyModule from 'my-library/MyModule';
import App from 'my-library/components/App';
import Header from 'my-library/components/App/Header';
import Footer from 'my-library/components/App/Footer';

Using a transformation file

If you need more advanced transformation logic, you may provide a path to a .js file which exports a function to run instead. Keep in mind that the .js file will be required relative from this plugin's path, likely located in /node_modules/babel-plugin-transform-imports. You may provide any filename, as long as it ends with .js.

.babelrc:

{
    "plugins": [
        ["@QCFE/babel-plugin-lego-imports", {
            "my-library": {
                "transform": "../../path/to/transform.js",
                "preventFullImport": true
            }
        }]
    ]
}

/path/to/transform.js:

module.exports = function(importName, matches) {
    return {
        importResultName: 'my-library/etc/' + importName.toUpperCase(),
        skipDefaultConversion: false,
    };
};

This is a little bit hacky, but options are a bit limited due to .babelrc being a JSON5 file which does not support functions as a type. In Babel 7.0, it appears .babelrc.js files will be supported, at which point this plugin will be updated to allow transform functions directly in the configuration file. See: https://github.com/babel/babel/pull/4892

Webpack

This can be used as a plugin with babel-loader.

webpack.config.js:

module: {
    rules: [{
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
            loader: 'babel-loader',
            query: {
                plugins: [
                    [require('@QCFE/babel-plugin-lego-imports'), {
                        "my-library": {
                            "transform": function(importName) {
                                return {
                                    importResultName: 'my-library/etc/' + importName.toUpperCase(),
                                    skipDefaultConversion: false,
                                };
                            },
                            preventFullImport: true
                        }
                    }]
                ]
            }
        }
    }]
}

Options

| Name | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | transform | string | yes | undefined | The library name to use instead of the one specified in the import statement. ${member} will be replaced with the import name, aka Grid/Row/Col/etc., and ${1-n} will be replaced by any matched regular expression groups. Alternatively, pass a path to a .js file which exports a function to process the transform, which is invoked with parameters: (importName, matches). (see Advanced Transformations) | | preventFullImport | boolean | no | false | Whether or not to throw when an import is encountered which would cause the entire module to be imported. | | camelCase | boolean | no | false | When set to true, runs ${member} through _.camelCase. | | kebabCase | boolean | no | false | When set to true, runs ${member} through _.kebabCase. | | snakeCase | boolean | no | false | When set to true, runs ${member} through _.snakeCase. | | skipDefaultConversion | boolean | no | false | When set to true, will preserve import { X } syntax instead of converting to import X. |