All Projects → trayio → Babel Plugin Webpack Alias

trayio / Babel Plugin Webpack Alias

Licence: mit
babel 6 plugin which allows to use webpack resolve options

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Babel Plugin Webpack Alias

React Pages Boilerplate
Deliver react + react-router application to gh-pages
Stars: ✭ 134 (-11.26%)
Mutual labels:  webpack, babel
Webpack Babel Multi Target Plugin
A Webpack plugin that works with Babel to allow differential loading - production deployment of ES2015 builds targeted to modern browsers, with an ES5 fallback for legacy browsers.
Stars: ✭ 150 (-0.66%)
Mutual labels:  webpack, babel
170330 webpack
「最新版で学ぶwebpack入門」のサンプルコード集です。ReactやBabelなど30以上の構成のサンプルを用意しています。
Stars: ✭ 135 (-10.6%)
Mutual labels:  webpack, babel
Webpack Babel Env Deps
Find dependencies to transpile with Babel.
Stars: ✭ 130 (-13.91%)
Mutual labels:  webpack, babel
Js Stack Boilerplate
Final boilerplate code of the JavaScript Stack from Scratch tutorial –
Stars: ✭ 145 (-3.97%)
Mutual labels:  webpack, babel
Modern Javascript
Code for TylerMcGinnis.com's "Modern JavaScript" course
Stars: ✭ 130 (-13.91%)
Mutual labels:  webpack, babel
Book
《现代化前端工程师权威指南》https://guoyongfeng.github.io/book/
Stars: ✭ 141 (-6.62%)
Mutual labels:  webpack, babel
Reactly Starter Kit
Deployable React + Webpack 2 starter kit
Stars: ✭ 122 (-19.21%)
Mutual labels:  webpack, babel
React Starter Kit
完美使用 React, Redux, and React-Router!超好用的脚手架
Stars: ✭ 1,755 (+1062.25%)
Mutual labels:  webpack, babel
Iceberg
Front-End Boilerplate built with React + Babel + Webpack + SASS
Stars: ✭ 144 (-4.64%)
Mutual labels:  webpack, babel
Webpack Starter
✨ A lightweight foundation for your next webpack based frontend project.
Stars: ✭ 1,745 (+1055.63%)
Mutual labels:  webpack, babel
Keepformac
keep for mac
Stars: ✭ 147 (-2.65%)
Mutual labels:  webpack, babel
Mhy
🧩 A zero-config, out-of-the-box, multi-purpose toolbox and development environment
Stars: ✭ 128 (-15.23%)
Mutual labels:  webpack, babel
Angular2 Esnext Todomvc
Angular TodoMVC application in Javascript (ES6/ES7).
Stars: ✭ 132 (-12.58%)
Mutual labels:  webpack, babel
Serverless Prisma
AWS Serverless Prisma Boilerplate
Stars: ✭ 126 (-16.56%)
Mutual labels:  webpack, babel
Preact Minimal
🚀 Minimal preact structure
Stars: ✭ 136 (-9.93%)
Mutual labels:  webpack, babel
Vue Starter
🐩 A boilerplate for HTML5, Vue, Vue Router, i18n, Tailwind, Windi, Netlify, and Vite.
Stars: ✭ 120 (-20.53%)
Mutual labels:  webpack, babel
Across Tabs
Easy communication between cross-origin browser tabs. Simplified "CORS"ing!
Stars: ✭ 1,575 (+943.05%)
Mutual labels:  webpack, babel
Project Webcube
Continuously updated JS infrastructure for modern web dev
Stars: ✭ 141 (-6.62%)
Mutual labels:  webpack, babel
Breko Hub
Babel React Koa Hot Universal Boilerplate
Stars: ✭ 145 (-3.97%)
Mutual labels:  webpack, babel

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');
    
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].