From 19e29ca2ca8cee049f2ce774a49dec4275a861f5 Mon Sep 17 00:00:00 2001 From: Pankaj Vishwani <pavishwa@cisco.com> Date: Wed, 10 Apr 2019 15:41:43 -0700 Subject: [PATCH] Adding support for ES6 import/export --- README.md | 31 +++++++++++++++++++++++++++++++ package-lock.json | 5 +++++ package.json | 4 +++- src/index.js | 2 +- src/worker.js | 11 ++++++----- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b737873..3e8c77a 100644 --- a/README.md +++ b/README.md @@ -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"); diff --git a/package-lock.json b/package-lock.json index 954310c..8068aaa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1273,6 +1273,11 @@ } } }, + "esm": { + "version": "3.2.22", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.22.tgz", + "integrity": "sha512-z8YG7U44L82j1XrdEJcqZOLUnjxco8pO453gKOlaMD1/md1n/5QrscAmYG+oKUspsmDLuBFZrpbxI6aQ67yRxA==" + }, "espree": { "version": "3.5.3", "resolved": "https://registry.npmjs.org/espree/-/espree-3.5.3.tgz", diff --git a/package.json b/package.json index 6cb1126..4d1676a 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,7 @@ "grunt-contrib-watch": "~1.0.0", "grunt-eslint": "~19.0.0" }, - "dependencies": {} + "dependencies": { + "esm": "^3.2.22" + } } diff --git a/src/index.js b/src/index.js index a3ee836..3cf7af8 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { diff --git a/src/worker.js b/src/worker.js index 32db2aa..cd2f918 100644 --- a/src/worker.js +++ b/src/worker.js @@ -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)) { @@ -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) { -- GitLab