Skip to content

Commit

Permalink
Merge pull request #44 from surma/new-url-plain-filename
Browse files Browse the repository at this point in the history
Add support for plain filenames in `new URL`
  • Loading branch information
surma committed May 20, 2021
2 parents 7580b0c + b637f0e commit 6d912b5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,28 @@ export default {

### Auto bundling

In your project's code:
In your project's code use a module-relative path via `new URL` to include a Worker:

```js
const worker = new Worker("./worker.js", { type: "module" });
const worker = new Worker(new URL("worker.js", import.meta.url), {
type: "module"
});
```
This will just work.
If required, the plugin also supports explicitly module-relative paths:
If required, the plugin also supports plain literal paths:
```js
const worker = new Worker(new URL("./worker.js", import.meta.url), {
type: "module"
});
const worker = new Worker("./worker.js", { type: "module" });
```
However, those are less portable: in Rollup they would result in module-relative
path, but if used directly in the browser, they'll be relative to the document
URL instead.
Hence, they're deprecated and `new URL` pattern is encouraged instead for portability.
### Importing workers as URLs
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:
Expand Down
27 changes: 22 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,28 @@ This will become a hard error in the future.`,
workerFile = workerFile.slice(1, -1);

if (!/^\.{1,2}\//.test(workerFile)) {
this.warn(
`Paths passed to the Worker constructor must be relative to the current file, i.e. start with ./ or ../ (just like dynamic import!). Ignoring "${workerFile}".`,
matchIndex
);
continue;
let isError = false;
if (directWorkerFile) {
// If direct worker file, it must be in `./something` form.
isError = true;
} else {
// If `new URL(...)` it can be in `new URL('something', import.meta.url)` form too,
// so just check it's not absolute.
if (/^(\/|https?:)/.test(workerFile)) {
isError = true;
} else {
// If it does turn out to be `new URL('something', import.meta.url)` form,
// prepend `./` so that it becomes valid module specifier.
workerFile = `./${workerFile}`;
}
}
if (isError) {
this.warn(
`Paths passed to the Worker constructor must be relative to the current file, i.e. start with ./ or ../ (just like dynamic import!). Ignoring "${workerFile}".`,
matchIndex
);
continue;
}
}

workerIdPromise = this.resolve(workerFile, id).then(res => res.id);
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/url-import-meta-worker/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

// The type module should get removed for AMD format!
const w = new Worker(new URL("./worker.js", import.meta.url), {
const w = new Worker(new URL("worker.js", import.meta.url), {
type: "module"
});
w.addEventListener("message", ev => {
Expand Down

0 comments on commit 6d912b5

Please sign in to comment.