npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-taro-import

v0.1.2

Published

用于Taro工程运行前转换代码中对于“@tarojs/taro”的引用

Downloads

12

Readme

babel-plugin-transform-taro-import

用于Taro工程编译前转换代码中对于“@tarojs/taro”的引用

为何使用?

当你有需求如下需求可能会使用:

  • 准备对Taro进行外层的封装
  • 你的库中引用了一些Taro的各个运行时API的实现

可以看出小程序和 Web 端上组件标准API 标准有很大差异,这些差异仅仅通过代码编译手段是无法抹平的,例如你不能直接在编译时将小程序的 <view /> 直接编译成 <div />,因为他们虽然看上去有些类似,但是他们的组件属性有很大不同的,仅仅依靠代码编译,无法做到一致,同理,众多 API 也面临一样的情况。针对这样的情况,Taro 采用了定制一套运行时标准来抹平不同平台之间的差异。

这一套标准主要以三个部分组成,包括标准运行时框架标准基础组件库标准端能力 API,其中运行时框架和 API 对应 @taro/taro,组件库对应 @tarojs/components,通过在不同端实现这些标准,从而达到去差异化的目的。

如上(引自:Taro 多端开发实现原理与项目实战),Taro有一部分的端能力API是通过编译时初始化到Taro的核心库上的。所以在编译前你的库引用的是“@tarojs/taro”,编译后(如果你的平台是微信小程序)引用的应该是是“@tarojs/taro-weapp”。但Taro目前编译工具并未对三方库对其核心包的引用做编译,所以我们如果自己的库引用了Taro的核心库且使用的一些运行时的API,就需要该插件。

效果

{ "pileName": "alipay" }

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@tarojs/taro'), require('other')) : typeof define === 'function' && define.amd ? define(['@tarojs/taro', 'other'], factory) : (global = global || self, global.demo = factory(global.Taro, global.other));
})(this, function (Taro, other) {});

      ↓ ↓ ↓ ↓ ↓ ↓

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@tarojs/taro-alipay'), require('other')) : typeof define === 'function' && define.amd ? define(['@tarojs/taro-alipay', 'other'], factory) : (global = global || self, global.demo = factory(global.Taro, global.other));
})(this, function (Taro, other) {});

使用

npm install babel-plugin-transform-taro-import --save-dev

编辑 .babelrc

{
  "plugins": [["transform-taro-import", options]]
}

options

options 是一个object.

{
  "pileName": "alipay",  // 或  h5 、weapp 、swan 、 tt等
}

For Example(babel@7+):

// .babelrc
"plugins": [
  ["transform-taro-import", { "pileName": "alipay",}]
]

pileName

h5            ->         '@tarojs/taro-h5',
weapp   ->         '@tarojs/taro-weapp',
swan      ->         '@tarojs/taro-swan',
alipay    ->         '@tarojs/taro-alipay',
tt             ->         '@tarojs/taro-tt'

例子

详见工程下的demo。

  • .babelrc

    {
      "plugins": [
        [
          "babel-plugin-transform-taro-import",
          {
            "pileName": "alipay"
          }
        ]
      ]
    }
  • babel.config.js (babel@7+)

    module.exports = function (api) {
        // demo的量级不需要缓存
        api.cache(false);
      
        /**
         * 获取当前编译平台
         */
        const getCurrentCompilePile = function () {
            return process.env.npm_lifecycle_event ? process.env.npm_lifecycle_event.replace('demo:', '') : null;
        };planning_5.0.md
      
        const plugins = [
            [
                "babel-plugin-transform-taro-import",
                {
                    "pileName": getCurrentCompilePile()
                }
            ]
        ];
      
        return {
            plugins
        };
    };

计划

  • 增加测试用例
  • 增加debug
  • 增加代码构建
  • ~~增加ESM文件的转换~~