Skip to content

feat(harper.js): export both binary and inlinedBinary for different runtimes#607

Merged
elijah-potter merged 27 commits intoAutomattic:masterfrom
Asuka109:feat/harper-binary-formats
Mar 13, 2025
Merged

feat(harper.js): export both binary and inlinedBinary for different runtimes#607
elijah-potter merged 27 commits intoAutomattic:masterfrom
Asuka109:feat/harper-binary-formats

Conversation

@Asuka109
Copy link
Copy Markdown
Contributor

@Asuka109 Asuka109 commented Feb 6, 2025

This pull request includes several changes to improve the Harper.js project, focusing on updating dependencies, modifying test configurations, and refactoring the code to use the new harper-wasm package. The most important changes include updating the package.json dependencies, modifying the test scripts, and refactoring the Linter and LocalLinter classes to use the new binary module.

Dependency and configuration updates:

  • packages/harper.js/package.json: Updated dependencies to use harper-wasm instead of wasm, added new test scripts, and updated the versions of several development dependencies. Added sideEffects field so that all the exports can be removed by bundler treeshaking.

Refactoring and code improvements:

  • packages/harper.js/src/binary.ts: Migrated from src/WorkerLinter/communication.ts and src/loadWasm.ts, it provides BinaryModule as a wrapper for the raw WebAssembly module, bringing more readable method names and additional features. It also exports binary and binaryInlined to load the WebAssembly file in different ways.
  • packages/harper.js/src/Linter.ts: Updated imports to use harper-wasm and added a new LinterInit interface that includes the binary module. [1] [2]
  • packages/harper.js/src/LocalLinter.ts: Refactored the LocalLinter class to use the new binary module for WebAssembly operations, removing the old loadWasm method. Now it should be initialized with passing a BinaryModule.
  • packages/harper.js/src/WorkerLinter/index.ts: Updated the WorkerLinter class to use the new binary module and refactored the worker communication setup.

Testing improvements:

@Asuka109 Asuka109 marked this pull request as draft February 6, 2025 16:40
@elijah-potter
Copy link
Copy Markdown
Collaborator

Hey @Asuka109, just a heads up. If you want the precommit job to ✔️, you need to run just format and commit the results.

@Asuka109
Copy link
Copy Markdown
Contributor Author

Asuka109 commented Feb 13, 2025

@elijah-potter Got it. I missed your comment last week. I tried it just now, but it seems it won't resolve the ESLint issues. Also, I noticed that VSCode cannot resolve the .eslintrc file and isn't linting the problems. I can improve that in the next PR.

@Asuka109
Copy link
Copy Markdown
Contributor Author

Asuka109 commented Feb 13, 2025

@elijah-potter It seems like there is something wrong with the cases running in Firefox. I will handle it later. There are quite a lot of changes to the API. Could you please take a look at the changes and let me know if anything needs to be changed?

Copy link
Copy Markdown
Collaborator

@elijah-potter elijah-potter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've made a lot of changes here. Some of the reasoning needs a bit more explanation.

Comment thread packages/harper.js/src/utils.ts Outdated
Comment thread packages/harper.js/src/binary.ts
Comment thread packages/harper.js/src/LocalLinter.ts
@Asuka109 Asuka109 marked this pull request as ready for review February 15, 2025 12:33
@Asuka109
Copy link
Copy Markdown
Contributor Author

@elijah-potter I think everything is ready now.

@Asuka109
Copy link
Copy Markdown
Contributor Author

@elijah-potter I found that initializing binary makes Node.js programs panic, even when only binaryInlined is used. Please hold off on reviewing this until I fix it.

@elijah-potter
Copy link
Copy Markdown
Collaborator

Would it be alright if I took a look at it? Is there a reproducible example I can work off?

@Asuka109
Copy link
Copy Markdown
Contributor Author

Asuka109 commented Mar 5, 2025

@elijah-potter It just happened when I tried to require harper.js in a Node.js script. I will go back and work on this MR.

@Asuka109
Copy link
Copy Markdown
Contributor Author

@elijah-potter Sorry for keeping you waiting. I've made some commits, and now it's ready for review!

@Asuka109
Copy link
Copy Markdown
Contributor Author

When exporting both binary and binaryInlined, treeshaking can only remove the unused one when using a bundler. However, I forgot that Node.js runtime doesn't execute treeshaking. It caused an error in the binary constructor when harper_wasm.js attempted to fetch the wasm module from the binary URL. So in the latest commit, binary has been changed to initialize lazily.

@Asuka109
Copy link
Copy Markdown
Contributor Author

export const loadBinary = pMemoize(async (binary: string) => {
const exports = await import('harper-wasm');
let input: InitInput;
if (typeof process !== 'undefined' && binary.startsWith('file://')) {
const fs = await import(/* webpackIgnore: true */ /* @vite-ignore */ 'fs');
input = new Promise((resolve, reject) => {
fs.readFile(new URL(binary).pathname, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
} else {
input = binary;
}
await exports.default({ module_or_path: input });
return exports;
});

In the implementation of loadBinary, I had to use the typeof process expression to determine if it's a Node.js environment to prevent harper_wasm.js executing fetch(module_or_path). It's hard to find a better way because I've noticed that many JavaScript libraries using wasm don't support both Node.js and web platforms. As for the binary parameter in new Linter({ binary }), I've thought of a way to make it optional, which I'll introduce in a later PR.

@elijah-potter
Copy link
Copy Markdown
Collaborator

It's hard to find a better way because I've noticed that many JavaScript libraries using wasm don't support both Node.js and web platforms.

Yeah, it's an unfortunate circumstance to find ourselves in. Do you think

As for the binary parameter in new Linter({ binary }), I've thought of a way to make it optional, which I'll introduce in a later PR.

Nice! Keep me updated.

I can't take a look at this PR right now. I'll circle back later today.

@Asuka109
Copy link
Copy Markdown
Contributor Author

@elijah-potter I found the way to load binary in different ways according to the platform. Here's the demo: https://github.com/Asuka109/binary-mutiple-platform

@elijah-potter
Copy link
Copy Markdown
Collaborator

@elijah-potter I found the way to load binary in different ways according to the platform. Here's the demo: https://github.com/Asuka109/binary-mutiple-platform

Good idea! Let's make that part of a different PR though. I've gone ahead and fixed the WordPress plugin to use the API changes. I think this is ready to merge!

@elijah-potter elijah-potter added this pull request to the merge queue Mar 13, 2025
Merged via the queue into Automattic:master with commit 38ea4c8 Mar 13, 2025
22 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Mar 24, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [Automattic/harper/harper-ls](https://github.com/Automattic/harper) | minor | `v0.24.0` -> `v0.26.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>Automattic/harper (Automattic/harper/harper-ls)</summary>

### [`v0.26.0`](https://github.com/Automattic/harper/releases/tag/v0.26.0)

[Compare Source](Automattic/harper@v0.25.1...v0.26.0)

#### What's Changed

-   docs: fix user dictionary by [@&#8203;kit494way](https://github.com/kit494way) in Automattic/harper#893
-   feat: mask out comments beginning with spellchecker:ignore by [@&#8203;grantlemons](https://github.com/grantlemons) in Automattic/harper#861
-   feat(harper.js): export both binary and inlinedBinary for different runtimes by [@&#8203;Asuka109](https://github.com/Asuka109) in Automattic/harper#607
-   feat: linter for "as far back as" to replace "as early back as" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#889
-   feat: flag "explanation mark/point" instead of "exclamation" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#895
-   feat: correct "in anyway" to "in any way" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#894
-   build(deps): bump [@&#8203;babel/helpers](https://github.com/babel/helpers) from 7.26.9 to 7.26.10 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#899
-   fix: two spelling mistakes based on homophones by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#886
-   feat: allow blank lines and comments in `dictionary.dict` by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#756
-   docs: fix typo [#&#8203;906](Automattic/harper#906) by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#912
-   hotfix(core): properly store spans in `PatternLinter` cache by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#926
-   Dictionary curation 2025 03 12 by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#902
-   Dialect prototyping by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#925
-   feat: insert newline automatically in `just addnoun` by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#931
-   docs: fix 3 grammar mistakes by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#933
-   feat: linter for "each and everyone" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#923
-   feat: expand the "get rid off" lint to cover "get ride of" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#900
-   fix(vscode-plugin): ignore non-existent ".git" files, support untitled/unsaved files on VS Code by [@&#8203;kiding](https://github.com/kiding) in Automattic/harper#927
-   feat(core): improve assertion to allow overlapping suggestions by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#935
-   build(deps): bump [@&#8203;wordpress/editor](https://github.com/wordpress/editor) from 14.19.0 to 14.20.0 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#915
-   build(deps): bump indexmap from 2.7.1 to 2.8.0 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#921
-   build(deps): bump tokio from 1.43.0 to 1.44.1 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#919
-   build(deps-dev): bump [@&#8203;types/node](https://github.com/types/node) from 22.13.9 to 22.13.10 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#913
-   build(deps): bump foldhash from 0.1.4 to 0.1.5 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#917
-   feat: correct "along time" to "a long time" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#910
-   Add -able affix to open (openable) by [@&#8203;claydugo](https://github.com/claydugo) in Automattic/harper#930
-   docs: mention hidden library dependencies by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#943
-   feat(core): create new test assertion for `nth` suggestion results by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#942
-   build: migrate to pnpm workspace & biome by [@&#8203;Asuka109](https://github.com/Asuka109) in Automattic/harper#924
-   build(deps): bump serde from 1.0.218 to 1.0.219 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#920
-   build(deps): bump clap from 4.5.31 to 4.5.32 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#946
-   Web improvements by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#944
-   feat: ignore shebang lines by [@&#8203;holmanb](https://github.com/holmanb) in Automattic/harper#947
-   feat(web): add mask-image to header by [@&#8203;Asuka109](https://github.com/Asuka109) in Automattic/harper#951
-   fix(core): reduce ambiguity for `AvoidContraction` by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#941
-   chore: add comments describing major sections by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#952

#### New Contributors

-   [@&#8203;kit494way](https://github.com/kit494way) made their first contribution in Automattic/harper#893
-   [@&#8203;holmanb](https://github.com/holmanb) made their first contribution in Automattic/harper#947

**Full Changelog**: Automattic/harper@v0.25.1...v0.26.0

### [`v0.25.1`](https://github.com/Automattic/harper/releases/tag/v0.25.1)

[Compare Source](Automattic/harper@v0.25.0...v0.25.1)

#### What's Changed

-   docs(ls): give example config that disables `sentence_capitalization` by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#879
-   fix(core): indexing problem in Regexish work by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#883
-   Just getforms improvements by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#862
-   Dictionary curation 2025 03 11 by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#884
-   fix(core): insert paragraph breaks after code blocks by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#882

**Full Changelog**: Automattic/harper@v0.25.0...v0.25.1

### [`v0.25.0`](https://github.com/Automattic/harper/releases/tag/v0.25.0)

[Compare Source](Automattic/harper@v0.24.0...v0.25.0)

#### What's Changed

-   docs: update integrations section by [@&#8203;mcecode](https://github.com/mcecode) in Automattic/harper#755
-   Typst Corrections by [@&#8203;grantlemons](https://github.com/grantlemons) in Automattic/harper#442
-   refactor: add comments to `just addnoun` and tweak logic by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#605
-   feat: implements [#&#8203;841](Automattic/harper#841) by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#842
-   Add WordPress Plugin Documentation and Demo by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#838
-   feat: add `just newest-dict-changes` by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#701
-   Spellcheck improvements by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#844
-   fix: add missing "gotten rid off" to other "rid off" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#840
-   Rules page improvements by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#843
-   build(deps): bump axios from 1.8.1 to 1.8.2 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#845
-   Regexish by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#669
-   fix: fall back to `grep` when `rg` is not available by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#848
-   feat: flag "monumentous" and offer "momentous" and "monumental" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#864
-   build(deps-dev): bump svelte-check from 4.1.4 to 4.1.5 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#874
-   build(deps): bump typst-syntax from 0.13.0 to 0.13.1 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#867
-   build(deps-dev): bump typescript from 5.7.3 to 5.8.2 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#871
-   build(deps-dev): bump autoprefixer from 10.4.20 to 10.4.21 in /packages by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#873
-   Dictionary curation 2025 03 08 by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#860
-   feat: add many variants of "change of tact"->"tack" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#852
-   feat: implement [#&#8203;525](Automattic/harper#525) (worse/worst confusion) by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#856
-   build(deps): bump cached from 0.54.0 to 0.55.1 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#868
-   build(deps): bump anyhow from 1.0.96 to 1.0.97 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#865
-   Build against an older GLIBC version by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#877
-   Cache busting by [@&#8203;elijah-potter](https://github.com/elijah-potter) in Automattic/harper#876
-   build(deps): bump thiserror from 2.0.11 to 2.0.12 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#866
-   build(deps): bump serde_json from 1.0.139 to 1.0.140 by [@&#8203;dependabot](https://github.com/dependabot) in Automattic/harper#869
-   feat: add a lint to correct "in of itself" to "in and of itself" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#863
-   feat: implement "ticking time clock" by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#851
-   feat: implements [#&#8203;746](Automattic/harper#746) by [@&#8203;hippietrail](https://github.com/hippietrail) in Automattic/harper#855
-   feat(dict): added words to dictionary by [@&#8203;ficcdaf](https://github.com/ficcdaf) in Automattic/harper#847
-   fix: Ignore hex codes inside rgb function calls by [@&#8203;grantlemons](https://github.com/grantlemons) in Automattic/harper#857
-   feat: Added Linux musl compilations by [@&#8203;kiding](https://github.com/kiding) in Automattic/harper#878

#### New Contributors

-   [@&#8203;kiding](https://github.com/kiding) made their first contribution in Automattic/harper#878

**Full Changelog**: Automattic/harper@v0.24.0...v0.25.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xOTIuMCIsInVwZGF0ZWRJblZlciI6IjM5LjIxMC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants