SWI-Prolog WebAssembly build as a NPM package

Overview

swipl-wasm

SWI-Prolog WebAssembly build as a 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

Usage

In browser:

<div id="solution"></div>
<script src="/dist/swipl/swipl-web.js"></script>
<script>
  (async () => {
    const swipl = await SWIPL({
      noInitialRun: true,
      arguments: ["-q"],
      locateFile: (url) => {
        if (url === "swipl-web.data") {
          return "/dist/swipl/swipl-web.data";
        } else if (url === "swipl-web.wasm") {
          return "/dist/swipl/swipl-web.wasm";
        }
        return url;
      },
    });
    const query = "member(X, [a, b, c]).";
    const solutionElement = document.getElementById("solution");
    // See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
    const firstSolution = swipl.prolog.query(query).once().X;
    solutionElement.textContent = firstSolution;
  })();
</script>

You can run this example by executing npm run test:serve-http and visiting http://localhost:8080/examples/browser.html.

In Nodejs:

// This helps to find the correct locations of the
// .data and .wasm files.
const swiplModuleLocation = require.resolve("swipl-wasm/swipl");
const swipl = await SWIPL({
  noInitialRun: true,
  arguments: ["-q"],
  locateFile: (url) => {
    // These are common with the web version.
    if (url === "swipl-web.data") {
      return path.join(path.dirname(swiplModuleLocation), "swipl-web.data");
    } else if (url === "swipl-web.wasm") {
      return path.join(path.dirname(swiplModuleLocation), "swipl-web.wasm");
    }
    return url;
  },
});
// See https://swi-prolog.discourse.group/t/swi-prolog-in-the-browser-using-wasm/5650/1
console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);

You can run this example with node examples/run-on-node.js.

Build

The package can be built using npm or yarn. Please use yarn to add new dependencies and update yarn.lock file. SWI-Prolog WebAssembly version is currently built inside Docker with Emscripten.

TODO

  • More examples (how to bundle with webpack)
  • Integrate with SWI-Prolog CI
  • TypeScript types for Prolog.js (and the Query interface)
  • Use EcmaScript modules as output: emscripten-core/emscripten#11792

License

Same as SWI-Prolog license, BSD simplified: https://github.com/SWI-Prolog/swipl-devel/blob/master/LICENSE

Comments
  • fix: make `@types/emscripten` a peer dependency

    fix: make `@types/emscripten` a peer dependency

    As explained here @types/ packages should be a peerDependency if the type is part of what the consumer of the package should see. Otherwise the type is unresolved for downstream developers.

    opened by jeswr 0
  • Sync sys

    Sync sys

    Some changes syncing with the current version. Also renamed the build directory from swipl.wasm to build.wasm as I was getting a bit confused by all the various swipl.wasm names :smile:

    opened by JanWielemaker 0
  • prolog.js re-assigns a const

    prolog.js re-assigns a const

    This is to track https://github.com/SWI-Prolog/swipl-devel/issues/1083 and request that a patch release of this library be made once that upstream issue is closed.

    opened by jeswr 0
  • Add bundled distribution of SWIPL

    Add bundled distribution of SWIPL

    Following on from the recent discussion in https://github.com/rla/npm-swipl-wasm/issues/1; this modifies the build to create a swipl-bundle.js file that contains everything needed to run swipl in browser and node. I have also made this the main entry point as there currently is none.


    I've calculated that the size of swipl-bundle.js is 33.5% larger than the combination of swipl-web.wasm, swipl-web.data and swipl-web.js. And it seems to run pretty fast once loaded. I personally think this is acceptable for the type of POC use-cases that I am working on.

    2M swipl-web.wasm 6M swipl-web.data 1M swipl-web.js =9M

    11M swipl-bundle.js

    opened by jeswr 4
  • feat: minimal `swipl-web`

    feat: minimal `swipl-web`

    This might be a more appropriate issue to open in https://github.com/SWI-Prolog/swipl-devel/ down the line; but I think it is fine to keep it here for now as I don't expect this will be a priority for anyone anytime soon.

    It would be nice to have a much smaller build of swipl-web that does not make use of the path, fs and crypto modules; and also does not do any fetch handling internally.

    This means that use cases with apps that just want to run basic queries don't need to import unecessary code into their apps.

    I'm analyse tree-shaking downstream can offer for this particular file; which could well make this a moot point to solve anyway.

    opened by jeswr 3
  • feat: Isomorphic node/browser usage

    feat: Isomorphic node/browser usage

    Thanks again for creating this package!

    I would like isomorphic usage across browser/node. As as far as I see it, this entails bunding the locateFile function in the package and exporting a different version based on whether the environment is browser or node. I will create a PR for this shortly.

    opened by jeswr 24
Owner
Raivo Laanemets
Front-end developer
Raivo Laanemets
npm i uuid, npm i nodemon, npm i commander

goit-nodejs-hw-01 Получаем и выводим весь список контактов в виде таблицы (console.table) node index.js --action list Получаем контакт по id node inde

Oksana Banshchykova 3 Jul 5, 2022
An ergonomic, mobile-first, Prolog playground!

Paízo Programmer Pronounced "Pay-zo Programmer". https://lf94.github.io/paizo-programmer/ Paizo is a playground for programmers. It's intended to be u

Lee 30 Aug 13, 2022
An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

Snyk Labs 57 Dec 28, 2022
WebVM is a server-less virtual Linux environment running fully client-side in HTML5/WebAssembly.

WebVM This repository hosts the source code of the https://webvm.io live demo page. WebVM is a server-less virtual Linux environment running fully cli

Leaning Technologies Ltd 1.7k Jan 8, 2023
A planetary body simulator with a HTML5/CSS3/JS frontend and simulated using Rust WebAssembly.

Solar Sim ?? A planetary body simulator with a HTML5/CSS3/JS frontend and simulated using Rust WebAssembly. Try the latest version here! Built with ??

Ben Plate 3 Jun 15, 2022
Solid, AssemblyScriptified and compiled to WebAssembly

solid-as Solid as _____! Solid, AssemblyScriptified and compiled to WebAssembly (very work in progress). Installation TODO Test npm i && npm test TODO

Joe Pea 4 Jul 19, 2022
A JavaScript / WebAssembly library for generating regular expressions from user-provided test cases

1. What does this library do? grex is a library that is meant to simplify the often complicated and tedious task of creating regular expressions. It d

Peter M. Stahl 164 Dec 30, 2022
Super Mario 64 ported to JavaScript and WebAssembly via Emscripten.

SM64-JS Super Mario 64 ported to JavaScript and WebAssembly via Emscripten. REPL Takedown I was forced to take down the REPL for this project due to c

Sevenworks 12 Dec 23, 2022
NPM Package that simplifies Auth with Google OAuth2 🔐

Node Google OAuth2 ?? A simple authentication flow for Google OAuth2 Explore the docs » Report Bug Table of Contents About The Project Getting Started

Adarsh C 8 Jun 17, 2022
portfolio-project is a npm package to automatically update your projects section in your portfolio website. It will fetch the selected repositories directly from your GitHub account.

portfolio-project Those days of manually updating portfolio website after every new project made are gone ⚡ Yesss . . . you read that right. ?? portfo

Gaurav Gulati 15 Aug 3, 2021
A NPM package powered by Yeoman that generates a scaffolding boilerplate for back-end workflow with Node.js.

generator-noderplate Generate Node.js starter files with just one command! We have deployed a npm package that will generate a boilerplate for nodejs

Samarjeet 3 Jan 24, 2022
Detect npm packages by author name in your package-lock.json or yarn.lock.

detect-package-by-author Detect npm packages by author name in your package-lock.json or yarn.lock. Install Install with npm: # Not Yet Publish # npm

azu 2 Jan 11, 2022
A Compiler npm Package.

vcompiler ?? Version 1.x is live ! ?? Introducation It is the npm package for the compilation of the code. Currently it supports the following program

Ankit Choudhary अंकित चौधरी 2 May 30, 2022
Source of the cosmwasm npm package

About Cosmwasm.js was created to help new developers get started with their first dApps. It is just a wrapper package to easily import needed features

CosmWasm 36 Dec 28, 2022
Npm package :p

?? poopoo-api Welcome to an "ok" api ⭐ If you like this package, don't forget to star Github package repo here and the website repo here Example code:

frostzzone 2 Mar 18, 2022
A cool npm package.

aditya.utils Colorful Console const a = require('aditya.utils') a.logblue("TEXT") // Blue Text a.logred("TEXT") // Red Text a.loggreen("TEXT") // Gree

AD!TYA 2 Apr 8, 2022
A cool npm package.

aditya.utils Colorful Console const a = require('aditya.utils') a.logblue("TEXT") // Blue Text a.logred("TEXT") // Red Text a.loggreen("TEXT") // Gree

AD!TYA 2 Apr 8, 2022
🛫 TypeScript Starter template to simplify creating your next npm package.

TypeScript Starter Kit This is an opinionated TypeScript Starter kit to help kick-start development of your next npm package. ?? Get Started Luckily,

Open Web 7 Sep 27, 2022
A CLI tool to make Taobao's npm mirror sync your package immediately.

npm-mirror-sync A CLI tool to make Taobao's npm mirror sync your package immediately. 让淘宝的 NPM 镜像立即收录你的包的新版本。 背景 相信国内小伙伴都在用淘宝的 NPM 镜像(npmmirror.com)作为

CSS魔法 10 Jun 9, 2022