Skip to content

Commit

Permalink
Merge pull request #5 from bytedance/feat/isomorphic-package
Browse files Browse the repository at this point in the history
feat: isomorphic package for both node.js and web bundler
chore: move nodejs benchmark into examples folder
feat: add nextjs-benchmark in examples
  • Loading branch information
eligao authored Oct 27, 2022
2 parents 580e2b5 + a90b5f2 commit 3703703
Show file tree
Hide file tree
Showing 26 changed files with 2,412 additions and 25 deletions.
48 changes: 28 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,40 @@ SHELL = /bin/bash
.DEFAULT_GOAL := build
.DELETE_ON_ERROR:
.SUFFIXES:
.PHONY: clean test benchmark

build: pkg/bundler

# wasm-pack targets bundlers by default
# https://rustwasm.github.io/docs/wasm-pack/commands/build.html#target
# updating the package name with sed before publish
# because wasm-pack doesn't support '@' in the Cargo.toml package name
.PHONY: clean test bench

build: pkg/isomorphic

# wasm-pack outputs for one target at a time, so we need to build for each
# target and stitch them together as a single isomorphic package
# to support node.js and web bundlers at the same time.
# see:
# - https://rustwasm.github.io/docs/wasm-pack/commands/build.html#target
# - https://github.com/rustwasm/wasm-pack/issues/313
pkg/isomorphic: pkg/bundler pkg/nodejs
cp -r pkg/bundler pkg/isomorphic
mkdir pkg/isomorphic/bundler
mv pkg/isomorphic/tar_wasm* pkg/isomorphic/bundler
mkdir pkg/isomorphic/nodejs
cp pkg/nodejs/tar_wasm* pkg/isomorphic/nodejs
cat pkg/bundler/package.json | jq '. |= .+ { "name":"@bytedance/tar-wasm", "files": [ "bundler", "nodejs" ], "module": "bundler/tar_wasm.js", "main": "nodejs/tar_wasm.js", "types": "bundler/tar_wasm.d.ts" }' > pkg/isomorphic/package.json

# build for the browser and bundlers, in ESModules format
pkg/bundler: src/* Cargo.toml README.md
wasm-pack build --target bundler --out-dir pkg/bundler
sed -i -e 's/"name": "tar-wasm"/"name": "@bytedance\/tar-wasm"/g' pkg/bundler/package.json

test: src tests Cargo.toml
wasm-pack test --chrome --headless

# for benchmarking only
# build for node.js, in CommonJS format
pkg/nodejs: src/* Cargo.toml README.md
wasm-pack build --target nodejs --out-dir pkg/nodejs
sed -i -e 's/"name": "tar-wasm"/"name": "@bytedance\/tar-wasm"/g' pkg/nodejs/package.json

clean:
rm -rf ./pkg ./target ./benchmark/node_modules
test: src tests Cargo.toml
wasm-pack test --chrome --headless

bench: pkg/isomorphic
cd examples/nodejs-benchmark; yarn install; yarn bench;

bench: pkg/nodejs
cd benchmark; yarn install; yarn bench;
clean:
rm -rf ./pkg ./target ./benchmark/node_modules

publish: pkg/bundler
cd pkg/bundler; npm publish
publish: pkg/isomorphic
cd pkg/isomorphic; npm publish
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ tar-js: 3943.49857711792ms (160.11482740095886x slower than @bytedance/tar-wasm)
tarts: 1251.595199584961ms (50.817553357373065x slower than @bytedance/tar-wasm)
```

See `./benchmark` folder for more details.
See `./examples/nodejs-benchmark` folder for more details.

## TODO

- [ ] Include a Promise API working from a separate worker thread.
- [ ] Add support for reading and writing tar files.
- [ ] Support of Node.js environment
- [x] Support of Node.js environment
- [x] Tests
- [x] Benchmarks
3 changes: 3 additions & 0 deletions examples/nextjs-benchmark/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions examples/nextjs-benchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
34 changes: 34 additions & 0 deletions examples/nextjs-benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
29 changes: 29 additions & 0 deletions examples/nextjs-benchmark/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
webpack: (webpackConfig, { isServer }) => {
// WASM imports are not supported by default. Workaround inspired by:
// https://github.com/vercel/next.js/issues/29362#issuecomment-1149903338
// https://github.com/vercel/next.js/issues/32612#issuecomment-1082704675
return {
...webpackConfig,
experiments: {
asyncWebAssembly: true,
layers: true,
},
optimization: {
...webpackConfig.optimization,
moduleIds: "named",
},
output: {
...webpackConfig.output,
webassemblyModuleFilename: isServer
? "./../static/wasm/[modulehash].wasm"
: "static/wasm/[modulehash].wasm",
},
};
},
};

module.exports = nextConfig;
30 changes: 30 additions & 0 deletions examples/nextjs-benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "nextjs-benchmark",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "13.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"@bytedance/tar-wasm": "../../pkg/isomorphic",
"lorem-ipsum": "^2.0.4",
"tar-js": "^0.3.0",
"tarts": "^2.0.1"
},
"devDependencies": {
"@types/node": "18.11.7",
"@types/react": "18.0.24",
"@types/react-dom": "18.0.8",
"eslint": "8.26.0",
"eslint-config-next": "13.0.0",
"typescript": "4.8.4",
"@types/tar-js": "^0.3.2",
"ts-node": "^10.7.0"
}
}
6 changes: 6 additions & 0 deletions examples/nextjs-benchmark/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import '../styles/globals.css'
import type { AppProps } from 'next/app'

export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
13 changes: 13 additions & 0 deletions examples/nextjs-benchmark/pages/api/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'

type Data = {
name: string
}

export default function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
res.status(200).json({ name: 'John Doe' })
}
46 changes: 46 additions & 0 deletions examples/nextjs-benchmark/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";
import { bench } from "../utils/bench";

export default function Home() {
const handleBenchButtonClick = () => {
alert("Benchmark started, check the console");
bench();
};

return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>

<p className={styles.description}>
<button onClick={handleBenchButtonClick}>
Start tar-wasm benchmark
</button>
</p>
</main>

<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{" "}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
);
}
Binary file added examples/nextjs-benchmark/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/nextjs-benchmark/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3703703

Please sign in to comment.