This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add parceljs browser example (#1726)
- Loading branch information
1 parent
ce4ff3e
commit 548504d
Showing
8 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-env"], | ||
"plugins": ["syntax-async-functions","transform-regenerator"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dist/ | ||
/node_modules/ | ||
.nvmrc | ||
.cache | ||
npm-debug.log | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Bundle js-ipfs with [Parcel.js](https://parceljs.org/) | ||
|
||
> In this example, you will find a boilerplate application that connects to | ||
IPFS using JS-IPFS and is bundled with [Parcel.js](https://parceljs.org/), so | ||
that you can follow it for creating Parcel.js bundled js-ipfs DApps. | ||
|
||
## Before you start | ||
|
||
1. Start your IPFS daemon of choice e.g. `ipfs daemon` (optional if you do not | ||
want to serve the example over IPFS) | ||
1. Open a new terminal | ||
1. `cd` into this folder | ||
1. Run `npm install` | ||
|
||
## Running this example in development mode with auto-reloading | ||
|
||
1. `npm start` | ||
1. Open your browser at `http://localhost:1234` | ||
|
||
You should see the following: | ||
|
||
![](https://ipfs.io/ipfs/QmSiZ18GffagbbJ3z72kK7u3SP9MXqBB1vrU1KFYP3GMYs/1.png) | ||
|
||
## Build and add to IPFS | ||
|
||
1. Clear the contents of `dist` if this is not the first time you are building | ||
e.g. `rm -r dist` on a unix system | ||
1. `npm run build` | ||
1. The production build of the site is now in the `dist` folder | ||
1. Add the folder to ipfs using your IPFS client of choice e.g. | ||
`ipfs add -r dist` | ||
|
||
The last hash output is the hash of the directory. This can be used to access | ||
this example served over IPFS and will be accessible by a public gateway: | ||
|
||
> https://ipfs.io/ipfs/<hash_of_directory>/ | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "browser-parceljs", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"browserslist": [ | ||
"last 2 Chrome versions" | ||
], | ||
"scripts": { | ||
"lint": "standard public/**/*.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "parcel public/index.html", | ||
"build": "parcel build public/index.html --public-url ./" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"ipfs": "file:../../" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/preset-env": "^7.1.6", | ||
"babel-plugin-syntax-async-functions": "^6.13.0", | ||
"babel-plugin-transform-regenerator": "^6.26.0", | ||
"babel-polyfill": "^6.26.0", | ||
"parcel-bundler": "^1.10.3", | ||
"standard": "^12.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
|
||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>js-ipfs parcel.js browser example</title> | ||
</head> | ||
|
||
<body> | ||
<header> | ||
<h1 id="status">Connecting to IPFS...</h1> | ||
</header> | ||
|
||
<main> | ||
<pre id="output"></pre> | ||
</main> | ||
|
||
<script src="./index.js"></script> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'babel-polyfill' | ||
import IPFS from 'ipfs' | ||
|
||
// IPFS node setup | ||
const node = new IPFS({ repo: String(Math.random() + Date.now()) }) | ||
|
||
// UI elements | ||
const status = document.getElementById('status') | ||
const output = document.getElementById('output') | ||
|
||
output.textContent = '' | ||
|
||
function log (txt) { | ||
console.info(txt) | ||
output.textContent += `${txt.trim()}\n` | ||
} | ||
|
||
node.on('ready', async () => { | ||
status.innerText = 'Connected to IPFS :)' | ||
|
||
const version = await node.version() | ||
|
||
log(`The IPFS node version is ${version.version}`) | ||
|
||
const filesAdded = await node.add({ | ||
path: 'hello-parcel.txt', | ||
content: Buffer.from('Hello from parcel.js bundled ipfs example') | ||
}) | ||
|
||
log(`This page deployed ${filesAdded[0].path} to IPFS and its hash is ${filesAdded[0].hash}`) | ||
|
||
const fileBuffer = await node.cat(filesAdded[0].hash) | ||
|
||
log(`The contents of the file was: ${fileBuffer.toString()}`) | ||
}) |