Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Add webpack example #421

Merged
merged 1 commit into from
Nov 17, 2016
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ var ipfs = ipfsAPI({host: 'localhost', port: '5001', procotol: 'http'})

Same as in Node.js, you just have to [browserify](http://browserify.org) the code before serving it. See the browserify repo for how to do that.

See the example at the [examples folder](/examples/bundle-browserify) to get a boilerplate.
See the example in the [examples folder](/examples/bundle-browserify) to get a boilerplate.

### In a web browser through webpack

See the example in the [examples folder](/examples/bundle-webpack) to get an idea on how to use js-ipfs-api with webpack

### In a web browser from CDN

Expand Down
3 changes: 3 additions & 0 deletions examples/bundle-webpack/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stage": 0
}
11 changes: 11 additions & 0 deletions examples/bundle-webpack/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "standard",
"rules": {
"react/jsx-uses-react": 2,
"react/jsx-uses-vars": 2,
"react/react-in-jsx-scope": 2
},
"plugins": [
"react"
]
}
4 changes: 4 additions & 0 deletions examples/bundle-webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
.DS_Store
dist
Binary file added examples/bundle-webpack/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions examples/bundle-webpack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Bundle js-ipfs-api with Webpack!

> In this example, you will find a boilerplate you can use to guide yourself into bundling js-ipfs-api with webpack, so that you can use it in your own web app!

## Setup

As for any js-ipfs-api example, **you need a running IPFS daemon**, you learn how to do that here:

- [Spawn a go-ipfs daemon](https://ipfs.io/docs/getting-started/)
- [Spawn a js-ipfs daemon](https://github.com/ipfs/js-ipfs#usage)

**Note:** If you load your app from a different domain than the one the daemon is running (most probably), you will need to set up CORS, see https://github.com/ipfs/js-ipfs-api#cors to learn how to do that.

A quick (and dirty way to get it done) is:

```bash
> ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"
> ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
```

## Run this example

Once the daemon is on, run the following commands within this folder:

```bash
> npm install
> npm start
```

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

You should see the following:

![](https://ipfs.io/ipfs/QmZndNLRct3co7h1yVB72S4qfwAwbq7DQghCpWpVQ45jSi/1.png)

10 changes: 10 additions & 0 deletions examples/bundle-webpack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>Sample App</title>
</head>
<body>
<div id='root'>
</div>
<script src="/static/bundle.js"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/bundle-webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "bundle-browserify",
"version": "1.0.0",
"description": "Bundle js-ipfs-api with Browserify",
"scripts": {
"start": "node server.js"
},
"author": "Victor Bjelkholm <[email protected]>",
"license": "MIT",
"keywords": [],
"devDependencies": {
"babel-core": "^5.4.7",
"babel-loader": "^5.1.2",
"ipfs-api": "^11.1.0",
"json-loader": "^0.5.3",
"react": "^0.13.0",
"react-hot-loader": "^1.3.0",
"webpack": "^1.9.6",
"webpack-dev-server": "^1.8.2"
}
}
16 changes: 16 additions & 0 deletions examples/bundle-webpack/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'
var webpack = require('webpack')
var WebpackDevServer = require('webpack-dev-server')
var config = require('./webpack.config')

new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
if (err) {
console.log(err)
}

console.log('Listening at localhost:3000')
})
63 changes: 63 additions & 0 deletions examples/bundle-webpack/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'
const React = require('react')
const ipfsAPI = require('ipfs-api')

const ipfs = ipfsAPI('localhost', '5001')
const stringToUse = 'hello world from webpacked IPFS'

class App extends React.Component {
constructor (props) {
super(props)
this.state = {
id: null,
version: null,
protocol_version: null,
added_file_hash: null,
added_file_contents: null
}
}
componentDidMount () {
ipfs.id((err, res) => {
if (err) throw err
this.setState({
id: res.id,
version: res.agentVersion,
protocol_version: res.protocolVersion
})
})
ipfs.add([new Buffer(stringToUse)], (err, res) => {
if (err) throw err
const hash = res[0].hash
this.setState({added_file_hash: hash})
ipfs.cat(hash, (err, res) => {
if (err) throw err
let data = ''
res.on('data', (d) => {
data = data + d
})
res.on('end', () => {
this.setState({added_file_contents: data})
})
})
})
}
render () {
return <div style={{textAlign: 'center'}}>
<h1>Everything is working!</h1>
<p>Your ID is <strong>{this.state.id}</strong></p>
<p>Your IPFS version is <strong>{this.state.version}</strong></p>
<p>Your IPFS protocol version is <strong>{this.state.protocol_version}</strong></p>
<div>
<div>
Added a file! <br />
{this.state.added_file_hash}
</div>
<div>
Contents of this file: <br />
{this.state.added_file_contents}
</div>
</div>
</div>
}
}
module.exports = App
5 changes: 5 additions & 0 deletions examples/bundle-webpack/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'
const React = require('react')
const App = require('./App')

React.render(<App />, document.getElementById('root'))
31 changes: 31 additions & 0 deletions examples/bundle-webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var path = require('path')
var webpack = require('webpack')

module.exports = {
devtool: 'eval',
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./src/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [{
test: /\.js$/,
loaders: ['react-hot', 'babel'],
include: path.join(__dirname, 'src')
}, { test: /\.json$/, loader: 'json-loader' }]
},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"aegir": "^9.1.2",
"chai": "^3.5.0",
"eslint-plugin-react": "^6.7.1",
"gulp": "^3.9.1",
"hapi": "^15.2.0",
"interface-ipfs-core": "^0.18.3",
Expand Down