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

@zhangliu/babel-plugin-webpack-alias

v1.0.0

Published

babel 6 plugin which allows to use webpack aliases

Downloads

4

Readme

babel-plugin-webpack-alias

This Babel 6 plugin allows you to use webpack aliases and most of webpack resolve features in Babel.

Travis build Appveyor build codecov Gemnasium

Stable version Downloads semantic-release Commitizen friendly

This plugin is simply going to take the aliases defined in your webpack config and replace require paths. It is especially useful when you rely on webpack aliases to keep require paths nicer (and sometimes more consistent depending on your project configuration) but you can't use webpack in a context, for example for unit testing.

If you are having issues while making this plugin work, have a look at the examples folder. Play with them, mix your own config in, and feel free to open an issue!

Example

With the following webpack.config.js:

module.exports = {
    ...
    resolve: {
        alias: {
            'my-alias': path.join(__dirname, '/alias-folder/js/'),
            'library-name': './library-folder/folder'
        }
    }
    ...
};

A javascript file before compilation:

var MyModule = require('my-alias/src/lib/MyModule');
import MyImport from 'library-name/lib/import/MyImport';

will become:

var MyModule = require('../../alias-folder/js/lib/MyModule');
import MyImport from '../../library-folder/folder/lib/import/MyImport';

This is an example but the plugin will output the relative path depending on the position of the file and the alias folder.

See the examples folder for more configuration examples.

Installation

$ npm install --save-dev babel-plugin-webpack-alias

Add it as a plugin to your .babelrc file. You can optionally add a path to a config file, for example:

{
   "presets":[ "react", "es2015", "stage-0" ],
   "env": {
    "test": {
      "plugins": [
        [ "babel-plugin-webpack-alias", { "config": "./webpack.config.test.js" } ]
      ]
    }
  }
}

In this case, the plugin will only be run when NODE_ENV is set to test.

Supported resolve options

  • resolve.alias: That is the reason why this plugin has been made, see above for examples and details.
  • resolve.extensions: It will try to match extensions provided in the webpack configuration.

Options

  • config(string): Path to your webpack config file.

    The plugin is going to look for a webpack.config.js file or a webpack.config.babel.js at the root, in case your webpack configuration file is in another location, you can use this option to provide an absolute or relative path to it. You can also use environment variable in this option, using lodash template, for example:

    {
       "presets":[ "react", "es2015", "stage-0" ],
       "env": {
        "test": {
          "plugins": [
            [ "babel-plugin-webpack-alias", {
                "config": "${PWD}/webpack.config.test.js"
              }
            ]
          ]
        }
      }
    }

    And run with:

    $ PWD=$(pwd) NODE_ENV=test ava
  • findConfig(boolean): Will find the nearest webpack configuration file when set to true.

    It is possible to pass a findConfig option, and the plugin will attempt to find the nearest webpack configuration file within the project using find-up. For example:

    {
       "presets":[ "react", "es2015", "stage-0" ],
       "env": {
        "test": {
          "plugins": [
            [ "babel-plugin-webpack-alias", {
                "config": "webpack.config.test.js",
                "findConfig": true
              } ]
          ]
        }
      }
    }
  • noOutputExtension(boolean): Don't append extension at the end of filenames even when a resolve.extensions webpack config is set.

    The normal behaviour of the resolve.extensions support is this one:

    var MyModule = require('my-alias/src/lib/MyComponent.jsx');
    // is converted to:
    var MyModule = require('../../alias-folder/js/lib/MyComponent.jsx');

    However in particular cases you'll compile MyComponent.jsx to a MyComponent.js file so the build alias won't be able to resolve the jsx file. In that case you can turn noOutputExtension on and get:

    var MyModule = require('my-alias/src/lib/MyComponent.jsx');
    // is converted to:
    var MyModule = require('../../alias-folder/js/lib/MyComponent');