|
| 1 | +# Compiling a Bundle |
| 2 | +This page contains information on compiling bundles, both for consumption by `js-slang` as well as other bundles. |
| 3 | + |
| 4 | +> [!WARNING] |
| 5 | +> The build tools automatically ignore files under a `__tests__` directory. Such files are considered |
| 6 | +> test files and will not be included in the compiled output. |
| 7 | +
|
| 8 | +## For `js-slang` |
| 9 | +In its raw ESM format, the bundle is unsuitable for use with `js-slang`. It is thus necessary to compile your written Typescript into the format needed by `js-slang`. |
| 10 | + |
| 11 | +Run the following command from within your bundle directory. |
| 12 | + |
| 13 | +```sh |
| 14 | +yarn build |
| 15 | +``` |
| 16 | + |
| 17 | +Note that running this command will **NOT** perform typechecking. If you wish to perform typechecking, use the following command: |
| 18 | +```sh |
| 19 | +yarn build --tsc |
| 20 | +``` |
| 21 | + |
| 22 | +This will run the TypeScript compiler before compiling your bundle. If there are any type errors, it will be displayed in the command line |
| 23 | +and your bundle will not be compiled. |
| 24 | + |
| 25 | +The output for your bundle will be placed at `/build/bundles/[your_bundle_name].js`. |
| 26 | + |
| 27 | +## For Other Bundles |
| 28 | +A Source Bundle may use another Source Bundle as a dependency. As an example, the `plotly` bundle relies on the `curve` bundle: |
| 29 | +```ts {2,3} |
| 30 | +// plotly/src/curve_functions.ts |
| 31 | +import { x_of, y_of, z_of, r_of, g_of, b_of } from '@sourceacademy/bundle-curve'; |
| 32 | +import type { Curve } from '@sourceacademy/bundle-curve/curves_webgl'; |
| 33 | +import Plotly, { type Data, type Layout } from 'plotly.js-dist'; |
| 34 | +import { CurvePlot } from './plotly'; |
| 35 | +``` |
| 36 | + |
| 37 | +If you intend for your bundle to be consumed from other bundles, do the following: |
| 38 | + |
| 39 | +### 1. Ensure that your `tsconfig.json` is properly configured |
| 40 | +```json |
| 41 | +{ |
| 42 | + "compilerOptions" { |
| 43 | + "outDir": "./dist", // Make sure outDir is specified |
| 44 | + "noEmit": false, // noEmit needs to be false |
| 45 | + "declaration": true // declaration needs to be true |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### 2. Ensure that your `package.json` points to the correct exports |
| 51 | +```json |
| 52 | +{ |
| 53 | + "name": "@sourceacademy/bundle-curve", |
| 54 | + "type": "module", |
| 55 | + "exports": { |
| 56 | + ".": "./dist/index.js", |
| 57 | + "./*": "./dist/*.js" |
| 58 | + }, |
| 59 | + "scripts": { |
| 60 | + "prepare": "yarn buildtools tsc" |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | +Refer to [this page](https://nodejs.org/api/packages.html#package-entry-points) for more information on how to configure your package exports. |
| 65 | + |
| 66 | +The `prepare` script is necessary as the bundle will need to be built using `tsc` during installation. |
| 67 | + |
| 68 | +### 3. Run `tsc` |
| 69 | +If necessary, run `yarn tsc` to produce Javascript and Typescript declaration files. |
0 commit comments