Skip to content

Commit

Permalink
Merge pull request #1963 from h3poteto/try/dual-package
Browse files Browse the repository at this point in the history
ESM and CJS dual package support
  • Loading branch information
h3poteto authored Oct 25, 2023
2 parents 9a81968 + a783243 commit 2e26d36
Show file tree
Hide file tree
Showing 26 changed files with 649 additions and 68 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Example

on:
push:
branches:
- master

tags:

pull_request:

jobs:
typescript:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 19.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: |
yarn install
- name: Build
run: |
yarn workspace megalodon run build
- name: Typescript example
env:
MASTODON_URL: https://fedibird.com
PLEROMA_URL: https://pleroma.io
FIREFISH_URL: https://cybre.club
FRIENDICA_URL: https://squeet.me
run: |
yarn workspace example run build
node example/typescript/dist/mastodon/instance.js
node example/typescript/dist/pleroma/instance.js
node example/typescript/dist/firefish/instance.js
node example/typescript/dist/friendica/instance.js
webpack:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 19.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: |
yarn install
- name: Build
run: |
yarn workspace megalodon run build
- name: Webpack example
run: |
yarn workspace webpack run build
vite:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 19.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: |
yarn install
- name: Build
run: |
yarn workspace megalodon run build
- name: Vite example
run: |
yarn workspace vite build
4 changes: 2 additions & 2 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
yarn workspace example lint | reviewdog -f=eslint -reporter=github-pr-review -fail-on-error=true
- name: example/browser eslint
- name: example/webpack eslint
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
yarn workspace browser lint | reviewdog -f=eslint -reporter=github-pr-review -fail-on-error=true
yarn workspace webpack lint | reviewdog -f=eslint -reporter=github-pr-review -fail-on-error=true
35 changes: 0 additions & 35 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,3 @@ jobs:
- name: Testing
run: |
yarn workspace megalodon run test
example:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x, 19.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: |
yarn install
- name: Build
run: |
yarn workspace megalodon run build
- name: Browser example
run: |
yarn workspace browser run build
- name: Typescript example
env:
MASTODON_URL: https://fedibird.com
PLEROMA_URL: https://pleroma.io
FIREFISH_URL: https://cybre.club
FRIENDICA_URL: https://squeet.me
run: |
yarn workspace example run build
node example/typescript/dist/mastodon/instance.js
node example/typescript/dist/pleroma/instance.js
node example/typescript/dist/firefish/instance.js
node example/typescript/dist/friendica/instance.js
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
lib
example/typescript/dist
example/javascript/dist
example/browser/dist
docs/
example/webpack/dist
example/vite/dist
docs/
.env
24 changes: 24 additions & 0 deletions example/vite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
14 changes: 14 additions & 0 deletions example/vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>

<script type="module" src="/src/main.ts"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions example/vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "vite",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"megalodon": "*"
},
"devDependencies": {
"typescript": "^5.0.2",
"vite": "^4.4.5",
"node-stdlib-browser": "^1.2.0",
"vite-plugin-node-stdlib-browser": "^0.2.1",
"process": "^0.11.10"
}
}
55 changes: 55 additions & 0 deletions example/vite/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import generator, { Entity } from 'megalodon'

const BASE_URL: string = import.meta.env.VITE_MASTODON_URL
const ACCESS_TOKEN: string = import.meta.env.VITE_MASTODON_ACCESS_TOKEN
console.log(BASE_URL)
console.log('start')

const client = generator('mastodon', BASE_URL, ACCESS_TOKEN)

const stream = client.publicSocket()
stream.on('connect', () => {
console.log('connect')
})

stream.on('pong', () => {
console.log('pong')
})

stream.on('update', (status: Entity.Status) => {
console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
console.log(notification)
})

stream.on('delete', (id: number) => {
console.log(id)
})

stream.on('error', (err: Error) => {
console.error(err)
})

stream.on('status_update', (status: Entity.Status) => {
console.log('updated: ', status.url)
})

stream.on('heartbeat', () => {
console.log('thump.')
})

stream.on('close', () => {
console.log('close')
})

stream.on('parser-error', (err: Error) => {
console.error(err)
})
//
// setTimeout(() => {
// stream.removeAllListeners()
// stream.stop()
// console.log('closed')
// }, 10000)
1 change: 1 addition & 0 deletions example/vite/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions example/vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
20 changes: 20 additions & 0 deletions example/vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from 'vite'
import nodePolyfills from 'vite-plugin-node-stdlib-browser'

// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
process: 'process/browser'
}
},
define: {
'process.browser': true,
'process.env.NODE_DEBUG': false
},
plugins: [
nodePolyfills({
include: ['buffer']
})
]
})
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion example/browser/index.html → example/webpack/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Megalodon</title>
<script src="./dist/main.js"></script>
<script type="module" src="./dist/main.js"></script>
</head>
<body>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "browser",
"name": "webpack",
"version": "4.0.0",
"private": true,
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions example/browser/src/index.ts → example/webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ console.log('start')

const client = generator('mastodon', BASE_URL, ACCESS_TOKEN)

const stream = client.localSocket()
const stream = client.publicSocket()
stream.on('connect', () => {
console.log('connect')
})
Expand Down Expand Up @@ -52,4 +52,4 @@ setTimeout(() => {
stream.removeAllListeners()
stream.stop()
console.log('closed')
}, 10000)
}, 100000)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["esnext", "dom", "dom.iterable", "scripthost"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion megalodon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "Mastodon API client for node.js and browser",
"main": "./lib/src/index.js",
"typings": "./lib/src/index.d.ts",
"module": "./lib/esm/src/index.js",
"scripts": {
"build": "tsc -p ./",
"build": "tsc -p ./ && tsc -p tsconfig.mjs.json",
"lint": "eslint --ext .js,.ts src",
"doc": "typedoc --out ../docs ./src",
"test": "NODE_ENV=test jest -u --maxWorkers=3"
Expand Down
5 changes: 2 additions & 3 deletions megalodon/src/firefish/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,8 @@ namespace FirefishAPI {
}
const url = this.baseUrl + '/streaming'
const streaming = new WebSocket(url, channel, this.accessToken, listId, this.userAgent, this.proxyConfig)
process.nextTick(() => {
streaming.start()
})

streaming.start()
return streaming
}
}
Expand Down
6 changes: 3 additions & 3 deletions megalodon/src/friendica/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ namespace FriendicaAPI {
}
const url = this.baseUrl + path
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
process.nextTick(() => {
streaming.start()
})

streaming.start()

return streaming
}
}
Expand Down
Loading

0 comments on commit 2e26d36

Please sign in to comment.