diff --git a/README.md b/README.md index b73787334ced7e4f9bc48a5aba4f4cb5b1f7644e..80956adf569d6b7ad571ee0668c3566b20135de5 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 954310c788394484401e050e8b0eef1dbf2ca0fe..8068aaaf55710200e106b08a14a97096b254617e 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 6cb1126d554264444ef849bb7a893c554a7a0eb7..4d1676a4f9dc58ee702c4c492ef109b5e77cdddb 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 a3ee836c25ff9eaa580686d3947c8365ad8a8453..3cf7af85ec0b6d87bc55c6565814eff2f37ba402 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 32db2aa3d8c046b7fd14f6b6fb852d18b7b3440e..cd2f9181c8fc3f23a3babc93092a1dc7e6e7b972 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) {