taro-plugin-assets-indexer
v0.0.1
Published
Taro plugin to automatically generate assets files entrance
Readme
Taro Plugin Assets Indexer
Taro 插件,用于自动生成静态资源文件入口。
简介
该插件会在构建过程中自动扫描项目中的 assets 文件夹,并为其中的静态资源生成对应的 TypeScript 入口文件,方便在项目中导入和使用这些资源。
安装
npm install taro-plugin-assets-indexer --save-dev或使用 yarn:
yarn add taro-plugin-assets-indexer --dev或使用 pnpm:
pnpm add taro-plugin-assets-indexer --save-dev使用
配置插件
在 Taro 项目的配置文件中添加插件:
const config = {
plugins: ["taro-plugin-assets"],
}
module.exports = config配置选项
可以通过在插件配置中传递选项来自定义行为:
const config = {
plugins: [
[
"taro-plugin-assets",
{
assetsFileName: "File_Name", // 默认为 'assets'
exclude: ["*.tmp"], // 排除特定文件
},
],
],
}
module.exports = config忽略文件夹
如果不想为某个 assets 文件夹生成入口文件,可以在该文件夹中添加 .assetsIgnore 文件。
使用示例
在项目中创建 assets 文件夹,例如:
src/
assets/
logo.png
images/
banner.jpg插件会在构建过程中自动生成 src/assets.ts 文件:
// @ts-nocheck
// import here
export default abstract class Assets {
/**  */
static Logo = Logo
/**  */
static ImagesBanner = ImagesBanner
}然后你可以在项目中这样使用:
import Assets from "@/assets"
// 使用图片资源
;<Image src={Assets.logo} />