Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass type: "module" to new Worker() #2426

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed

- `new Worker` is now called with the `{ type: "module" }` option. Despite not being supported by NodeJS or Firefox, indicating this option is technically more correct and is necessary in order for smoldot to run with Deno. ([#2426](https://github.com/paritytech/smoldot/pull/2426))
- When a database and a chain specification checkpoint are both provided to `addChain`, the block in the database is used only if it has a higher block number than the block in the chain specification checkpoint. This makes it possible to bypass issues where smoldot is incapable of syncing over a certain block by updating the chain specification, without having to manually clear existing databases. ([#2401](https://github.com/paritytech/smoldot/pull/2401))

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion bin/wasm-node/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The code in this package uses a web worker (in browsers) or a worker thread (on
line of JavaScript that creates the worker is of the following form:

``` js
new Worker(new URL('./worker.js', import.meta.url));
new Worker(new URL('./worker.js', import.meta.url), { type: "module" });
```

This format is compatible [with Webpack 5](https://webpack.js.org/guides/web-workers/), meaning
Expand Down
12 changes: 11 additions & 1 deletion bin/wasm-node/javascript/src/worker/spawn-browser-overwrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export default function () {
// Because this line is precisely recognized by bundlers, we extract it to a separate
// JavaScript file.
// See also the README.md for more context.
const worker = new Worker(new URL('./worker.js', import.meta.url), { name: "smoldot" });

// Note that, at the time of writing, Firefox doesn't support the `type: "module"` option.
// Because browsers don't fully support modules yet, this code is expected to be run through
// a bundler (e.g. WebPack) before being given to a browser, which will remove all usage of
// modules in the worker code. It is thus also the role of this bundler to tweak or remove
// the value of this `type` property to indicate to the browser that modules aren't in use.
//
// WebPack in particular does this, but it is unclear whether *all* bundlers do it.
// Whether bundlers actually do this or not, it is nonetheless more correct to indicate
// `type: "module"` and doing so doesn't have any drawback.
const worker = new Worker(new URL('./worker.js', import.meta.url), { name: "smoldot", type: "module" });
return worker;
}
6 changes: 5 additions & 1 deletion bin/wasm-node/javascript/src/worker/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export default function () {
// Because this line is precisely recognized by bundlers, we extract it to a separate
// JavaScript file.
// See also the README.md for more context.
const worker = new Worker(new URL('./worker.js', import.meta.url));

// Note that at the time of writing of this comment, NodeJS doesn't support the
// `type: "module"` option, as modules "just work". But we put it anyway because Deno throws
// an exception if this option isn't present.
const worker = new Worker(new URL('./worker.js', import.meta.url), { type: "module" });
return worker;
}