Skip to content

Commit

Permalink
Merge pull request #8 from taylor1791/additional-examples
Browse files Browse the repository at this point in the history
Add ES6 example
  • Loading branch information
borisirota committed May 29, 2016
2 parents f2a4a4f + d2fd2f4 commit 4ec0dab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
File renamed without changes.
12 changes: 12 additions & 0 deletions example/es6-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gamma from 'gamma';

export default function () {
self.addEventListener('message',function (ev){
var startNum = parseInt(ev.data); // ev.data=4 from main.js

setInterval(function () {
var r = startNum / Math.random() - 1;
self.postMessage([ startNum, r, gamma(r) ]);
}, 500);
});
};
19 changes: 15 additions & 4 deletions example/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
var commonWorker = require('./common-js-worker.js');
import es6Worker from './es6-worker.js';

var work = require('../');

var w = work(require('./worker.js'));
w.addEventListener('message', function (ev) {
console.log(ev.data);

var w1 = work(commonWorker);
w1.addEventListener('message', function (ev) {
console.log('CommonJS Worker:', ev.data);
});

w1.postMessage(4); // send the worker a message

var w2 = work(es6Worker);
w2.addEventListener('message', function (ev) {
console.log('ES6 Worker', ev.data);
});

w.postMessage(4); // send the worker a message
w2.postMessage(4); // send the worker a message
20 changes: 20 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
entry: {
app: ["./example/main.js"]
},
output: {
filename: "output.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['es2015']
}
}
]
}
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "launch a web worker that can require() in the browser with webpack",
"main": "index.js",
"scripts": {
"build": "webpack example/main.js output.js",
"build": "webpack --config example/webpack.config.js",
"serve": "http-server",
"example": "npm run build && npm run serve"
},
Expand All @@ -29,6 +29,9 @@
},
"homepage": "https://github.com/borisirota/webworkify-webpack",
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"gamma": "^1.0.0",
"http-server": "^0.8.5",
"webpack": "^2.0.6-beta"
Expand Down

0 comments on commit 4ec0dab

Please sign in to comment.