Skip to content

yoyooyooo/import.macro

Folders and files

NameName
Last commit message
Last commit date
Dec 20, 2019
Dec 20, 2019
Dec 20, 2019
Dec 20, 2019
Dec 20, 2019
Dec 20, 2019
Dec 20, 2019
Dec 31, 2019
Dec 20, 2019
Dec 20, 2019
Dec 20, 2019
Apr 26, 2020
May 12, 2021

Repository files navigation

English | 简体中文

import.macro

auto import importDeclaration

NPM version Build Status codecov

install

npm i -D import.macro
// or
yarn add -D import.macro

ensure you have installed babel-plugin-macros

.babelrc

{
  plugins: ['babel-plugin-macros']
}

usage

import i from 'import.macro';
i('./a/b');
i('./a/b');

output:

import _aB from './a/b';
_aB;
_aB;

custom import

add a config file:

  • .babel-plugin-macrosrc
  • .babel-plugin-macrosrc.json
  • .babel-plugin-macrosrc.yaml
  • .babel-plugin-macrosrc.yml
  • .babel-plugin-macrosrc.js
  • babel-plugin-macros.config.js
  • babelMacros in package.json

Configuration is as follows:

// .babel-plugin-macrosrc.js
module.exports = {
  importHelper: {
    // import i from 'import.macro'
    defaultImport: {
      transformSource: a => a
    },
    imports: [
      [
        'customImport',
        {
          prefix: '/path/to', // prefix source path
          isDefaultExport: true, // default is true
          transformSource: a => a // before add importDeclaration,transform sourcePath
        }
      ]
    ]
  }
};

then, you can import customImport from import.macro

import { customImport } from 'import.macro';
customImport('filename');

output:

import filename from '../a/b/filename';
filename;

there is also a default import,you can configure it by importHelper.defaultImport.

when start with @, it will be compiled to React component. For example,

import { customImport } from 'import.macro';
customImport('@SomeComponent');

will be compiled to,

<SomeComponent />

About

auto add importDeclaration

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published