Skip to content

Commit 1f73de0

Browse files
authored
ESM support (#1297)
* rename require -> import * esm all ts files * fix all unit tests * add config.ts * workaround with React unused variable error (#1299) * fix function order * 0.10.1 * add esm badge * 0.10.2
1 parent 98d1fde commit 1f73de0

File tree

95 files changed

+476
-462
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+476
-462
lines changed
File renamed without changes.

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ tmp/
1313

1414
hash.file
1515
jekyll/_posts/2020-12-31-osschat-a-bridge-to-the-apache-way-in-china.md
16+
17+
.nyc_output/

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![GitHub Pages CI](https://github.com/wechaty/wechaty.js.org/workflows/GitHub%20Pages%20CI/badge.svg)](https://github.com/wechaty/wechaty.js.org/actions?query=workflow%3A%22GitHub+Pages+CI%22)
44
[![Powered by Wechaty](https://img.shields.io/badge/Powered%20By-Wechaty-brightgreen.svg)](https://github.com/Wechaty/wechaty)
55
[![DIVIO documentation system](https://img.shields.io/badge/DIVIO-Documentation%20System-blue)](https://documentation.divio.com/)
6+
[![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https://github.com/Chatie/tsconfig/issues/16)
67

78
![Wechaty Docusaurus](docs/images/wechaty-docusaurus.png)
89

docusaurus/docs/api/wechaty.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Creates an instance of Wechaty.
8383
**Example** _\(The World's Shortest ChatBot Code: 6 lines of JavaScript\)_
8484

8585
```javascript
86-
const { Wechaty } = require('wechaty')
86+
import { Wechaty } from 'wechaty'
8787
const bot = new Wechaty()
8888
bot.on('scan', (qrcode, status) => console.log(['https://api.qrserver.com/v1/create-qr-code/?data=',encodeURIComponent(qrcode),'&size=220x220&margin=20',].join('')))
8989
bot.on('login', user => console.log(`User ${user} logined`))
@@ -356,7 +356,7 @@ Get the global instance of Wechaty
356356
**Example** _\(The World's Shortest ChatBot Code: 6 lines of JavaScript\)_
357357
358358
```javascript
359-
const { Wechaty } = require('wechaty')
359+
import { Wechaty } from 'wechaty'
360360

361361
Wechaty.instance() // Global instance
362362
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))

docusaurus/docs/examples/advanced/busy-bot.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ Create a new file `busy-bot.js`. You will be writing code here.
208208
Let's import required packages in `busy-bot.js` and initialize the bot by providing it a name and puppet to be used.
209209

210210
```js
211-
const { Wechaty, log } = require("wechaty");
211+
import { Wechaty, log } from 'wechaty';
212212

213-
const qrTerm = require("qrcode-terminal");
213+
import qrTerm from 'qrcode-terminal';
214214

215215
const bot = new Wechaty({
216216
name: "busy-bot",

docusaurus/docs/examples/advanced/friend-bot.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ Start by creating a new file `friend-bot.js`. We will be writing the code here.
219219
Let's import the required packages in the JavaScript file:
220220

221221
```js
222-
const qrTerm = require("qrcode-terminal");
223-
const { log, Wechaty, Friendship } = require("wechaty");
222+
import qrTerm from 'qrcode-terminal';
223+
import { log, Wechaty, Friendship } from 'wechaty';
224224
```
225225

226226
Define some functions required for handling different events returned by the Wechaty bot:

docusaurus/docs/examples/advanced/media-file-bot.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ Start by creating a new file `media-file-bot.js`. We will be writing the code he
214214
Let's import the required packages in the JavaScript file:
215215

216216
```js
217-
const qrTerm = require("qrcode-terminal");
218-
const { Wechaty, Message } = require("wechaty");
217+
import qrTerm from 'qrcode-terminal';
218+
import { Wechaty, Message } from 'wechaty';
219219
```
220220

221221
Specify some functions that you will require for handling different events returned by the Wechaty bot.

docusaurus/docs/examples/advanced/room-bot.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ Let's import required packages in `room-bot.js` file and create a `HELPER_CONTAC
218218
// or room create function will not work.
219219
const HELPER_CONTACT_NAME = "huan";
220220

221-
const qrTerm = require("qrcode-terminal");
221+
import qrTerm from 'qrcode-terminal';
222222

223-
const { Wechaty, log } = require("wechaty");
223+
import { Wechaty, log } from 'wechaty';
224224
```
225225
Now, we will write some functions which will be required for handling different events returned by bot.
226226

docusaurus/docs/examples/basic/contact-bot.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ Start by creating a new folder called `src`, and add a file `contact-bot.js`. We
215215
Let's import the required packages in the JavaScript file:
216216

217217
```js
218-
const qrTerm = require("qrcode-terminal");
219-
const { Contact, log, Wechaty } = require("wechaty");
218+
import qrTerm from 'qrcode-terminal';
219+
import { Contact, log, Wechaty } from 'wechaty';
220220
```
221221

222222
Specify some functions that you will require for handling different events returned by the Wechaty bot.

docusaurus/docs/examples/professional/tuling123-bot.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ Start by creating a new file `tuling-bot.js`. We will be writing the code here.
195195
Let's import the required packages in the JavaScript file:
196196

197197
```js
198-
const qrTerm = require('qrcode-terminal')
199-
const Tuling123 = require('tuling123-client')
198+
import qrTerm from 'qrcode-terminal'
199+
import Tuling123 from 'tuling123-client'
200200

201201
const {
202202
Wechaty,

docusaurus/docs/howto/deploy-whatsapp.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const {
103103
log,
104104
}= require('wechaty')
105105

106-
const qrTerm = require('qrcode-terminal')
106+
import qrTerm from 'qrcode-terminal'
107107

108108
console.log(welcome)
109109
const bot = new Wechaty()

docusaurus/docs/howto/event.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ await bot.start()
131131
<TabItem value="js">
132132

133133
```ts
134-
const { Wechaty,ScanStatus,log } = require('wechaty')
134+
import { Wechaty,ScanStatus,log } from 'wechaty'
135135

136136
async function onScan (qrcode,status)
137137
{

docusaurus/docs/howto/file-box.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ await bot.say(fileBox)
3636
<TabItem value="js">
3737

3838
```js
39-
const { FileBox } = require('wechaty')
39+
import { FileBox } from 'wechaty'
4040

4141
const fileBox = FileBox.fromUrl('https://wechaty.js.org/img/icon.png')
4242
await bot.say(fileBox)
@@ -141,7 +141,7 @@ bot.on('message', onMessage)
141141
<TabItem value="js">
142142

143143
```js
144-
const { Message } = require('wechaty')
144+
import { Message } from 'wechaty'
145145

146146
async function onMessage (message) {
147147
const fileTypeList = [

docusaurus/docs/howto/install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ We have a Wechaty [starter repository](https://github.com/wechaty/wechaty-gettin
1414

1515
```javascript
1616

17-
const { Wechaty } = require('wechaty') // import { Wechaty } from 'wechaty'
17+
import { Wechaty } from 'wechaty' // import { Wechaty } from 'wechaty'
1818

1919
Wechaty.instance() // Global Instance
2020
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`))

docusaurus/docs/howto/message.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bot.on('message', onMessage)
5656
<TabItem value="js">
5757

5858
```js
59-
const { Message } = require('wechaty')
59+
import { Message } from 'wechaty'
6060

6161
async function onMessage(message) {
6262
if (await message.mentionSelf()) {
@@ -178,7 +178,7 @@ bot.on('message', onMessage)
178178
<TabItem value="js">
179179

180180
```js
181-
const { Message } = require('wechaty')
181+
import { Message } from 'wechaty'
182182

183183
async function onMessage(message) {
184184
if (message.self()) {

docusaurus/docs/howto/wechaty.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ main()
4747
<TabItem value="js">
4848

4949
```js
50-
const { Wechaty } = require('wechaty')
50+
import { Wechaty } from 'wechaty'
5151

5252
async function main () {
5353
const bot = new Wechaty()

docusaurus/docs/polyglot/typescript/transclusions/shortest-chatbot-js.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```js
2-
const { Wechaty } = require('wechaty')
2+
import { Wechaty } from 'wechaty'
33

44
async function main () {
55
const bot = new Wechaty()

docusaurus/docs/tutorials/using-vorpal-with-wechaty.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Just scan the generated QR code with **WeChat** app, and you are ready to play w
4949
```ts
5050
import { Wechaty } from 'wechaty'
5151
import { WechatyVorpal } from 'wechaty-vorpal'
52-
const hackerNews = require('vorpal-hacker-news')
52+
import hackerNews from 'vorpal-hacker-news'
5353

5454
const wechaty = new Wechaty()
5555

@@ -244,7 +244,7 @@ import {
244244

245245
import { WechatyVorpal } from 'wechaty-vorpal'
246246
import { generate } from 'qrcode-terminal'
247-
const hackerNews = require('vorpal-hacker-news')
247+
import hackerNews from 'vorpal-hacker-news'
248248
```
249249

250250
Now, you have to define some functions that will help you to handle the different events returned by the Wechaty bot.

docusaurus/src/components/friday-qrcode-image.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react'
22

3-
export default function FridayQrCodeImage (_props) {
3+
void React
4+
5+
export default function FridayQrCodeImage (_props: any) {
46
return <img
57
src='/img/friday-qrcode.svg'
68
/>

docusaurus/src/components/qrcode.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from 'react'
22
import { QRCode } from 'react-qr-svg'
33

4-
export default function QrCodeComponent (props) {
4+
void React
5+
6+
export default function QrCodeComponent (props: any) {
57
const value = decodeURIComponent(props.match.params[0]) || 'https://wechaty.js.org'
68

79
return <QRCode

docusaurus/src/pages/index.tsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react'
2+
import type { FunctionComponent } from 'react'
23
import clsx from 'clsx'
34
import Layout from '@theme/Layout'
45
import Link from '@docusaurus/Link'
56
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'
67
import useBaseUrl from '@docusaurus/useBaseUrl'
78
import styles from './styles.module.css'
89

9-
import { DocusaurusConfig } from '@docusaurus/types'
10+
import type { DocusaurusConfig } from '@docusaurus/types'
11+
12+
void React
1013

1114
// one-pager - Encapsulates the essence of a technology onto a single page.
1215

@@ -44,14 +47,14 @@ const features = [
4447
},
4548
]
4649

47-
function Feature ({ key, imageUrl, title, description }) {
50+
const Feature: FunctionComponent<any> = ({ key, imageUrl, title, description }) => {
4851
void key
4952
const imgUrl = useBaseUrl(imageUrl)
5053
return (
51-
<div className={clsx('col col--4', styles.feature)}>
54+
<div className={clsx('col col--4', styles['feature'])}>
5255
{imgUrl && (
5356
<div className="text--center">
54-
<img className={styles.featureImage} src={imgUrl} alt={title} />
57+
<img className={styles['featureImage']} src={imgUrl} alt={title} />
5558
</div>
5659
)}
5760
<h3>{title}</h3>
@@ -67,15 +70,15 @@ function Home () {
6770
<Layout
6871
title={`${siteConfig.title}`}
6972
description="RPA SDK for Chatbot Makers.">
70-
<header className={clsx('hero hero--primary', styles.heroBanner)}>
73+
<header className={clsx('hero hero--primary', styles['heroBanner'])}>
7174
<div className="container">
7275
<h1 className="hero__title">{siteConfig.title}</h1>
7376
<p className="hero__subtitle">{siteConfig.tagline}</p>
74-
<div className={styles.buttons}>
77+
<div className={styles['buttons']}>
7578
<Link
7679
className={clsx(
7780
'button button--outline button--secondary button--lg',
78-
styles.getStarted,
81+
styles['getStarted'],
7982
)}
8083
to={useBaseUrl('/docs/')}>
8184
Get Started
@@ -85,7 +88,7 @@ function Home () {
8588
</header>
8689
<main>
8790
{features && features.length > 0 && (
88-
<section className={styles.features}>
91+
<section className={styles['features']}>
8992
<div className="container">
9093
<div className="row">
9194
{features.map((props, idx) => (

docusaurus/src/pages/press.tsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react'
2+
import type { FunctionComponent } from 'react'
23
import clsx from 'clsx'
34
import Layout from '@theme/Layout'
45
import Link from '@docusaurus/Link'
56
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'
67
import useBaseUrl from '@docusaurus/useBaseUrl'
78
import styles from './styles.module.css'
89

9-
import { DocusaurusConfig } from '@docusaurus/types'
10+
import type { DocusaurusConfig } from '@docusaurus/types'
11+
12+
void React
1013

1114
const features = [
1215
{
@@ -42,14 +45,14 @@ const features = [
4245
},
4346
]
4447

45-
function Feature ({ key, imageUrl, title, description }) {
48+
const Feature: FunctionComponent<any> = ({ key, imageUrl, title, description }) => {
4649
void key
4750
const imgUrl = useBaseUrl(imageUrl)
4851
return (
49-
<div className={clsx('col col--4', styles.feature)}>
52+
<div className={clsx('col col--4', styles['feature'])}>
5053
{imgUrl && (
5154
<div className="text--center">
52-
<img className={styles.featureImage} src={imgUrl} alt={title} />
55+
<img className={styles['featureImage']} src={imgUrl} alt={title} />
5356
</div>
5457
)}
5558
<h3>{title}</h3>
@@ -65,15 +68,15 @@ function Home () {
6568
<Layout
6669
title='Press and Media'
6770
description="Read about the most recent press releases, release launches, and open source community announcements.">
68-
<header className={clsx('hero hero--primary', styles.heroBanner)}>
71+
<header className={clsx('hero hero--primary', styles['heroBanner'])}>
6972
<div className="container">
7073
<h1 className="hero__title">{siteConfig.title}</h1>
7174
<p className="hero__subtitle">{'For inquiries, please contact us at:'}</p>
72-
<div className={styles.buttons}>
75+
<div className={styles['buttons']}>
7376
<Link
7477
className={clsx(
7578
'button button--outline button--secondary button--lg',
76-
styles.getStarted,
79+
styles['getStarted'],
7780
)}
7881
to={useBaseUrl('/press')}>
7982
@@ -85,7 +88,7 @@ function Home () {
8588
</header>
8689
<main>
8790
{features && features.length > 0 && (
88-
<section className={styles.features}>
91+
<section className={styles['features']}>
8992
<div className="container">
9093
<div className="row">
9194
{features.map((props, idx) => (

jekyll/_posts/2016-12-03-welcome-to-wechaty.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Wechaty is a easy to use **ChatBot Framework** which can help you write **the wo
2121
The following 6 lines javascript code example will show you how does Wechaty work:
2222

2323
```javascript
24-
const { Wechaty } = require('wechaty')
24+
import { Wechaty } from 'wechaty'
2525

2626
Wechaty.instance() // Singleton
2727
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))

jekyll/_posts/2016-12-05-ghostcloud-wechaty-docker.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ wechaty使用node编写,所以支持几乎所有的平台,wechaty的hello-wo
2828
Step 1: 新建一个mybot.js,内容如下:
2929

3030
```javascript
31-
const { Wechaty } = require('wechaty')
31+
import { Wechaty } from 'wechaty'
3232

3333
Wechaty.instance() // Singleton
3434
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))

jekyll/_posts/2016-12-10-try-to-write-wexinrobot.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Wechaty依赖的很多包都在墙外,建议使用国外的VPS,我使用的
107107
环境部署好以后下面6行代码,就可以成功的实现基础的bot功能:将微信机器人收到的所有消息打印出来:
108108

109109
```shell
110-
const { Wechaty } = require('wechaty')
110+
import { Wechaty } from 'wechaty'
111111

112112
Wechaty.instance()
113113
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))

jekyll/_posts/2017-04-21-interact-wechaty-with-ruby-on-rails-from-scratch.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ npm i --save wechaty chromedriver request
8888
Then create a new file named `bot.js` with this code base:
8989

9090
```javascript
91-
const { Wechaty } = require('wechaty');
91+
import { Wechaty } from 'wechaty';
9292

9393
function startBot() {
9494
const bot = Wechaty.instance({ profile: 'chatieme' });
@@ -104,7 +104,7 @@ startBot();
104104
Since we'd like to send the message to Rails app, we have to build a JSON object:
105105

106106
```javascript
107-
const { Wechaty } = require('wechaty');
107+
import { Wechaty } from 'wechaty';
108108

109109
function startBot() {
110110
const bot = Wechaty.instance({ profile: 'chatieme' });
@@ -338,8 +338,8 @@ We can test the API provided by Rails app using `curl`:
338338
We could take advantages of the library [request](https://github.com/request/request), posting the data to Rails:
339339
340340
```javascript
341-
const { Wechaty } = require('wechaty');
342-
const request = require('request');
341+
import { Wechaty } from 'wechaty';
342+
import request from 'request';
343343

344344
function startBot() {
345345
const bot = Wechaty.instance({ profile: 'chatieme' });

0 commit comments

Comments
 (0)