Skip to content

Commit

Permalink
Merge branch 'module-worker'
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Sep 23, 2020
2 parents ed8af2a + 5fe442f commit fa019a7
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 9 deletions.
32 changes: 28 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ const { readFileSync } = require("fs");
const { join } = require("path");

const defaultOpts = {
marker: "comlink"
marker: "comlink",
useModuleWorker: false
};

function generateLoaderModule(path) {
function generateLoaderModule(path, { useModuleWorker = false } = {}) {
return `
import workerPath from "omt:${path}";
import {wrap} from "comlink";
export default wrap(new Worker(workerPath));
export default wrap(new Worker(workerPath${
useModuleWorker ? `, {type: "module"}` : ""
}));
`;
}

Expand Down Expand Up @@ -56,6 +59,27 @@ module.exports = function(opts = {}) {
return prefix + newId;
},

outputOptions({ format }) {
if ((format === "esm" || format === "es") && !opts.useModuleWorker) {
this.error(
`Can only use {format: "${format}"} with {useModuleWorker: true}`
);
return;
}
if (format === "amd" && opts.useModuleWorker) {
this.error(
`Can only use {useModuleWorker: true} with {format: "${format}"}`
);
return;
}
if (format !== "amd" && format !== "esm" && format != "es") {
this.error(
"Output format MUST be `amd`. `esm` is also allowed, but has very little browser support."
);
return;
}
},

load(id) {
if (id.endsWith(suffix) && !id.startsWith("omt:")) {
const wrapper = generateWorkerWrapper(id.slice(0, -suffix.length));
Expand All @@ -64,7 +88,7 @@ module.exports = function(opts = {}) {
if (!id.startsWith(prefix)) return;

const realId = id.slice(prefix.length) + suffix;
const loader = generateLoaderModule(realId);
const loader = generateLoaderModule(realId, opts);
return loader;
}
};
Expand Down
5 changes: 4 additions & 1 deletion run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ async function fileExists(file) {

async function init() {
await Promise.all(
["./tests/fixtures/simple-bundle/entry.js"].map(async input => {
[
"./tests/fixtures/simple-bundle/entry.js",
"./tests/fixtures/module-worker/entry.js"
].map(async input => {
const pathName = path.dirname(input);
const outputOptions = {
dir: path.join(pathName, "build"),
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/module-worker/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export function f() {
return "a";
}
15 changes: 15 additions & 0 deletions tests/fixtures/module-worker/build/runner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!doctype html>
<script type="module" src="entry.js"></script>
21 changes: 21 additions & 0 deletions tests/fixtures/module-worker/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import workerAPI from "comlink:./a.js";

(async function() {
console.log({ workerAPI });
const v = await workerAPI.f();
console.log({ v });
window.parent.postMessage(v);
})();
25 changes: 25 additions & 0 deletions tests/fixtures/module-worker/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const omt = require("@surma/rollup-plugin-off-main-thread");
const comlink = require("../../../");
const resolve = require("@rollup/plugin-node-resolve").nodeResolve;

module.exports = {
input: `${__dirname}/entry.js`,
output: {
dir: `${__dirname}/build`,
format: "esm"
},
plugins: [resolve(), comlink({ useModuleWorker: true }), omt()]
};
2 changes: 1 addition & 1 deletion tests/fixtures/simple-bundle/a.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/simple-bundle/entry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/simple-bundle/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const comlink = require("../../../");
const resolve = require("@rollup/plugin-node-resolve").nodeResolve;

module.exports = {
input: "./tests/fixtures/simple-bundle/entry.js",
input: `${__dirname}/entry.js`,
output: {
dir: "./tests/fixtures/simple-bundle/build",
dir: `${__dirname}/build`,
format: "amd"
},
plugins: [resolve(), comlink(), omt()]
Expand Down
54 changes: 54 additions & 0 deletions tests/module-worker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function jsBlobFromString(s) {
return URL.createObjectURL(new Blob([s], { type: "text/javascript" }));
}

// If `new Worker()` checks the `type` prop on the options object
// we can be fairly sure that the browser supports module workers.
// If so, replace we can switch `maybeIt` from ignoring tests (`xit`)
// to actually running the tests (`it`).
let maybeIt = xit;
let wasRead = false;
const detector = {
get type() {
wasRead = true;
return "module";
}
};
new Worker(jsBlobFromString("export default 4"), detector).terminate();
if (wasRead) {
maybeIt = it;
}

describe("Comlink plugin", function() {
beforeEach(function() {
this.ifr = document.createElement("iframe");
document.body.append(this.ifr);
});

afterEach(function() {
this.ifr.remove();
});

maybeIt("proxies module workers", function(done) {
window.addEventListener("message", function l(ev) {
if (ev.data === "a") {
window.removeEventListener("message", l);
done();
}
});
this.ifr.src = "/base/tests/fixtures/module-worker/build/runner.html";
});
});

0 comments on commit fa019a7

Please sign in to comment.