Skip to content

How to build lnagb.js

Duong (Zach) Nguyen edited this page Nov 15, 2022 · 2 revisions

This page will show you how you can build lnagb.js from its source code, and even how you can customize your builds.

Setting up

Before building the library, you will have to clone the repository and install the dependencies. Fire up the command line, and let's get started. 🔥

  1. Clone the repository.
git clone https://github.com/cszach/lnagb.js.git

Before cloning the repository, you would probably want to change the working directory to where you want to save the repository at.

  1. Move into the directory.
cd lnagb.js
  1. Install dependencies.
npm install

Building lnagb.js

Now that you've got the source code and the tools (dependencies) needed, it's time to build lnagb.js!

npm run build

This will build lnagb.js in 2 formats and place the files in the folder build. The files are:

  • lnagb.cjs: The library in the UMD format; and
  • lnagb.esm.js: The library in the ESM format.

Not sure what UMD and ESM are? See this wiki page. 👍

Building components of lnagb.js

lnagb.js has 3 main components: Equations, Matrices, and Vectors. Each component is composed of files defining quantities closely related to each other (e.g. Equations has linear equations, Matrices has matrices of all kinds, and so on). You can build these components separately with...

npm run build-components

This will build components separately and place the files in build/equations, build/matrices, and build/vectors. Again, each component is built into 2 formats, UMD and ESM, and you can read this wiki page to know how they differ.

Custom builds

⭐ You can customize your lnagb.js builds to include just what you need!

lnagb.js uses Rollup to build lnagb.js. Basically, it combines all the source files of lnagb.js into one single file. If you've run npm install, you have installed Rollup (locally), so there is no additional installation needed.

First, you have to create an entry JavaScript file to define what functions of lnagb.js you want to include in your builds. Typically this file is named index.js. If you look into the src folder, you will see an index.js file. This is the file from which the complete lnagb.js library (lnagb.cjs & lnagb.esm.js) is built. Looking further into src/equations, src/matrices, and src/vectors, you will see that each has an index.js file, which lists things to include in the final compiled library. Now, we are going to create a similar file.

Create a new JavaScript file, and name it whatever you want (in this example, myCustomBuild.js).

touch myCustomBuild.js

Next, in the JavaScript file, use the export statement to define what you want your build to export. This page on our website has a list of every module of lnagb.js as well as what each of them is. Be sure to export from src/index.js. Here is an example.

export {
    LinearEquation,
    SystemOfLinearEquations,
    Matrix,
    AugmentedMatrix,
    SquareMatrix,
    IdentityMatrix,
    Vector
} from './src/index.js';

Using export is very simple and straightforward, isn't it?

Now that we have the JavaScript file set, it's time to call Rollup into action! And here's how:

npx rollup -f umd -n lnagbjs -o build/myCustomBuild.js myCustomBuild.js
  • Replace "umd" with a format you prefer: esm for ESM, cjs for CommonJS, iife for IIFE, and amd for AMD. See Library formats to know how these formats differ.
  • If you choose a format that is not UMD, remove "-n lnagbjs". Otherwise, replace lnagbjs with an export namespace you prefer.
  • Replace "build/myCustomBuild.js" with the output file name you like (with leading directories if needed).
  • Replace myCustomBuild.js (the last argument) with the JavaScript file you have created above.

And there you have it! A custom build of lnagb.js! 😎

Clone this wiki locally