Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add esbuild example #34

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- helia-101
- helia-cjs
- helia-electron
- helia-esbuild
- helia-nextjs
- helia-script-tag
- helia-vite
Expand Down Expand Up @@ -78,6 +79,7 @@ jobs:
- helia-101
- helia-cjs
- helia-electron
- helia-esbuild
- helia-nextjs
- helia-script-tag
- helia-vite
Expand Down
17 changes: 17 additions & 0 deletions examples/helia-esbuild/.github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ⚠️ IMPORTANT ⚠️

# Please do not create a Pull Request for this repository

The contents of this repository are automatically synced from the parent [Helia Examples Project](https://github.com/ipfs-examples/helia-examples) so any changes made to the standalone repository will be lost after the next sync.

Please open a PR against [IPFS Examples](https://github.com/ipfs-examples/helia-examples) instead.

## Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.

1. Fork the [Helia Examples Project](https://github.com/ipfs-examples/helia-examples)
2. Create your Feature Branch (`git checkout -b feature/amazing-example`)
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`)
4. Push to the Branch (`git push origin feature/amazing-example`)
5. Open a Pull Request
19 changes: 19 additions & 0 deletions examples/helia-esbuild/.github/workflows/sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: pull

on:
workflow_dispatch

jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Pull from another repository
uses: ipfs-examples/actions-pull-directory-from-repo@main
with:
source-repo: ipfs-examples/helia-examples
source-folder-path: examples/${{ github.event.repository.name }}
source-branch: main
target-branch: main
git-username: github-actions
git-email: github-actions@github.com
105 changes: 105 additions & 0 deletions examples/helia-esbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<p align="center">
<a href="https://github.com/ipfs/helia" title="Helia">
<img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
</a>
</p>

<h3 align="center">Helia with esbuild</h3>

<p align="center">
<img src="https://raw.githubusercontent.com/jlord/forkngo/gh-pages/badges/cobalt.png" width="200">
<br>
<a href="https://ipfs.github.io/helia/modules/helia.html">Explore the docs</a>
·
<a href="https://codesandbox.io/s/github/ipfs-examples/helia-esbuild">View Demo</a>
·
<a href="https://github.com/ipfs-examples/helia-examples/issues">Report Bug</a>
·
<a href="https://github.com/ipfs-examples/helia-examples/issues">Request Feature/Example</a>
</p>

## Table of Contents

- [Table of Contents](#table-of-contents)
- [About The Project](#about-the-project)
- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Installation and Running example](#installation-and-running-example)
- [Usage](#usage)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [Want to hack on IPFS?](#want-to-hack-on-ipfs)

## About The Project

- Read the [docs](https://ipfs.github.io/helia/modules/helia.html)
- Look into other [examples](https://github.com/ipfs-examples/helia-examples) to learn how to spawn a Helia node in Node.js and in the Browser
- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
- Check out https://docs.ipfs.io for tips, how-tos and more
- See https://blog.ipfs.io for news and more
- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io

## Getting Started

### Prerequisites

Make sure you have installed all of the following prerequisites on your development machine:

- Git - [Download & Install Git](https://git-scm.com/downloads). OSX and Linux machines typically have this already installed.
- Node.js - [Download & Install Node.js](https://nodejs.org/en/download/) and the npm package manager.

### Installation and Running example

```console
> npm install
> npm start
```

Now open your browser at `http://localhost:8888`

## Usage

In this example, you will find a boilerplate you can use to guide yourself into bundling js-ipfs with [esbuild](https://esbuild.github.io/), so that you can use it in your own web app!

You should see the following:

![](./img/1.png)
![](./img/2.png)

_For more examples, please refer to the [Documentation](#documentation)_

## Documentation

- [IPFS Primer](https://dweb-primer.ipfs.io/)
- [IPFS Docs](https://docs.ipfs.io/)
- [Tutorials](https://proto.school)
- [More examples](https://github.com/ipfs-examples/helia-examples)
- [API - Helia](https://ipfs.github.io/helia/modules/helia.html)
- [API - @helia/unixfs](https://ipfs.github.io/helia-unixfs/modules/helia.html)

## Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.

1. Fork the IPFS Project
2. Create your Feature Branch (`git checkout -b feature/amazing-feature`)
3. Commit your Changes (`git commit -a -m 'feat: add some amazing feature'`)
4. Push to the Branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## Want to hack on IPFS?

[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)

The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:

Read the [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md) and [JavaScript Contributing Guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md).

- **Check out existing issues** The [issue list](https://github.com/ipfs/helia/issues) has many that are marked as ['help wanted'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22help+wanted%22) or ['difficulty:easy'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Adifficulty%3Aeasy) which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
- **Look at the [Helia Roadmap](https://github.com/ipfs/helia/blob/main/ROADMAP.md)** This are the high priority items being worked on right now
- **Perform code reviews** More eyes will help
a. speed the project along
b. ensure quality, and
c. reduce possible future bugs
- **Add tests**. There can never be enough tests
14 changes: 14 additions & 0 deletions examples/helia-esbuild/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { build } from 'esbuild'

build({
entryPoints: ['./src/index.js'],
outfile: './dist/index.js',
sourcemap: 'external',
minify: true,
bundle: true,
define: {
'process.env.NODE_DEBUG': 'false',
global: 'globalThis'
}
})
.catch(() => process.exit(1))
Binary file added examples/helia-esbuild/img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/helia-esbuild/img/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions examples/helia-esbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "helia-esbuild",
"version": "1.0.0",
"private": true,
"type": "module",
"description": "Bundle Helia with esbuild",
"keywords": [],
"license": "ISC",
"author": "",
"scripts": {
"clean": "rimraf ./dist",
"build": "mkdir -p dist && cp src/index.html src/style.css src/ipfs-logo.svg src/favicon.ico dist && node esbuild.js",
"start": "npm run build && esbuild --servedir=dist",
"test": "npm run build && playwright test tests"
},
"browserslist": "last 1 Chrome version",
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.4",
"@chainsafe/libp2p-yamux": "^3.0.7",
"@helia/unixfs": "^1.2.1",
"@libp2p/bootstrap": "^6.0.3",
"@libp2p/websockets": "^5.0.8",
"@libp2p/webtransport": "^1.0.11",
"blockstore-core": "^4.1.0",
"datastore-core": "^9.1.1",
"helia": "^1.0.0",
"libp2p": "^0.43.3"
},
"devDependencies": {
"@babel/core": "^7.14.8",
"@playwright/test": "^1.12.3",
"esbuild": "^0.15.7",
"playwright": "^1.12.3",
"process": "^0.11.10",
"rimraf": "^3.0.2",
"test-util-ipfs-example": "^1.0.2",
"util": "^0.12.4"
}
}
Binary file added examples/helia-esbuild/src/favicon.ico
Binary file not shown.
50 changes: 50 additions & 0 deletions examples/helia-esbuild/src/helia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable no-console */

import { createHelia } from 'helia'
import { createLibp2p } from 'libp2p'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { webSockets } from '@libp2p/websockets'
import { webTransport } from '@libp2p/webtransport'
import { bootstrap } from '@libp2p/bootstrap'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'

export async function createNode () {
// the blockstore is where we store the blocks that make up files
const blockstore = new MemoryBlockstore()

// application-specific data lives in the datastore
const datastore = new MemoryDatastore()

// libp2p is the networking layer that underpins Helia
const libp2p = await createLibp2p({
datastore,
transports: [
webSockets(),
webTransport()
],
connectionEncryption: [
noise()
],
streamMuxers: [
yamux()
],
peerDiscovery: [
bootstrap({
list: [
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
]
})
]
})

return await createHelia({
datastore,
blockstore,
libp2p
})
}
95 changes: 95 additions & 0 deletions examples/helia-esbuild/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bundle js-ipfs with parcel.js</title>

<link
rel="stylesheet"
href="https://unpkg.com/tachyons@4.10.0/css/tachyons.min.css"
crossorigin
/>
<link
rel="stylesheet"
href="https://unpkg.com/ipfs-css@0.12.0/ipfs.css"
crossorigin
/>
<link rel="shortcut icon" href="./favicon.ico" />
<link rel="stylesheet" href="./style.css" />

<script type="module" defer src="./index.js"></script>
</head>
<body class="montserrat f5">
<header class="flex items-center pa3 bg-navy bb bw3 b--aqua">
<a href="https://ipfs.io" title="home">
<img
alt="IPFS logo"
src="./ipfs-logo.svg"
style="height: 50px"
class="v-top"
/>
</a>
</header>

<main class="pa4-l bg-snow mw7 mv5 center pa4">
<h1 class="pa0 f2 ma0 mb4 aqua tc">Add data to Helia from the browser</h1>

<form id="add-file">
<label for="file-name" class="f5 ma0 pb2 aqua fw4 db">Name</label>
<input
class="input-reset bn black-80 bg-white pa3 w-100 mb3"
id="file-name"
name="file-name"
type="text"
placeholder="file.txt"
required
/>

<label for="file-content" class="f5 ma0 pb2 aqua fw4 db">Content</label>
<input
class="input-reset bn black-80 bg-white pa3 w-100 mb3 ft"
id="file-content"
name="file-content"
type="text"
placeholder="Hello world"
required
/>

<button
class="
button-reset
pv3
tc
bn
bg-animate bg-black-80
hover-bg-aqua
white
pointer
w-100
"
id="add-submit"
type="submit"
>
Add file
</button>
</form>

<h3>Output</h3>

<div class="window">
<div class="header"></div>
<div id="terminal" class="terminal">
<div id="output"></div>
</div>
</div>

<h3>Peers</h3>

<div class="window">
<div id="peers" class="terminal"></div>
</div>
</main>
</body>
</html>
Loading