Skip to content

Commit

Permalink
Adding support for ES6 import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj Vishwani committed Apr 10, 2019
1 parent bc1edc8 commit 19e29ca
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ worker.onmessage = function (ev) {
worker.postMessage("Hello World!");
```

#### Enable ES6 import/export within Worker file
The worker helper script (helper.js):
```javascript
export const dataFormatter = (data) => {
return `${data} World!`;
};
```

The worker script (repeat.js):
```javascript
import { dataFormatter } from "./helper";

onmessage = function (ev) {
const data = dataFormatter(ev.data);
postMessage(data);
};
```

The core script:
```javascript
var Worker = require("tiny-worker");
var worker = new Worker("repeat.js", [], {esm: true});

worker.onmessage = function (ev) {
console.log(ev.data);
worker.terminate();
};

worker.postMessage("Hello");
```

#### Creating a Worker from a Function
```javascript
var Worker = require("tiny-worker");
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@
"grunt-contrib-watch": "~1.0.0",
"grunt-eslint": "~19.0.0"
},
"dependencies": {}
"dependencies": {
"esm": "^3.2.22"
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Worker {
}
});

this.child.send({input: input, isfn: isfn, cwd: options.cwd});
this.child.send({ input: input, isfn: isfn, cwd: options.cwd, esm: options.esm });
}

static setRange (min, max) {
Expand Down
11 changes: 6 additions & 5 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ function toFunction (arg) {

// Bootstraps the Worker
process.once("message", obj => {
const exp = obj.isfn ? toFunction(obj.input) : fs.readFileSync(obj.input, "utf8");
const { isfn, input, esm, cwd } = obj;
const exp = isfn ? toFunction(input) : esm ? `require("${input}");` : fs.readFileSync(input, "utf8");

global.self = {
close: () => {
process.exit(0);
},
postMessage: msg => {
process.send(JSON.stringify({data: msg}, null, 0));
process.send(JSON.stringify({ data: msg }, null, 0));
},
onmessage: void 0,
onerror: err => {
process.send(JSON.stringify({error: err.message, stack: err.stack}, null, 0));
process.send(JSON.stringify({ error: err.message, stack: err.stack }, null, 0));
},
addEventListener: (event, fn) => {
if (events.test(event)) {
Expand All @@ -33,9 +34,9 @@ process.once("message", obj => {
}
};

global.__dirname = obj.cwd;
global.__dirname = cwd;
global.__filename = __filename;
global.require = require;
global.require = esm ? require("esm")(module) : require;

global.importScripts = (...files) => {
if (files.length > 0) {
Expand Down

0 comments on commit 19e29ca

Please sign in to comment.