项目作者: uiwjs

项目描述 :
Modular import plugin for babel.
高级语言: JavaScript
项目地址: git://github.com/uiwjs/babel-plugin-transform-uiw-import.git
创建时间: 2019-12-07T09:54:51Z
项目社区:https://github.com/uiwjs/babel-plugin-transform-uiw-import

开源协议:MIT License

下载


babel-plugin-transform-uiw-import

NPM version
Build Status
Coverage Status

Modular import uiw plugin for babel. compatible with uiw, antd, lodash, material-ui, and so on.

Usage

  1. npm install babel-plugin-transform-uiw-import --save-dev

Via .babelrc or babel-loader.

  1. {
  2. "plugins": [
  3. ["babel-plugin-transform-uiw-import"]
  4. ]
  5. }
  1. // Input Code
  2. import { Alert } from 'uiw';
  3. import { CopyToClipboard } from 'uiw';
  4. import { DateInput, DatePicker } from 'uiw';
  5. // Output ↓ ↓ ↓ ↓ ↓ ↓
  6. import Alert from "uiw/lib/cjs/alert";
  7. import CopyToClipboard from "uiw/lib/cjs/copy-to-clipboard";
  8. import DateInput from "uiw/lib/cjs/date-input";
  9. import DatePicker from "uiw/lib/cjs/date-picker";

Output Result

  1. - import { Alert } from 'uiw';
  2. + import Alert from "uiw/lib/cjs/alert";
  3. - import { CopyToClipboard } from 'uiw';
  4. + import CopyToClipboard from "uiw/lib/cjs/copy-to-clipboard";
  5. - import { DateInput, DatePicker } from 'uiw';
  6. + import DateInput from "uiw/lib/cjs/date-input";
  7. + import DatePicker from "uiw/lib/cjs/date-picker";

Options

  1. export interface Options {
  2. [key: string]: {
  3. transform: (importName: string) => void | string;
  4. preventFullImport?: boolean;
  5. skipDefaultConversion?: boolean;
  6. }
  7. }

Default Options

  1. /**
  2. * Camel conversion horizontal line interval
  3. * @param {String} name
  4. * `CopyToClipboard` => `copy-to-clipboard`
  5. */
  6. function toLine(name) {
  7. return name.replace(/\B([A-Z])/g, '-$1').toLowerCase()
  8. }
  9. // The default Options
  10. {
  11. uiw: {
  12. transform: importName => `uiw/lib/cjs/${toLine(importName)}`,
  13. }
  14. }

transform: string | function(importName: string): string

The library name to use instead of the one specified in the import statement. ${member} will be replaced with the import name, aka Grid/Row/Col/etc., and ${1-n} will be replaced by any matched regular expression groups. If using a JS Babel config file, a function may be passed directly. (see Programmatic Usage)

preventFullImport: boolean

default false

Whether or not to throw when an import is encountered which would cause the entire module to be imported.

skipDefaultConversion: boolean

default false

When set to true, will preserve import { X } syntax instead of converting to import X.

  1. // Input Code
  2. import { Grid as gird } from 'uiw';
  3. // Output ↓ ↓ ↓ ↓ ↓ ↓ ====> skipDefaultConversion: true
  4. import { Grid as gird } from 'uiw/lib/Grid';
  1. - import { Grid as gird } from 'uiw';
  2. + import { Grid as gird } from 'uiw/lib/Grid';

Programmatic Usage

  1. import plugin from 'babel-plugin-transform-uiw-import'
  2. import { transform } from 'babel-core'
  3. // `CopyToClipboard` => `copy-to-clipboard`
  4. function toLine(name) {
  5. return name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
  6. }
  7. function replace (code) {
  8. return transform(code, {
  9. babelrc: false,
  10. plugins: [
  11. [plugin, {
  12. 'date-fns': {
  13. transform: importName => `date-fns/${toLine(importName)}`,
  14. preventFullImport: true,
  15. },
  16. }]
  17. ],
  18. }).code;
  19. }
  20. replace("import { CopyToClipboard } from 'date-fns';")
  21. //=> "import CopyToClipboard from "date-fns/copy-to-clipboard";"

License

MIT © Kenny Wong