1.0.1 • Published 2 years ago

babel-plugin-dynamic-import-builder v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

babel-plugin-dynamic-import-builder

一个可以将指定组件挂载到 window 上的插件。可以使某些组件在运行时被替换。

如何使用

安装

$ npm i babel-plugin-dynamic-import-builder -D

配置

babel 参数

参数名类型必填说明
redirectImportsImportConfig[]需要被替换组件的描述,参数见下表
filePathstring临时文件生成的地址,默认为${process.cwd()}/temp.js
scopeNamestring被挂载在 window 上的变量 默认为 global

ImportConfig

参数名类型必填说明
componentNamestring需要被替换组件的名称
packageSourcestring需要被替换组件的 source
defaultImportboolean是否是 default 方式引入
scopeNamestring与上表中同名参数作用相同,但优先级更高

将它配置到 babel 插件中:

[
  'babel-plugin-dynamic-import-builder',
  {
    redirectImports: [
      {
        componentName: 'Dialog',
        packageSource: '@alifd/next',
      },
      {
        componentName: 'View',
        packageSource: 'rax-view',
        defaultImport: true,
      },
    ],
    filePath: 'filePath',
    scopeName: 'global',
  },
];

它做了什么

原始代码:

import { Dialog } from '@alifd/next';
import View from 'rax-view';

经过插件将会变为:

import { Dialog, View } from 'filePath';

filePath 的内容是:

import { Dialog as DialogDefault } from '@alifd/next';
import ViewDefault from 'rax-view';

export const Dialog = (window.global && window.global['Dialog']) || DialogDefault;
export const View = (window.global && window.global['ViewDefault']) || ViewDefault;