Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noyobo/platform.macro
用于执行编译时平台环境判断
https://github.com/noyobo/platform.macro
Last synced: 8 days ago
JSON representation
用于执行编译时平台环境判断
- Host: GitHub
- URL: https://github.com/noyobo/platform.macro
- Owner: noyobo
- Created: 2020-03-09T15:37:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-21T02:38:17.000Z (5 months ago)
- Last Synced: 2024-06-21T21:27:23.539Z (5 months ago)
- Language: JavaScript
- Size: 131 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# platform.macro
用于执行编译时平台环境判断 [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros)
通过编译时环境变量 `PLATFORM` 判断, 将编译是时的引用 `isWechat` `isWeb` `isToutiao` 转换成 boolean 值, 构建出对应平台所需的代码(移除其他平台) 实现按需构建
## 安装
`npm install platform.macro --save`
## 示例
配合 `babel-plugin-macros` 插件使用
```js
// input.js
import { isWecaht } from 'platform.macro';if (isWechat) {
// 小程序环境执行
wx.showToast('我是小程序');
} else {
window.alert('我是 web 环境');
}
``````bash
$ PLATFORM=wechat babel input.js -o output.js
``````js
// output.js
'use strict';// 小程序环境执行
wx.showToast('我是小程序');
```