├── CODEOWNERS ├── tests ├── fixtures │ ├── empty.js │ ├── assets-in-worker │ │ ├── build │ │ │ ├── assets │ │ │ │ └── my-asset-620b911b.bin │ │ │ └── runner.html │ │ ├── rollup.config.js │ │ ├── entry.js │ │ └── worker.js │ ├── amd-function-name │ │ ├── config.json │ │ ├── build │ │ │ └── runner.html │ │ ├── a.js │ │ └── entry.js │ ├── public-path │ │ ├── config.json │ │ ├── build │ │ │ └── runner.html │ │ ├── entry.js │ │ └── a.js │ ├── import-worker-url-custom-scheme │ │ ├── config.json │ │ ├── build │ │ │ └── runner.html │ │ ├── b.js │ │ ├── a.js │ │ ├── worker.js │ │ └── entry.js │ ├── module-worker │ │ ├── rollup.config.js │ │ ├── build │ │ │ └── runner.html │ │ ├── worker.js │ │ ├── a.js │ │ ├── entry.js │ │ └── b.js │ ├── import-meta-worker │ │ ├── a.js │ │ ├── build │ │ │ └── runner.html │ │ └── entry.js │ ├── single-default │ │ ├── a.js │ │ ├── build │ │ │ └── runner.html │ │ └── entry.js │ ├── worker │ │ ├── build │ │ │ └── runner.html │ │ ├── worker.js │ │ ├── b.js │ │ ├── a.js │ │ └── entry.js │ ├── dynamic-import │ │ ├── build │ │ │ └── runner.html │ │ ├── entry.js │ │ └── a.js │ ├── import-meta │ │ ├── build │ │ │ └── runner.html │ │ ├── entry.js │ │ └── a.js │ ├── more-workers │ │ ├── build │ │ │ └── runner.html │ │ ├── worker_a.js │ │ ├── a.js │ │ ├── b.js │ │ ├── worker_b.js │ │ └── entry.js │ ├── simple-bundle │ │ ├── build │ │ │ └── runner.html │ │ ├── entry.js │ │ └── a.js │ ├── import-worker-url │ │ ├── build │ │ │ └── runner.html │ │ ├── b.js │ │ ├── a.js │ │ ├── worker.js │ │ └── entry.js │ └── url-import-meta-worker │ │ ├── build │ │ └── runner.html │ │ ├── worker.js │ │ ├── b.js │ │ ├── a.js │ │ └── entry.js ├── worker.test.js ├── more-workers.test.js ├── public-path.test.js ├── simple-bundle.test.js ├── dynamic-import.test.js ├── asset-in-worker.test.js ├── import-meta.test.js ├── single-default.test.js ├── amd-function-name.test.js ├── url-import-meta-worker.test.js ├── import-meta-worker.test.js ├── import-worker-url.test.js ├── import-worker-url-custom-scheme.test.js └── module-worker.test.js ├── renovate.json ├── .gitignore ├── .travis.yml ├── CONTRIBUTORS ├── Dockerfile ├── package.json ├── karma.conf.js ├── CONTRIBUTING ├── loader.ejs ├── README.md ├── run_tests.js ├── LICENSE └── index.js /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @surma 2 | -------------------------------------------------------------------------------- /tests/fixtures/empty.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["config:base"] 3 | } 4 | -------------------------------------------------------------------------------- /tests/fixtures/assets-in-worker/build/assets/my-asset-620b911b.bin: -------------------------------------------------------------------------------- 1 | assetcontent -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | tests/fixtures/*/build/*.js 2 | node_modules 3 | package-lock.json 4 | -------------------------------------------------------------------------------- /tests/fixtures/amd-function-name/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "amdFunctionName": "foo" 3 | } 4 | -------------------------------------------------------------------------------- /tests/fixtures/public-path/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "publicPath": "/base/tests/fixtures/public-path/build" 3 | } 4 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url-custom-scheme/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "urlLoaderScheme": "fancy-custom-scheme" 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | services: 4 | - docker 5 | 6 | script: 7 | - docker build -t omt . 8 | - docker run --rm -v `pwd`:/usr/src omt 9 | -------------------------------------------------------------------------------- /tests/fixtures/module-worker/rollup.config.js: -------------------------------------------------------------------------------- 1 | module.exports = (config, outputOptions, omt) => { 2 | outputOptions.format = "esm"; 3 | config.plugins = [omt()]; 4 | }; 5 | -------------------------------------------------------------------------------- /tests/fixtures/import-meta-worker/a.js: -------------------------------------------------------------------------------- 1 | let worker; 2 | 3 | if (self.document) { 4 | worker = new Worker(import.meta.url); 5 | } 6 | if (self.postMessage) { 7 | setTimeout(() => { 8 | postMessage("a"); 9 | }, 500); 10 | } 11 | 12 | export { worker }; 13 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # People who have agreed to one of the CLAs and can contribute patches. 2 | # The AUTHORS file lists the copyright holders; this file 3 | # lists people. For example, Google employees are listed here 4 | # but not in AUTHORS, because Google holds the copyright. 5 | # 6 | # https://developers.google.com/open-source/cla/individual 7 | # https://developers.google.com/open-source/cla/corporate 8 | # 9 | # Names should be added to this file as: 10 | # Name 11 | Surma 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM selenium/node-chrome:latest 2 | 3 | USER root 4 | 5 | RUN apt-get update -qqy \ 6 | && rm -rf /var/lib/apt/lists/* /var/cache/apt/* \ 7 | && rm /bin/sh && ln -s /bin/bash /bin/sh \ 8 | && chown seluser /usr/local 9 | 10 | ENV NVM_DIR /usr/local/nvm 11 | RUN mkdir -p $NVM_DIR \ 12 | && wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash \ 13 | && source $NVM_DIR/nvm.sh \ 14 | && nvm install v12 15 | 16 | ENV CHROME_BIN /opt/google/chrome/chrome 17 | ENV INSIDE_DOCKER=1 18 | 19 | WORKDIR /usr/src 20 | ENTRYPOINT source $NVM_DIR/nvm.sh && npm i && npm test -------------------------------------------------------------------------------- /tests/fixtures/assets-in-worker/rollup.config.js: -------------------------------------------------------------------------------- 1 | const MARKER = "my-special-import"; 2 | module.exports = (config, outputOptions, omt) => { 3 | config.plugins = [ 4 | omt(), 5 | { 6 | resolveId(id) { 7 | if (id !== MARKER) { 8 | return; 9 | } 10 | return id; 11 | }, 12 | load(id) { 13 | if (id !== MARKER) { 14 | return; 15 | } 16 | const referenceId = this.emitFile({ 17 | type: "asset", 18 | name: "my-asset.bin", 19 | source: "assetcontent" 20 | }); 21 | return `export default import.meta.ROLLUP_FILE_URL_${referenceId}`; 22 | } 23 | } 24 | ]; 25 | }; 26 | -------------------------------------------------------------------------------- /tests/fixtures/single-default/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default 4; 15 | -------------------------------------------------------------------------------- /tests/fixtures/worker/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/dynamic-import/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/import-meta/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/more-workers/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/public-path/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/simple-bundle/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/single-default/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/worker/worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import a from "./a.js"; 15 | 16 | a(); 17 | -------------------------------------------------------------------------------- /tests/fixtures/amd-function-name/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/assets-in-worker/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/import-meta-worker/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/import-meta/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import("./a.js").then(m => m.default()); 15 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/public-path/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import("./a.js").then(m => m.default()); 15 | -------------------------------------------------------------------------------- /tests/fixtures/simple-bundle/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import A from "./a.js"; 15 | 16 | A(); 17 | -------------------------------------------------------------------------------- /tests/fixtures/dynamic-import/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import("./a.js").then(m => m.default()); 15 | -------------------------------------------------------------------------------- /tests/fixtures/more-workers/worker_a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import a from "./a.js"; 15 | 16 | a(); 17 | -------------------------------------------------------------------------------- /tests/fixtures/url-import-meta-worker/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/worker/b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | return "a"; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/module-worker/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/url-import-meta-worker/worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import a from "./a.js"; 15 | 16 | a(); 17 | -------------------------------------------------------------------------------- /tests/fixtures/amd-function-name/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | return 4; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url-custom-scheme/build/runner.html: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url/b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | return 1; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/more-workers/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | self.postMessage("a"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/more-workers/b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | self.postMessage("b"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/url-import-meta-worker/b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | return "a"; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url-custom-scheme/b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | return 1; 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | self.postMessage("a"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url/worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import a from "./a.js"; 15 | 16 | a(); 17 | import("./b.js"); 18 | -------------------------------------------------------------------------------- /tests/fixtures/single-default/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import A from "./a.js"; 15 | 16 | window.parent.postMessage(A, "*"); 17 | -------------------------------------------------------------------------------- /tests/fixtures/dynamic-import/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | window.parent.postMessage("a", "*"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/public-path/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | window.parent.postMessage("a", "*"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/simple-bundle/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | window.parent.postMessage("a", "*"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url-custom-scheme/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | self.postMessage("a"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-meta/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default function() { 15 | window.parent.postMessage(import.meta.url, "*"); 16 | } 17 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url-custom-scheme/worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import a from "./a.js"; 15 | 16 | a(); 17 | import("./b.js"); 18 | -------------------------------------------------------------------------------- /tests/fixtures/module-worker/worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import a from "./a.js"; 15 | import("./b.js").then(({default: f}) => { 16 | a(f); 17 | }) 18 | -------------------------------------------------------------------------------- /tests/fixtures/more-workers/worker_b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | async function run() { 15 | const { default: b } = await import("./b.js"); 16 | b(); 17 | } 18 | run(); 19 | -------------------------------------------------------------------------------- /tests/fixtures/worker/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default async function() { 15 | const { default: f } = await import("./b.js"); 16 | self.postMessage(f()); 17 | } 18 | -------------------------------------------------------------------------------- /tests/fixtures/url-import-meta-worker/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default async function() { 15 | const { default: f } = await import("./b.js"); 16 | self.postMessage(f()); 17 | } 18 | -------------------------------------------------------------------------------- /tests/fixtures/assets-in-worker/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | const w = new Worker("./worker.js"); 15 | w.addEventListener("message", ev => { 16 | window.parent.postMessage(ev.data, "*"); 17 | }); 18 | -------------------------------------------------------------------------------- /tests/fixtures/assets-in-worker/worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import url from "my-special-import"; 15 | 16 | fetch(url) 17 | .then(resp => resp.text()) 18 | .then(text => postMessage(text)); 19 | -------------------------------------------------------------------------------- /tests/fixtures/module-worker/a.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | export default async function(f) { 15 | self.postMessage(f()); 16 | } 17 | 18 | export function someValue() { 19 | return self.String("a"); 20 | } 21 | -------------------------------------------------------------------------------- /tests/fixtures/module-worker/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | const w = new Worker("./worker.js", { type: "module" }); 15 | w.addEventListener("message", ev => { 16 | window.parent.postMessage(ev.data, "*"); 17 | }); 18 | -------------------------------------------------------------------------------- /tests/fixtures/amd-function-name/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import("./a.js").then(m => { 15 | if (m.default() != 4) { 16 | return; 17 | } 18 | if (!self.foo) { 19 | return; 20 | } 21 | window.parent.postMessage("a", "*"); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/fixtures/worker/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | // The type module should get removed for AMD format! 15 | const w = new Worker("./worker.js", { type: "module" }); 16 | w.addEventListener("message", ev => { 17 | window.parent.postMessage(ev.data, "*"); 18 | }); 19 | -------------------------------------------------------------------------------- /tests/fixtures/module-worker/b.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | // Import for side-effects (of which there are none) 15 | // to make sure that a.js gets its own chunk. 16 | import {someValue} from "./a.js"; 17 | 18 | export default function() { 19 | return someValue(); 20 | } 21 | -------------------------------------------------------------------------------- /tests/fixtures/url-import-meta-worker/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | // The type module should get removed for AMD format! 15 | const w = new Worker(new URL("worker.js", import.meta.url), { 16 | type: "module" 17 | }); 18 | w.addEventListener("message", ev => { 19 | window.parent.postMessage(ev.data, "*"); 20 | }); 21 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | import workerURL from "omt:./worker.js"; 14 | 15 | // Set this, so it can be picked up in the test. 16 | self.workerURL = workerURL; 17 | 18 | const w = new Worker(workerURL); 19 | w.addEventListener("message", ev => { 20 | window.parent.postMessage(ev.data, "*"); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/fixtures/import-meta-worker/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | import { worker } from "./a.js"; 14 | if (worker) { 15 | worker.addEventListener("message", ev => { 16 | window.parent.postMessage(ev.data, "*"); 17 | }); 18 | } 19 | // This should not get bundled into the worker module 20 | if (self.postMessage) { 21 | postMessage("xxx"); 22 | } 23 | -------------------------------------------------------------------------------- /tests/fixtures/import-worker-url-custom-scheme/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | import workerURL from "fancy-custom-scheme:./worker.js"; 14 | 15 | // Set this, so it can be picked up in the test. 16 | self.workerURL = workerURL; 17 | 18 | const w = new Worker(workerURL); 19 | w.addEventListener("message", ev => { 20 | window.parent.postMessage(ev.data, "*"); 21 | }); 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@surma/rollup-plugin-off-main-thread", 3 | "version": "2.2.3", 4 | "description": "Use Rollup with workers and ES6 modules today.", 5 | "main": "index.js", 6 | "scripts": { 7 | "fmt": "prettier --write 'tests/**/*.js' *.js *.md *.json", 8 | "test": "node ./run_tests.js" 9 | }, 10 | "author": "Surma ", 11 | "license": "Apache-2.0", 12 | "devDependencies": { 13 | "chai": "4.2.0", 14 | "chalk": "^2.4.2", 15 | "karma": "4.2.0", 16 | "karma-chai": "0.1.0", 17 | "karma-chrome-launcher": "3.0.0", 18 | "karma-firefox-launcher": "1.1.0", 19 | "karma-mocha": "1.3.0", 20 | "karma-safari-launcher": "1.0.0", 21 | "karma-safaritechpreview-launcher": "2.0.2", 22 | "mocha": "6.1.4", 23 | "prettier": "1.18.2", 24 | "rollup": "2.2.0" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "https://github.com/surma/rollup-plugin-off-main-thread" 29 | }, 30 | "dependencies": { 31 | "ejs": "^3.1.6", 32 | "json5": "^2.2.0", 33 | "magic-string": "^0.25.0", 34 | "string.prototype.matchall": "^4.0.6" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/fixtures/more-workers/entry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | async function run() { 15 | await new Promise(resolve => { 16 | const w = new Worker("./worker_a.js"); 17 | w.addEventListener("message", ev => { 18 | if (ev.data === "a") { 19 | resolve(); 20 | } 21 | }); 22 | }); 23 | await new Promise(resolve => { 24 | const w = new Worker("./worker_b.js"); 25 | w.addEventListener("message", ev => { 26 | if (ev.data === "b") { 27 | resolve(); 28 | } 29 | }); 30 | }); 31 | window.parent.postMessage("a", "*"); 32 | } 33 | 34 | run(); 35 | -------------------------------------------------------------------------------- /tests/worker.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Worker", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads transpiled modules", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/worker/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/more-workers.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Worker", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("can load multiple workers", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/more-workers/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/public-path.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Public Path", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads transpiled modules", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/public-path/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/simple-bundle.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Simple bundle", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads transpiled modules", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/simple-bundle/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/dynamic-import.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Dynamic import", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads transpiled modules", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/dynamic-import/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/asset-in-worker.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Assets in workers", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("can be fetched", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "assetcontent") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/assets-in-worker/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/import-meta.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("import.meta", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads transpiled modules", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (/^https?:\/\/.+\.js$/.test(ev.data)) { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/import-meta/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/single-default.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Single default const export", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads transpiled modules", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === 4) { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/single-default/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/amd-function-name.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("AMD Function Name", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads with a different AMD function name", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = "/base/tests/fixtures/amd-function-name/build/runner.html"; 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/url-import-meta-worker.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Worker", function() { 15 | beforeEach(function() { 16 | this.ifr = document.createElement("iframe"); 17 | document.body.append(this.ifr); 18 | }); 19 | 20 | afterEach(function() { 21 | this.ifr.remove(); 22 | }); 23 | 24 | it("loads module-relative worker paths", function(done) { 25 | window.addEventListener("message", function l(ev) { 26 | if (ev.data === "a") { 27 | window.removeEventListener("message", l); 28 | done(); 29 | } 30 | }); 31 | this.ifr.src = 32 | "/base/tests/fixtures/url-import-meta-worker/build/runner.html"; 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/import-meta-worker.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("omt: use import.meta.url in Worker constructor", function() { 15 | const runner = "/base/tests/fixtures/import-meta-worker/build/runner.html"; 16 | 17 | beforeEach(function() { 18 | this.ifr = document.createElement("iframe"); 19 | document.body.append(this.ifr); 20 | }); 21 | 22 | afterEach(function() { 23 | this.ifr.remove(); 24 | }); 25 | 26 | it("loads itself as a module", function(done) { 27 | window.addEventListener("message", function l(ev) { 28 | if (ev.data === "a") { 29 | window.removeEventListener("message", l); 30 | done(); 31 | } else { 32 | done(`Unexpected message! ${ev.data}`); 33 | } 34 | }); 35 | this.ifr.src = runner; 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | module.exports = function(config) { 15 | const configuration = { 16 | basePath: "", 17 | frameworks: ["mocha", "chai"], 18 | files: [ 19 | { 20 | pattern: "tests/fixtures/**", 21 | included: false 22 | }, 23 | { 24 | pattern: "tests/*.test.js" 25 | } 26 | ], 27 | reporters: ["progress"], 28 | port: 9876, 29 | colors: true, 30 | logLevel: config.LOG_INFO, 31 | autoWatch: true, 32 | singleRun: true, 33 | concurrency: 1, 34 | browsers: ["Chrome", "Firefox", "Safari"], 35 | customLaunchers: { 36 | DockerChrome: { 37 | base: "ChromeHeadless", 38 | flags: ["--no-sandbox"] 39 | } 40 | } 41 | }; 42 | 43 | if (process.env.INSIDE_DOCKER) configuration.browsers = ["DockerChrome"]; 44 | 45 | config.set(configuration); 46 | }; 47 | -------------------------------------------------------------------------------- /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at the end). 2 | 3 | ### Before you contribute 4 | Before we can use your code, you must sign the 5 | [Google Individual Contributor License Agreement] 6 | (https://cla.developers.google.com/about/google-individual) 7 | (CLA), which you can do online. The CLA is necessary mainly because you own the 8 | copyright to your changes, even after your contribution becomes part of our 9 | codebase, so we need your permission to use and distribute your code. We also 10 | need to be sure of various other things—for instance that you'll tell us if you 11 | know that your code infringes on other people's patents. You don't have to sign 12 | the CLA until after you've submitted your code for review and a member has 13 | approved it, but you must do it before we can put your code into our codebase. 14 | Before you start working on a larger contribution, you should get in touch with 15 | us first through the issue tracker with your idea so that we can help out and 16 | possibly guide you. Coordinating up front makes it much easier to avoid 17 | frustration later on. 18 | 19 | ### Code reviews 20 | All submissions, including submissions by project members, require review. We 21 | use Github pull requests for this purpose. 22 | 23 | ### The small print 24 | Contributions made by corporations are covered by a different agreement than 25 | the one above, the 26 | [Software Grant and Corporate Contributor License Agreement] 27 | (https://cla.developers.google.com/about/google-corporate). 28 | -------------------------------------------------------------------------------- /tests/import-worker-url.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("omt: URL import", function() { 15 | const runner = "/base/tests/fixtures/import-worker-url/build/runner.html"; 16 | 17 | beforeEach(function() { 18 | this.ifr = document.createElement("iframe"); 19 | document.body.append(this.ifr); 20 | }); 21 | 22 | afterEach(function() { 23 | this.ifr.remove(); 24 | }); 25 | 26 | it("imports as string", async function() { 27 | this.ifr.src = runner; 28 | await new Promise(resolve => this.ifr.addEventListener("load", resolve)); 29 | assert.typeOf(this.ifr.contentWindow.workerURL, "string"); 30 | }); 31 | 32 | it("loads transpiled modules", function(done) { 33 | window.addEventListener("message", function l(ev) { 34 | if (ev.data === "a") { 35 | window.removeEventListener("message", l); 36 | done(); 37 | } 38 | }); 39 | this.ifr.src = runner; 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /tests/import-worker-url-custom-scheme.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("omt: URL import with custom scheme", function() { 15 | const runner = 16 | "/base/tests/fixtures/import-worker-url-custom-scheme/build/runner.html"; 17 | 18 | beforeEach(function() { 19 | this.ifr = document.createElement("iframe"); 20 | document.body.append(this.ifr); 21 | }); 22 | 23 | afterEach(function() { 24 | this.ifr.remove(); 25 | }); 26 | 27 | it("imports as string", async function() { 28 | this.ifr.src = runner; 29 | await new Promise(resolve => this.ifr.addEventListener("load", resolve)); 30 | assert.typeOf(this.ifr.contentWindow.workerURL, "string"); 31 | }); 32 | 33 | it("loads transpiled modules", function(done) { 34 | window.addEventListener("message", function l(ev) { 35 | if (ev.data === "a") { 36 | window.removeEventListener("message", l); 37 | done(); 38 | } 39 | }); 40 | this.ifr.src = runner; 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/module-worker.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | describe("Module Worker", function() { 15 | const blob = new Blob([""], { type: "text/javascript" }); 16 | const url = URL.createObjectURL(blob); 17 | let supportsModuleWorker = false; 18 | const options = { 19 | get type() { 20 | supportsModuleWorker = true; 21 | } 22 | }; 23 | new Worker(url, options).terminate(); 24 | URL.revokeObjectURL(url); 25 | 26 | beforeEach(function() { 27 | if (!supportsModuleWorker) { 28 | return this.skip(); 29 | } 30 | this.ifr = document.createElement("iframe"); 31 | document.body.append(this.ifr); 32 | }); 33 | 34 | afterEach(function() { 35 | this.ifr.remove(); 36 | }); 37 | 38 | it("work", function(done) { 39 | window.addEventListener("message", function l(ev) { 40 | if (ev.data === "a") { 41 | window.removeEventListener("message", l); 42 | done(); 43 | } 44 | }); 45 | this.ifr.src = "/base/tests/fixtures/module-worker/build/runner.html"; 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /loader.ejs: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | // If the loader is already loaded, just stop. 15 | if (!self.<%- amdFunctionName %>) { 16 | let registry = {}; 17 | 18 | // Used for `eval` and `importScripts` where we can't get script URL by other means. 19 | // In both cases, it's safe to use a global var because those functions are synchronous. 20 | let nextDefineUri; 21 | 22 | const singleRequire = (uri, parentUri) => { 23 | uri = new URL(uri + ".js", parentUri).href; 24 | return registry[uri] || ( 25 | <% if (useEval) { %> 26 | fetch(uri) 27 | .then(resp => resp.text()) 28 | .then(code => { 29 | nextDefineUri = uri; 30 | eval(code); 31 | }) 32 | <% } else { %> 33 | new Promise(resolve => { 34 | if ("document" in self) { 35 | const script = document.createElement("script"); 36 | script.src = uri; 37 | script.onload = resolve; 38 | document.head.appendChild(script); 39 | } else { 40 | nextDefineUri = uri; 41 | importScripts(uri); 42 | resolve(); 43 | } 44 | }) 45 | <% } %> 46 | .then(() => { 47 | let promise = registry[uri]; 48 | if (!promise) { 49 | throw new Error(`Module ${uri} didn’t register its module`); 50 | } 51 | return promise; 52 | }) 53 | ); 54 | }; 55 | 56 | self.<%- amdFunctionName %> = (depsNames, factory) => { 57 | const uri = nextDefineUri || ("document" in self ? document.currentScript.src : "") || location.href; 58 | if (registry[uri]) { 59 | // Module is already loading or loaded. 60 | return; 61 | } 62 | let exports = {}; 63 | const require = depUri => singleRequire(depUri, uri); 64 | const specialDeps = { 65 | module: { uri }, 66 | exports, 67 | require 68 | }; 69 | registry[uri] = Promise.all(depsNames.map( 70 | depName => specialDeps[depName] || require(depName) 71 | )).then(deps => { 72 | factory(...deps); 73 | return exports; 74 | }); 75 | }; 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-off-main-thread 2 | 3 | Use Rollup with workers and ES6 modules _today_. 4 | 5 | ``` 6 | $ npm install --save @surma/rollup-plugin-off-main-thread 7 | ``` 8 | 9 | Workers are JavaScript’s version of threads. [Workers are important to use][when workers] as the main thread is already overloaded, especially on slower or older devices. 10 | 11 | This plugin takes care of shimming module support in workers and allows you to use `new Worker()`. 12 | 13 | OMT is the result of merging loadz0r and workz0r. 14 | 15 | ## Usage 16 | 17 | I set up [a gist] to show a full setup with OMT. 18 | 19 | ### Config 20 | 21 | ```js 22 | // rollup.config.js 23 | import OMT from "@surma/rollup-plugin-off-main-thread"; 24 | 25 | export default { 26 | input: ["src/main.js"], 27 | output: { 28 | dir: "dist", 29 | // You _must_ use either “amd” or “esm” as your format. 30 | // But note that only very few browsers have native support for 31 | // modules in workers. 32 | format: "amd" 33 | }, 34 | plugins: [OMT()] 35 | }; 36 | ``` 37 | 38 | ### Auto bundling 39 | 40 | In your project's code use a module-relative path via `new URL` to include a Worker: 41 | 42 | ```js 43 | const worker = new Worker(new URL("worker.js", import.meta.url), { 44 | type: "module" 45 | }); 46 | ``` 47 | 48 | This will just work. 49 | 50 | If required, the plugin also supports plain literal paths: 51 | 52 | ```js 53 | const worker = new Worker("./worker.js", { type: "module" }); 54 | ``` 55 | 56 | However, those are less portable: in Rollup they would result in module-relative 57 | path, but if used directly in the browser, they'll be relative to the document 58 | URL instead. 59 | 60 | Hence, they're deprecated and `new URL` pattern is encouraged instead for portability. 61 | 62 | ### Importing workers as URLs 63 | 64 | If your worker constructor doesn't match `workerRegexp` (see options below), you might find it easier to import the worker as a URL. In your project's code: 65 | 66 | ```js 67 | import workerURL from "omt:./worker.js"; 68 | import paintWorkletURL from "omt:./paint-worklet.js"; 69 | 70 | const worker = new Worker(workerURL, { name: "main-worker" }); 71 | CSS.paintWorklet.addModule(paintWorkletURL); 72 | ``` 73 | 74 | `./worker.js` and `./paint-worklet.js` will be added to the output as chunks. 75 | 76 | ## Options 77 | 78 | ```js 79 | { 80 | // ... 81 | plugins: [OMT(options)]; 82 | } 83 | ``` 84 | 85 | - `loader`: A string containing the EJS template for the amd loader. If `undefined`, OMT will use `loader.ejs`. 86 | - `useEval`: Use `fetch()` + `eval()` to load dependencies instead of `