babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式。
# 安装插件
npm i babel-plugin-import -D
加载Sass源文件,通过Taro转成CSS
// babel.config.js
module.exports = {
plugins: [
[
"import",
{
libraryName: "@taroify/core",
libraryDirectory: "",
style: true,
},
"@taroify/core",
],
[
"import",
{
libraryName: "@taroify/icons",
libraryDirectory: "",
camel2DashComponentName: false,
style: () => "@taroify/icons/style",
customName: (name) => name === "Icon" ? "@taroify/icons/van/VanIcon" : `@taroify/icons/${name}`,
},
"@taroify/icons",
],
],
};
若不想加载Sass源文件或者其他情况,也可以加载编译后的CSS文件
// babel.config.js
module.exports = {
plugins: [
[
"import",
{
libraryName: "@taroify/core",
libraryDirectory: "",
style: (name) => `${name}/index.css`,
},
"@taroify/core",
],
[
"import",
{
libraryName: "@taroify/icons",
libraryDirectory: "",
camel2DashComponentName: false,
style: () => "@taroify/icons/index.css",
customName: (name) => name === "Icon" ? "@taroify/icons/van/VanIcon" : `@taroify/icons/${name}`,
},
"@taroify/icons",
],
],
};
接着你可以在代码中直接引入 Taroify 组件:
// 插件会自动将代码转化为方式二中的按需引入形式
import { Button } from "@taroify/core"
在不使用插件的情况下,可以手动引入需要的组件。
import "@taroify/core/button/style"
import { Button } from "@taroify/core"
// css
import "@taroify/core/button/index.css"
import { Button } from "@taroify/core"
在使用上面两种CSS方式引入时,若要使用Style 内置样式, 需要单独引入(版本 > 0.8.1 才产出次文件,之前未产出,低版 本无法通过CSS方式引入)
import "@taroify/core/styles/index.css"
Taroify 支持一次性导入所有组件,引入所有组件会增加代码包体积,因此不推荐这种做法。
// app.ts
import "@taroify/icons/index.scss"
import "@taroify/core/index.scss"
// css
import "@taroify/icons/index.css"
import "@taroify/core/index.css"
Tips: 配置按需引入后,将不允许直接导入所有组件。