Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shaobeichen/babel-plugin-client-variable

Preventing client variables from reporting errors on the server is very useful in SSR. 防止客户端变量在服务端报错,在SSR里使用很有用。
https://github.com/shaobeichen/babel-plugin-client-variable

babel babel-plugin client server ssr variable variables

Last synced: about 1 month ago
JSON representation

Preventing client variables from reporting errors on the server is very useful in SSR. 防止客户端变量在服务端报错,在SSR里使用很有用。

Awesome Lists containing this project

README

        

# babel-plugin-client-variable

## 安装

使用 npm 安装

```bash
npm install babel-plugin-client-variable -D
```

## 使用

在 babel.config.js 中添加

```diff
+ const clientVariable = require('babel-plugin-client-variable');

module.exports = {
+ plugins: [
+ clientVariable
+ ]
}
```

## 使用后效果

使用前打包编译成

```js
function name1() {
const aaa = document.querySelector('body')
console.warn(aaa)
}

name1()
```

使用后打包编译成

```js
function name1() {
const aaa = process.client ? document.querySelector('body') : {}
console.warn(aaa)
}

name1()
```