Namespace, Primitive, Method
    Have ideas to improve npm?Join in the discussion! »

    babel-plugin-transform-remove-imports
    TypeScript icon, indicating that this package has built-in type declarations

    1.5.2 • Public • Published

    babel-plugin-transform-remove-imports

    NPM version Build and Test Build Status Coverage Status

    Modular import plugin for babel for server-side rendering. Also works for cjs to delete imported CSS to avoid compilation errors..

    Usage

    npm install babel-plugin-transform-remove-imports --save-dev

    Via .babelrc or babel-loader.

    {
      "plugins": [
        [
          "babel-plugin-transform-remove-imports", {
            "test": "\\.(less|css)$"
          }
        ]
      ]
    }
    // Input Code
    import './index.less';
    import './index.main.less';
    import { Button } from 'uiw';
    import { Select } from '@uiw/core';
    
    // Output   ↓ ↓ ↓ ↓ ↓ ↓
    import { Button } from 'uiw';
    import { Select } from '@uiw/core';

    Output Result

    - import './index.less';
    - import './index.main.less';
    import { Button } from 'uiw';
    import { Select } from '@uiw/core';

    Support require

    Via .babelrc or babel-loader.

    {
      "plugins": [
        [
          "babel-plugin-transform-remove-imports", {
            "test": "@babel/core",
          }
        ]
      ]
    }
    // Input Code
    const core = require('@babel/core');
    useCore(require('@babel/core'));
    require('@babel/core');
    require('jest');
    
    // Output   ↓ ↓ ↓ ↓ ↓ ↓
    require('jest');

    Output Result

    - const core = require('@babel/core');
    - useCore(require('@babel/core'));
    - require('@babel/core');
    require('jest');

    Options

    • test: RegExp | string | (RegExp | string)[]

    A regular expression to match the imports that will be removed. It could be a string or a RegExp object. You could also pass an array here.

    • removeAll: boolean

    Deletes all imports.

    • remove?: 'effects'

    Removing only side effects imports,Used with the test option. #3

    // Input Code
    import 'foo';
    import Foo from 'foo';
    
    // Output Code  ↓ ↓ ↓ ↓ ↓ ↓
    import Foo from 'foo';

    Programmatic Usage

    import plugin from 'babel-plugin-transform-remove-imports'
    import { transform } from 'babel-core'
     
    function replace (code) {
      return transform(code, {
        babelrc: false,
        plugins: [
          [plugin, { test: /\.(less|css)$/ }]
        ],
      }).code;
    }
     
    replace("import './index.main.less';import { Button } from 'uiw';")
    //=> "import { Button } from 'uiw';"

    License

    MIT © Kenny Wong & Slava Fomin II

    Install

    npm i babel-plugin-transform-remove-imports

    DownloadsWeekly Downloads

    1,982

    Version

    1.5.2

    License

    MIT

    Unpacked Size

    8.4 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar