Skip to content

timwangdev/babel-plugin-remove-import-export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-remove-import-export

npm Downloads

A babel plugin to remove import and export declaration in the source file.

This will be useful if you just want to provide a code snippet without the extra module syntex, for example LeetCode. Or test your internal code without export it to the external.

Warning: This plugin will break the context of the script, use with caution.

Example

In

import { LinkedList } from 'some-lib';

function foo() {
  return new LinkedList(['bar']);
}

export class Solution {
  add(a, b) {
    return a + b;
  }
}

export default foo;

Out

function foo() {
  return new LinkedList(['bar']);
}

class Solution {
  add(a, b) {
    return a + b;
  }
}

Installation

yarn add -D babel-plugin-remove-import-export

Or, use npm:

npm install babel-plugin-remove-import-export --save-dev

Usage

Via .babelrc (Recommended)

.babelrc

// without options
{
  "plugins": ["remove-import-export"]
}

// with options
{
  "plugins": [
    ["remove-import-export", {
      "removeImport": false,
      "removeExport": false,
      "removeExportDefault": false,
      "preseveNamedDeclaration": false
    }]
  ]
}

Via CLI

babel --plugins remove-import-export script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["remove-import-export"]
});

Options

removeImport

boolean, defaults to true.

removeExport

boolean, defaults to true.

removeExportDefault

boolean, defaults to true. Set this option to false will preserve the default export.

preseveNamedDeclaration

boolean, defaults to true.

Example

Set preseveNamedDeclaration to false will not keep the declaration after export keyword.

In

function foo() {
  return 'bar';
}

export class Solution {
  add(a, b) {
    return a + b;
  }
}

Out

function foo() {
  return 'bar';
}

About

A babel plugin to remove import and export declaration in the source file.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published