SWI-Prolog WebAssembly build as an NPM package. Please see this page for ongoing progress and information: <https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650>
npm install swipl-wasmSWI-Prolog WebAssembly build as an NPM package. Please see this page
for ongoing progress and information:
The easiest way to get started is by importing swipl-wasm into your npm project.
It can be imported for both Node.js and browser builds as follows:
script.mjs
``js
import SWIPL from "swipl-wasm";
async function main() {
const swipl = await SWIPL({ arguments: ["-q"] });
console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);
}
main();
`
For those who have not done this before, you will first need to install Node.js and npm.
After doing this, you can make a new project as follows:
`bash`Make the project directory
mkdir my-swipl-project && cd ./my-swipl-projectInitialise the project
npm initInstall swipl-wasm
npm i swipl-wasm
After this, place the above code in script.mjs in the root of your directory and run node script.mjs
to execute the script.
In browser:
`html`
The function locateFile will help the browser find the necessaryswipl-web.wasm
files ( and swipl-web.data). In this case, the filesswipl-web.js
should be served along with under the /dist/swipl
directory in the web server.
You can run this example by executing npm run test:serve-http and
visiting
In Node.js:
`js
const SWIPL = require("swipl-wasm");
const swipl = await SWIPL({ arguments: ["-q"] });
console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);
`
You can run this example with node examples/run-on-node.js.
The swipl-wasm package also comes with a single-file bundle. This does not.data
require distributing the and .wasm files, which are now embedded into.js
the file instead.
`html`
Often you will want to bundle a pre-built image of your Prolog file. The easiest way to do this is using the swipl-generate command to generate the image. For example, in ./examples/generation, the script npx swipl-generate ./max.pl ./dist/max.ts will generate a file ./dist/max.ts which contains the image of ./max.pl. This file can then be imported into your project and used as follows:
`ts
import SWIPL from './max';
async function main() {
const Module = await SWIPL();
const res = Module.prolog.query('max(1, 2, 3).');
console.log(res.next())
}
main();
`
Note that this procedure generates a file which imports directly from swipl-wasm/dist/loadImageDefault, so make sure that swipl-wasm is a direct dependency in your project rather than a dev dependency.
To generate the image data without it being pre-loaded into SWIPL, use the --image-only flag.
This uses eval:
`jsconsole.log('hello')
swipl.prolog
.query("js_run_script(Script)", {
Script: ,`
})
.once();
Webpack is a JavaScript and resources
bundler for large-scale frontend projects.
There is an example Webpack project in examples/webpack. It uses.data
Asset Modules to "load"
necessary and .wasm files. The location of these files is thenlocateFile
fed to (see above).
The package swipl-wasm is linked into the example. In an actual project,swipl-wasm
you would declare as a normal dependency.
To start the example:
``
cd examples/webpack
npm install
npm run build
npm run server
Then visit
Prolog".
For convenience, we provide deployed bundled versions of SWI-Prolog on GitHub Pages which can be directly used in an HTML document.
There is a bundled version for each release, which can be found at the URL:
``
https://SWI-Prolog.github.io/npm-swipl-wasm/vMajor/vMinor/vPatch/index.js
For instance, v3.3.0 has the URL https://SWI-Prolog.github.io/npm-swipl-wasm/3/3/0/index.js. We also have shortcuts for:
- the latest version https://SWI-Prolog.github.io/npm-swipl-wasm/latest/index.js,
- the latest of each major version https://SWI-Prolog.github.io/npm-swipl-wasm/vMajor/latest/index.js, and
- the latest of each minor version https://SWI-Prolog.github.io/npm-swipl-wasm/vMajor/vMinor/latest/index.js
Available versions can be browsed at https://github.com/SWI-Prolog/npm-swipl-wasm/tree/pages.
With this approach, the following script will work:
`html`
Dynamic imports are also available with the dynamic-import.js import name and can be used as follows:
`html`
The package can be built using npm. Please use npm to add new dependencies
and update the package-lock.json file. The SWI-Prolog WebAssembly version is
currently built inside Docker with Emscripten.
To develop with this package, clone the repository and run:
``Install dependencies
npm ciBuild the WebAssembly
npm run buildRun tests
npm t
Note: You need Docker and Node 16 or higher to be installed to build the package.
The package uses its own versioning scheme using semver. It is
detached from the versioning of SWI-Prolog itself.
To get the underlying SWI-Prolog version:
`js`
const swipl = await SWIPL({ arguments: ["-q"] });
const version = swipl.prolog
.query("current_prolog_flag(version, Version)")
.once().Version;
The version is returned as integer 10000 × Major + 100 × Minor + Patch`.
This package uses the same license as SWI-Prolog (BSD-2-Clause):