Skip to content

[M] Mount decimal.js in the sandbox and add a Decimal global - #2

Merged
evanrichards merged 1 commit into
mainfrom
decimal-by-default
Sep 12, 2026
Merged

evanrichards merged 1 commit into
mainfrom
decimal-by-default

Conversation

@evanrichards

Copy link
Copy Markdown
Collaborator

The host code works with decimal strings. Every embedder had to read the
decimal.js source from disk and mount it through the option nodeModules.
A file read does not work in a bundle or in a vm without a file system.

This change vendors the decimal.js source into the package as a string
constant. The virtual file system mounts it always, so the guest code runs
import Decimal from 'decimal.js' without an option. The new option
enableDecimalGlobal registers the class Decimal as a global, which
removes the need for an import statement in the guest code.

The package also exports the source as decimalJsSource. An embedder that
drives quickjs-emscripten-core directly gives the same class to the guest,
and needs no bundler rule to turn the file into text.

The script vendor.ts generates src/modules/generated/decimal.js from the
dev dependency decimal.js.

@evanrichards

Copy link
Copy Markdown
Collaborator Author

This change is part of the following stack:

Change managed by git-spice.

The host code works with decimal strings. Every embedder had to read the
decimal.js source from disk and mount it through the option `nodeModules`.
A file read does not work in a bundle or in a vm without a file system.

This change vendors the decimal.js source into the package as a string
constant. The virtual file system mounts it always, so the guest code runs
`import Decimal from 'decimal.js'` without an option. The new option
`enableDecimalGlobal` registers the class `Decimal` as a global, which
removes the need for an import statement in the guest code.

The package exports the source through the subpath `./decimal-source`. An
embedder that drives `quickjs-emscripten-core` directly gives the same class
to the guest, and needs no bundler rule to turn the file into text. The
subpath holds one string constant and imports nothing else, so a bundler
does not pull the sandbox and the module memfs into the output.

The script `vendor.ts` generates `src/modules/generated/decimal.js` from the
dev dependency decimal.js.
@evanrichards
evanrichards merged commit 4a90e8a into main Sep 12, 2026
1 check passed
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.

1 participant