babel-plugin-webpack-alias

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 屠兴旺
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

babel-plugin-webpack-alias

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

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');
  • 安装代码压缩插件 npm install uglifyjs-webpack-plugin --save-dev 在webpack.base.conf.js中配置 /* * @Descripttion: * @version: * @Author: zhangfan * @email: 2207044692@qq.com * @Date: 2020-05-18 13:46:43 *

  • 概述: 本文旨在通过从0开始搭建一套完整的React开发框架来掌握如webpack、react、ts、loader、babel、eslint、prettier、husky、lint-staged等各个部分基础是如何协同编译开发的,进而去了解creat-react-app之类的CI都做了哪些事情。 webpack中文网 1 初始化项目 npm init yarn init & npm init 都是

  • 性能分析 1. 统计基本信息 使用webpack内置的stats 可以统计出构建时间、构建资源清单及资源大小等信息 使用方法: 1. cli webpack --env production --json > stats.json 2. node API webpack(config, (err, stats) => { console.log(stats); }); 2. 速度分析 使用

  • //webpack.config.js var path = require('path') var webpack = require('webpack') const VueLoaderPlugin = require('vue-loader/lib/plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') const

  • 什么是webpack 本质上,webpack 是一个用于现代 JavaScript 应用程序的 静态模块打包工具 。当 webpack 处理应用程序时,它会在内部从一个或多个入口点构建一个 依赖图 ,然后将你项目中所需的每一个模块组合成一个或多个 bundles ,它们均为静态资源,用于展示你的内容 打包: 将不同类型的资源按模块处理进行打包 静态:打包最终产出静态资源 模块:webpack 支持

  • 自己的react项目用到了css-modules,由于不太想在写className时写style.xxx于是google解决方案,找到了这货->babel-plugin-react-css-modules。 然而写配置时踩了无数坑,网上唯一一篇中文讲使用的文章也过时了(webpack...),结合github文档及官方的demo终于鼓捣出了一个能用的配置。 { te

  • webpack.config.dev.js 'use strict'; const autoprefixer = require('autoprefixer'); const path = require('path'); const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plu

  • vue+typescript+webpack4 项目搭建步骤 前言: 因为最近才开始接触Typescript,然后首先就是搭建环境,而网上教程时间比较久而且不全,所以在这里记录下手把手的详细脚印。? ??? 源码地址请戳 ??? vue-ts-init 1、初始化项目 vue init webpack vue-ts-init cd vue-ts-init npm install 复制代码 脚手架项

 相关资料
  • DEPREACTED due to lack of support/bug fixes/ new features, project abandoned, please migrate on https://github.com/webpack-contrib/terser-webpack-plugin Babel Minify Webpack Plugin A Webpack Plugin fo

  • webpack-babel-multi-target-plugin This project, inspired by Phil Walton's articleDeploying es2015 Code in Production Today,adds tooling to simplify the additional configuration with aWebpack plugin, B

  • babel-plugin-prismjs A babel plugin to use PrismJS with standard bundlers. How to Use This plugin allows you to treat PrismJS as a standard module and configure what languages, plugins, & themes you w

  • koa-babel-webpack-boilerplate A simple boilerplate to create REST apps with koa@next (currently 2.0.0-alpha.3) babel (for async, await and stage-2 support) webpack How to use Boilerplate is packed wit

  • webpack-babel-env-deps Find dependencies to transpile with Babel. �� Read the docs! A webpack helper to find dependencies of your project that require transpilation with Babel (and @babel/preset-env)b

  • Tree-shaking example with Babel and Webpack This repository shows how to configure Babel and Webpack to enable tree-shaking.It will eliminate dead code if they have ES2015 module format. The source co