• babel-plugin-import 实现按需引入


     官方文档:https://github.com/umijs/babel-plugin-import#usage

    组件库按需加载原理分析 - 掘金

    组件库按需加载:

     组件库以组件为基本单位产出 js、css、less 文件,借助插件或者部分引入的写法,使得项目代码或 babel 编译后的代码中只包含使用到的组件的 js、css、less 等。 

     webpack 懒加载: webpack 将源码中的 import、require 引入的文件编译之后再根据动态加载语法配置(通常以页面路由为基本单位)将较大的代码拆分并构建出较小的 chunk 包,应用在运行时执行到相应业务逻辑时才去加载执行对应 chunk 代码。 webpack 懒加载主要发生在下图的 JS 拆分出不同的 Chunk 这一过程中。

     两者的差别主要在于:

    1. 两者执行时机不同,组件库按需加载是在源码编写阶段或者 babel 编译 js 阶段,而 webpack 懒加载则是在构建生成打包产物时,组件库按需加载在前,webpack 懒加载在后;
    2. 两者原理不同,组件库按需加载是在源码阶段就去掉了无关代码,而 webpack 懒加载则是将经过 tree-shaking 优化过后的大文件包进行拆分在适当的运行时进行按需加载。

    使用组件库:通常是引入min.js及css文件

    1. import Vue from 'vue';
    2. import Vant from 'vant';
    3. import 'vant/lib/index.css';
    4. Vue.use(Vant);

    这种写法经过 webpack 构建之后会将组件库产出的 vant.min.jsindex.css 引入并打包至构建产物中,而引入的 vant.min.js 文件是包含组件库全部组件的 js 部分,index.css 包含全部组件的 css 部分。因此,这会导致构建打包产物增大。

    组件库按需加载:

    方式一:手动加载

    手动引入需要使用到的组件以及其对应的样式文件即可,在 webpack 构件时组件库中其他未被引入的文件不会被打包。

    1. import Button from 'vant/lib/button';
    2. import 'vant/lib/button/style';

    组件库怎么实现产出为lib下对应组件?

    elementUI:

    1. //webpack.component.js
    2. const Components = require('../components.json');
    3. const webpackConfig = {
    4. mode: 'production',
    5. entry: Components,
    6. output: {
    7. path: path.resolve(process.cwd(), './lib'),
    8. publicPath: '/dist/',
    9. filename: '[name].js',
    10. chunkFilename: '[id].js',
    11. libraryTarget: 'commonjs2'
    12. },
    13. ...
    14. }
    15. //components.json:
    16. {
    17. "pagination": "./packages/pagination/index.js",
    18. "dialog": "./packages/dialog/index.js",
    19. "autocomplete": "./packages/autocomplete/index.js",
    20. "dropdown": "./packages/dropdown/index.js",
    21. "dropdown-menu": "./packages/dropdown-menu/index.js",
    22. ...
    23. }

    lib下还会生成整体的组件min.js及css:

    1. module.exports = {
    2. mode: 'production',
    3. entry: {
    4. app: ['./src/index.js']
    5. },
    6. output: {
    7. path: path.resolve(process.cwd(), './lib'),
    8. publicPath: '/dist/',
    9. filename: 'element-ui.common.js',
    10. chunkFilename: '[id].js',
    11. libraryExport: 'default',
    12. library: 'ELEMENT',
    13. libraryTarget: 'commonjs2'
    14. },
    15. ...
    16. }

    antd: 

    方式二:自动加载

    安装 babel-plugin-import 插件

    npm i babel-plugin-import -D

    修改 babel 插件配置

    1. module.exports = {
    2. plugins: [
    3. ['import', {
    4. libraryName: 'vant',
    5. libraryDirectory: 'es',
    6. style: true
    7. }, 'vant']
    8. ]
    9. };

    在项目代码中按需引入要用到的组件

    1. import { Button } from 'vant';
    2. Vue.use(Button);

    自动转换为

    1. import "vant/es/button/style";
    2. import _Button from "vant/es/button";

    为什么需要这个插件?
    在 antd 和 element 两个组件库中,index.js 分别是这样的:

    1. // antd
    2. export { default as Button } from './button';
    3. export { default as Table } from './table';
    4. // element
    5. import Button from '../packages/button/index.js';
    6. import Table from '../packages/table/index.js';
    7. export default {
    8.   Button,
    9.   Table
    10. }

    antd 和 element 都是通过 ES6 module 的 export 来导出带有命名的各个组件,因此我们可以通过 import 导入单组件的 JS 文件,但是我们还需要手动引入组件的样式:

    1. import Button from 'antd/lib/button';
    2. import 'antd/dist/antd.css';

    在上面的代码中,我们仅仅只需要一个 Button 组件,却把所有的样式都引入了,这明显是不合理的,会增加代码包的体积。

    当然我们也可以只引入单个组件的样式:

    1. import Button from 'antd/lib/button';
    2. import 'antd/lib/button/style';

    这样看上去没毛病,但是如果需要多个组件的时候,代码就显得不够优雅:

    1. import { Affix, Avatar, Button, Rate } from 'antd';
    2. import 'antd/lib/affix/style';
    3. import 'antd/lib/avatar/style';
    4. import 'antd/lib/button/style';
    5. import 'antd/lib/rate/style';

    这时候就应该思考一下,如何在引入组件的时候自动引入它的样式文件

    这个插件做了什么?
    简单来说,babel-plugin-import 就是解决了上面的问题,为组件库实现单组件按需加载并且自动引入其样式。

    1. import { Button } from 'antd';
    2. // 插件会自动将代码转换为按需引入的形式
    3. import Button from 'antd/lib/button';
    4. import 'antd/lib/button/style';

    这个插件怎么用
    简单来说只需关系三个参数即可。

    1. //在 babel 配置文件 .babelrc or babel-loader 中配置
    2. module.exports = {
    3.   plugins: [
    4.     ['import', {
    5.       libraryName: 'vant', // 包名
    6.       libraryDirectory: 'es', // 目录,默认 lib
    7.       style: true // 是否引入 style
    8.     }, 'vant']
    9.   ]
    10. };
    11. //多个包
    12. // 如果是 babel@6 版本,可以将 import.options 配置为一个数组:
    13. [
    14. {
    15. "libraryName": "antd",
    16. "libraryDirectory": "lib",
    17. "style": true
    18. },
    19. {
    20. "libraryName": "antd-mobile"
    21. },
    22. ]
    23. // 如果是 babel@7+ 版本,可以配置多个 `import` 插件实例:
    24. {
    25. "plugins": [
    26. ["import", { "libraryName": "antd", "libraryDirectory": "lib"}, "antd"],
    27. ["import", { "libraryName": "antd-mobile", "libraryDirectory": "lib"}, "antd-mobile"]
    28. ]
    29. }

    babel-plugin-import 具体实现按需加载思路如下:将代码解析成AST,去AST里面找ImportDeclaration,若是source对应的value为是该插件配置的libraryName,那么就是要处理的,之后看该引用有没有真正使用,未使用直接删掉引用,使用了,删掉引用语句,借助于babel相应plugin重新插入引用语句,比如引用对应目录下的组件及样式。

    转换为 AST 语法树后结构如下:(语法转换可以在这里尝试:astexplorer.net)

     

    1. {
    2. "type": "Program",
    3. "body": [
    4. {
    5. "type": "ImportDeclaration",
    6. "specifiers": [
    7. {
    8. "type": "ImportSpecifier",
    9. "imported": {
    10. "type": "Identifier",
    11. "name": "Button"
    12. },
    13. "local": {
    14. "type": "Identifier",
    15. "name": "Button"
    16. }
    17. },
    18. {
    19. "type": "ImportSpecifier",
    20. "imported": {
    21. "type": "Identifier",
    22. "name": "Input"
    23. },
    24. "local": {
    25. "type": "Identifier",
    26. "name": "Input"
    27. }
    28. }
    29. ],
    30. "source": {
    31. "type": "Literal",
    32. "value": "antd",
    33. }
    34. }
    35. ],
    36. "sourceType": "module"
    37. }

    以上 JSON 数据中,我们关注以下几个跟 import 导入有关的信息:

    • source.value:antd;
    • specifiers.imported.name:Button;
    • specifiers.local.name: Button;

    babel-plugin-import 的核心实现都在 Plugin 中:

    • 收集 import 语句 { xxx } 中的模块名称;
    • 分析模块导入后,是否被 call 使用到
    • 如果有被使用到,改写 import 语句,使得 path 具体到模块的所在目录。
    1. // Plugin.js
    2. import { join } from 'path';
    3. import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-imports';
    4. function transCamel(_str, symbol) {
    5. const str = _str[0].toLowerCase() + _str.substr(1);
    6. return str.replace(/([A-Z])/g, $1 => `${symbol}${$1.toLowerCase()}`);
    7. }
    8. function winPath(path) {
    9. return path.replace(/\\/g, '/');
    10. }
    11. export default class Plugin {
    12. constructor(
    13. libraryName, // 需要使用按需加载的包名
    14. libraryDirectory = 'lib', // 按需加载的目录
    15. style = false, // 是否加载样式
    16. types, // babel-type 工具函数
    17. index = 0,
    18. ) {
    19. this.libraryName = libraryName;
    20. this.libraryDirectory = libraryDirectory;
    21. this.style = style;
    22. this.types = types;
    23. this.pluginStateKey = `importPluginState${index}`;
    24. }
    25. // 获取内部状态,收集依赖,state 指向 plugin.visitor
    26. getPluginState(state) {
    27. if (!state[this.pluginStateKey]) {
    28. state[this.pluginStateKey] = {};
    29. }
    30. return state[this.pluginStateKey];
    31. }
    32. // 生成按需引入 import 语句(核心代码)
    33. importMethod(methodName, file, pluginState) {
    34. ...
    35. }
    36. ProgramEnter(path, state) {
    37. const pluginState = this.getPluginState(state);
    38. // 初始化插件实例的 state 对象
    39. pluginState.specified = Object.create(null);
    40. pluginState.libraryObjs = Object.create(null);
    41. pluginState.selectedMethods = Object.create(null);
    42. pluginState.pathsToRemove = [];
    43. }
    44. ProgramExit(path, state) {
    45. // 删除旧的 import
    46. this.getPluginState(state).pathsToRemove.forEach(p => !p.removed && p.remove());
    47. }
    48. // import 语句的处理方法,收集 import { xxx } 中的模块
    49. ImportDeclaration(path, state) {
    50. ...
    51. }
    52. // import 的模块被使用时的处理方法。上面收集了依赖之后,得判断这些 import 的变量是否被用到
    53. // 比如使用方式为:React.createElement(Button, null, "Hello"); 可将这行代码转换为 AST 节点树结合更容易理解 CallExpression 做的事情
    54. CallExpression(path, state) {
    55. ...
    56. }
    57. }

    第一步 依赖收集

    babel-plubin-import 会在 ImportDeclaration 里将所有的 specifier 收集起来。

    可以从这个 ImportDeclaration 语句中提取几个关键点:

    • source.value: antd
    • specifier.imported.name/specifier.local.name: Button 
    • specifier.local.name: Rate

    local.name 是导入进来的别名,比如 import { Button as MyButton } from 'antd' 的 MyButton 

    imported.name 是包 antd 真实 `导出` 的变量名 

    需要做的事情也很简单:

    1. import 的包是不是 antd,也就是 libraryName
    2. 把 Button 和 Rate 收集起来

    来看代码:

    1. ImportDeclaration(path, state) {
    2. const { node } = path;
    3. if (!node) return;
    4. // 代码里 import 的包名
    5. const { value } = node.source;
    6. // 配在插件 options 的包名
    7. const { libraryName } = this;
    8. // babel-type 工具函数
    9. const { types } = this;
    10. // 内部状态
    11. const pluginState = this.getPluginState(state);
    12. // 判断是不是需要使用该插件的包
    13. if (value === libraryName) {
    14. // node.specifiers 表示 import 了什么
    15. node.specifiers.forEach(spec => {
    16. // 判断是不是 ImportSpecifier 类型的节点,也就是是否是大括号的
    17. if (types.isImportSpecifier(spec)) {
    18. // 收集依赖
    19. // 也就是 pluginState.specified.Button = Button
    20. // local.name 是导入进来的别名,比如 import { Button as MyButton } from 'antd' 的 MyButton
    21. // imported.name 是真实导出的变量名
    22. pluginState.specified[spec.local.name] = spec.imported.name;
    23. } else {
    24. // ImportDefaultSpecifier 和 ImportNamespaceSpecifier
    25. pluginState.libraryObjs[spec.local.name] = true;
    26. }
    27. });
    28. pluginState.pathsToRemove.push(path);
    29. }
    30. }

    待 babel 遍历了所有的 ImportDeclaration 类型的节点之后,就收集好了依赖关系,下一步就是如何加载它们了。

    第二步 判断是否使用

    查找模块是否被使用 调用 CallExpression 分析被使用到的模块名,调用 importMethod 方法改写 import 路径.

    收集了依赖关系之后,得要判断一下这些 import 的变量是否被使用到了,我们这里说一种情况。

    我们知道,JSX 最终是变成 React.createElement() 执行的:

    1. ReactDOM.render(<Button>Hello</Button>);
    2. ↓ ↓ ↓ ↓ ↓ ↓
    3. React.createElement(Button, null, "Hello");

    没错,createElement 的第一个参数就是我们要找的东西,我们需要判断收集的依赖中是否有被 createElement 使用。

    分析一下这行代码的 ast,很容易就找到这个节点:

    来看代码:

    1. // import 的模块被使用时的处理方法。上面收集了依赖之后,得判断这些 import 的变量是否被用到
    2. // 比如使用方式为:React.createElement(Button, null, "Hello"); 可将这行代码转换为 AST 节点树结合更容易理解 CallExpression 做的事情
    3. CallExpression(path, state) {
    4. const { node } = path;
    5. const file = (path && path.hub && path.hub.file) || (state && state.file);
    6. // 方法调用者的 name,如:Button
    7. const { name } = node.callee;
    8. // babel-type 工具函数
    9. const { types } = this;
    10. // 内部状态
    11. const pluginState = this.getPluginState(state);
    12. // 如果方法调用者是 Identifier 类型
    13. if (types.isIdentifier(node.callee)) {
    14. if (pluginState.specified[name]) {
    15. node.callee = this.importMethod(pluginState.specified[name], file, pluginState);
    16. }
    17. }
    18. // 参数形式,如 React.createElement(Button, null, "Hello"),会将 Button 作为第一个参数
    19. node.arguments = node.arguments.map(arg => {
    20. const { name: argName } = arg;
    21. if (
    22. pluginState.specified[argName] &&
    23. path.scope.hasBinding(argName) && // 检查当前作用域内是否存在 Button 变量
    24. path.scope.getBinding(argName).path.type === 'ImportSpecifier' // 并且变量通过 import 方式创建
    25. ) {
    26. // 找到 specifier,调用 importMethod 方法
    27. return this.importMethod(pluginState.specified[argName], file, pluginState);
    28. }
    29. return arg;
    30. });
    31. }

    除了 React.createElement(Button) 之外,还有 const btn = Button / [Button] ... 等多种情况会使用 Button,源码中都有对应的处理方法,感兴趣的可以自己看一下: babel-plugin-import/Plugin.js at master · umijs/babel-plugin-import · GitHub ,这里就不多说了。

    第三步 生成引入代码(核心)

    第一步和第二步主要的工作是找到需要被插件处理的依赖关系,比如:

    1. import { Button, Rate } from 'antd';
    2. ReactDOM.render(<Button>Hello</Button>);

    Button 组件使用到了,Rate 在代码里未使用。所以插件要做的也只是自动引入 Button 的代码和样式即可。

    我们先回顾一下,当我们 import 一个组件的时候,希望它能够:

    1. import { Button } from 'antd';
    2. ↓ ↓ ↓ ↓ ↓ ↓
    3. var _button = require('antd/lib/button');
    4. require('antd/lib/button/style');

    并且再回想一下插件的配置 options,只需要将 libraryDirectory 以及 style 等配置用上就完事了。

    小朋友,你是否有几个问号?这里该如何让 babel 去修改代码并且生成一个新的 import 以及一个样式的 import 呢,不慌,看看代码就知道了:

    1. import { addSideEffect, addDefault, addNamed } from '@babel/helper-module-imports';
    2. importMethod(methodName, file, pluginState) {
    3. if (!pluginState.selectedMethods[methodName]) {
    4. // libraryDirectory:目录,默认 lib
    5. // style:是否引入样式
    6. const { style, libraryDirectory } = this;
    7. // 组件名转换规则
    8. // 优先级最高的是配了 camel2UnderlineComponentName:是否使用下划线作为连接符
    9. // camel2DashComponentName 为 true,会转换成小写字母,并且使用 - 作为连接符
    10. const transformedMethodName = this.camel2UnderlineComponentName
    11. ? transCamel(methodName, '_')
    12. : this.camel2DashComponentName
    13. ? transCamel(methodName, '-')
    14. : methodName;
    15. // 兼容 windows 路径
    16. // path.join('antd/lib/button') == 'antd/lib/button'
    17. const path = winPath(
    18. this.customName
    19. ? this.customName(transformedMethodName, file)
    20. : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName),
    21. );
    22. // 根据是否有导出 default 来判断使用哪种方法来生成 import 语句,默认为 true
    23. // addDefault(path, 'antd/lib/button', { nameHint: 'button' })
    24. // addNamed(path, 'button', 'antd/lib/button')
    25. pluginState.selectedMethods[methodName] = this.transformToDefaultImport
    26. ? addDefault(file.path, path, { nameHint: methodName })
    27. : addNamed(file.path, methodName, path);
    28. // 根据不同配置 import 样式
    29. if (this.customStyleName) {
    30. const stylePath = winPath(this.customStyleName(transformedMethodName));
    31. addSideEffect(file.path, `${stylePath}`);
    32. } else if (this.styleLibraryDirectory) {
    33. const stylePath = winPath(
    34. join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName),
    35. );
    36. addSideEffect(file.path, `${stylePath}`);
    37. } else if (style === true) {
    38. addSideEffect(file.path, `${path}/style`);
    39. } else if (style === 'css') {
    40. addSideEffect(file.path, `${path}/style/css`);
    41. } else if (typeof style === 'function') {
    42. const stylePath = style(path, file);
    43. if (stylePath) {
    44. addSideEffect(file.path, stylePath);
    45. }
    46. }
    47. }
    48. return { ...pluginState.selectedMethods[methodName] };
    49. }

    addSideEffectaddDefault 和 addNamed 是 @babel/helper-module-imports 的三个方法,作用都是创建一个 import 方法,具体表现是:

    addSideEffect

    1. addSideEffect(path, 'source');
    2. ↓ ↓ ↓ ↓ ↓ ↓
    3. import "source"

    addDefault

    1. addDefault(path, 'source', { nameHint: "hintedName" })
    2. ↓ ↓ ↓ ↓ ↓ ↓
    3. import hintedName from "source"

    addNamed

    1. addNamed(path, 'named', 'source', { nameHint: "hintedName" });
    2. ↓ ↓ ↓ ↓ ↓ ↓
    3. import { named as _hintedName } from "source"

    更多关于 @babel/helper-module-imports 见:@babel/helper-module-imports

    总结

    一起数个 1 2 3,babel-plugin-import 要做的事情也就做完了。

    我们来总结一下,babel-plugin-import 和普遍的 babel 插件一样,会遍历代码的 ast,然后在 ast 上做了一些事情:

    1. 收集依赖:找到 importDeclaration,分析出包 a 和依赖 b,c,d....,假如 a 和 libraryName 一致,就将 b,c,d... 在内部收集起来
    2. 判断是否使用:在多种情况下(比如文中提到的 CallExpression)判断 收集到的 b,c,d... 是否在代码中被使用,如果有使用的,就调用 importMethod 生成新的 impport 语句
    3. 生成引入代码:根据配置项生成代码和样式的 import 语句

    不过有一些细节这里就没提到,比如如何删除旧的 import 等... 感兴趣的可以自行阅读源码哦。

    看完一遍源码,是不是有发现,其实除了 antd 和 element 等大型组件库之外,任意的组件库都可以使用 babel-plugin-import 来实现按需加载和自动加载样式。

    没错,比如我们常用的 lodash,也可以使用 babel-plugin-import 来加载它的各种方法,可以动手试一下。

    核心支持类:

    @babel/parser

    它是Babel中使用的JavaScript解析器。默认启用ES2017,支持JSX,Flow,TypeScript,支持实验性的语言提议(至少是stage-0)

    @babel/traverse

    它实现了访问者模式,对AST进行遍历,插件可以通过它获取相应的AST节点,并对对应节点进行具体操作。

    @babel/generator

    它将AST转换成源代码,同时支持SourceMap

    参考

    简单实现 babel-plugin-import 插件 - axuebin - 博客园

    babel-plugin-import 使用 - 掘金 

    用 babel-plugin 实现按需加载 - 知乎

    庖丁解牛:最全babel-plugin-import源码详解 - 掘金

  • 相关阅读:
    Dcoker配置Rabbitmq集群
    【再识C进阶2(下)】详细介绍指针的进阶——利用冒泡排序算法模拟实现qsort函数,以及一下习题和指针笔试题
    Asp.Net Core 实现分片下载的最简单方式
    三分钟使用ngrok实现内网穿透
    01线程概述
    spark源码的scala解析
    Spring Boot是什么呢?
    教程 - 深度探讨在 Vue3 中引入 CesiumJS 的最佳方式
    python如何筛选具有多列值相同的两个dataframe
    【从安装JDK开始】Spring Cloud + Apache Ignite简单实例
  • 原文地址:https://blog.csdn.net/CamilleZJ/article/details/127862211