-
Notifications
You must be signed in to change notification settings - Fork 2
Add Module Loader #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AaravMalani
wants to merge
5
commits into
main
Choose a base branch
from
feat/add-module-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a86fded
feat: add module loader packages
AaravMalani 4ee5861
fix: if the module dir URL is changed to the same URL, then only skip…
AaravMalani c9f39c0
chore: add documentation
AaravMalani a55a905
chore: add tests
AaravMalani 9179da3
fix: fix Gemini points
AaravMalani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <h1 align="center"> Module Loader </h1> | ||
|
|
||
| <p align="center">A protocol to load Source Academy modules</p> | ||
|
|
||
| <p align="center"> | ||
| <a href="https://www.npmjs.com/package/@sourceacademy/common-module-loader"><img src="https://img.shields.io/npm/v/@sourceacademy/common-module-loader?color=1a2530&label=npm"></a> | ||
| <a href="https://github.com/source-academy/plugins/tree/main/src/common/module-loader"><img src="https://img.shields.io/badge/github-repo-blue?logo=github"></a> | ||
| </p> | ||
|
|
||
| ## Features | ||
| - Protocol for loading Source Academy modules on the runner side, from a changeable module directory on the host side | ||
|
|
||
| ## Installation | ||
| ```bash | ||
| yarn add @sourceacademy/common-module-loader | ||
| # OR | ||
| npm i @sourceacademy/common-module-loader | ||
| # OR | ||
| pnpm add @sourceacademy/common-module-loader | ||
| ``` | ||
|
|
||
| ## Structure | ||
| This package ([`@sourceacademy/common-module-loader`](https://github.com/source-academy/plugins/tree/main/src/common/module-loader)) contains the shared constants and types required by both the runner-side and web-side plugin implementations. | ||
|
|
||
| These include: | ||
| - The IDs and channel name the two plugins use to find each other (`RUNNER_ID`, `WEB_ID`, `CHANNEL_ID`) | ||
| - The message type (`ModuleLoaderMessage`) sent between the two plugins | ||
|
|
||
| ## Usage | ||
| Ideally, you should not need to use this package directly. Instead, use the runner-side plugin ([`@sourceacademy/runner-module-loader`](https://github.com/source-academy/plugins/tree/main/src/runner/module-loader)) or the web-side plugin ([`@sourceacademy/web-module-loader`](https://github.com/source-academy/plugins/tree/main/src/web/module-loader)). | ||
|
|
||
| However, if you do need to use this package directly, you can import the constants and types as follows: | ||
|
|
||
| ```ts | ||
| import type { ModuleLoaderMessage } from '@sourceacademy/common-module-loader'; | ||
| ... | ||
| ``` | ||
|
|
||
| ## Further reading | ||
| - To load a module, use [`@sourceacademy/runner-module-loader`](https://github.com/source-academy/plugins/tree/main/src/runner/module-loader) | ||
| - To provide the module directory URL, use [`@sourceacademy/web-module-loader`](https://github.com/source-academy/plugins/tree/main/src/web/module-loader) | ||
| - The [plugins wiki](https://github.com/source-academy/plugins/wiki) covers how Conductor plugins communicate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "type": "installable" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "@sourceacademy/common-module-loader", | ||
| "version": "0.0.1", | ||
| "packageManager": "yarn@4.6.0", | ||
| "description": "The common types for loading modules in Source Academy", | ||
| "scripts": { | ||
| "build": "rollup -c", | ||
| "prepack": "yarn build" | ||
| }, | ||
| "license": "ISC", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "main": "dist/index.cjs", | ||
| "module": "dist/index.mjs", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.cjs", | ||
| "types": "./dist/index.d.ts" | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
| "@rollup/plugin-node-resolve": "^16.0.3", | ||
| "@rollup/plugin-terser": "^1.0.0", | ||
| "@rollup/plugin-typescript": "^12.3.0", | ||
| "rollup": "^4.60.2", | ||
| "tslib": "^2.8.1", | ||
| "typescript": "^6.0.3", | ||
| "vitest": "^4.1.9" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import nodeResolve from "@rollup/plugin-node-resolve"; | ||
| import terser from "@rollup/plugin-terser"; | ||
| import typescript from "@rollup/plugin-typescript"; | ||
|
|
||
| /** | ||
| * @type {import('rollup').RollupOptions} | ||
| */ | ||
| export default { | ||
| input: "src/index.ts", | ||
| output: [ | ||
| { | ||
| file: "dist/index.cjs", | ||
| format: "cjs", | ||
| }, | ||
| { | ||
| file: "dist/index.mjs", | ||
| format: "esm", | ||
| }, | ||
| ], | ||
| plugins: [nodeResolve(), typescript(), terser()], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export const WEB_ID = "__web_module_loader"; | ||
| export const RUNNER_ID = "__runner_module_loader"; | ||
|
|
||
| export const CHANNEL_ID = "module_config"; | ||
|
|
||
| export enum ModuleLoaderMessageType { | ||
| REQUEST_MODULE = "request_module", | ||
| MODULE_RESPONSE = "module_response", | ||
| MODULE_ERROR = "module_error", | ||
| } | ||
|
|
||
| export type ModuleLoaderMessage = | ||
| | { | ||
| type: ModuleLoaderMessageType.REQUEST_MODULE; | ||
| moduleName: string; | ||
| } | ||
| | { | ||
| type: ModuleLoaderMessageType.MODULE_RESPONSE; | ||
| moduleName: string; | ||
| moduleURL: string; | ||
| tabs: string[]; | ||
| } | ||
| | { | ||
| type: ModuleLoaderMessageType.MODULE_ERROR; | ||
| moduleName: string; | ||
| error: string; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "exclude": ["./dist"], | ||
| "include": ["./src"], | ||
| "compilerOptions": { | ||
| "declaration": true, | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <h1 align="center">Module Loader</h1> | ||
|
|
||
| <p align="center">Runner-side plugin for loading Source Academy modules</p> | ||
|
|
||
| <p align="center"> | ||
| <a href="https://www.npmjs.com/package/@sourceacademy/runner-module-loader"><img src="https://img.shields.io/npm/v/@sourceacademy/runner-module-loader?color=1a2530&label=npm"></a> | ||
| <a href="https://github.com/source-academy/plugins/tree/main/src/runner/module-loader"><img src="https://img.shields.io/badge/github-repo-blue?logo=github"></a> | ||
| </p> | ||
|
|
||
| ## Features | ||
| The plugin provides an interface to load modules with a module ID | ||
|
|
||
| ## Installation | ||
| ```bash | ||
| yarn add @sourceacademy/runner-module-loader | ||
| # OR | ||
| npm i @sourceacademy/runner-module-loader | ||
| # OR | ||
| pnpm add @sourceacademy/runner-module-loader | ||
| ``` | ||
|
|
||
| ## Structure | ||
| This package ([`@sourceacademy/runner-module-loader`](https://github.com/source-academy/plugins/tree/main/src/runner/module-loader)) contains the `ModuleLoaderRunnerPlugin` class — a Conductor runner plugin that an evaluator can call to load modules. | ||
|
|
||
| ### API Reference | ||
| | Name | Description | | ||
| |------|-------------| | ||
| | `instance` | The singleton instance of the plugin. | | ||
| | `async requestModule(moduleName: string): Promise<IModulePlugin>` | Request a module by name from the host plugin. Rejects with an error if the module is not found. | | ||
|
|
||
| ## Usage | ||
| After installation, import `ModuleLoaderRunnerPlugin` and register it with the Conductor evaluator. When evaluating `import` statements, call `requestModule` to load the module. | ||
|
|
||
| ```ts | ||
| ... | ||
| const pluginObj = await ModuleLoaderRunnerPlugin.instance.requestModule(moduleName); | ||
| context.nativeStorage.loadedModules[moduleName] = Object.fromEntries( | ||
| pluginObj?.exports.map(t => [t.symbol, t]) || [], | ||
| ); | ||
| ... | ||
| ``` | ||
|
|
||
| ## Further reading | ||
| - For the shared protocol types and IDs, see [`@sourceacademy/common-module-loader`](https://github.com/source-academy/plugins/tree/main/src/common/module-loader) | ||
| - For the host-side plugin that accepts the module directory URL, see [`@sourceacademy/web-module-loader`](https://github.com/source-academy/plugins/tree/main/src/web/module-loader) | ||
| - The [plugins wiki](https://github.com/source-academy/plugins/wiki) covers how Conductor plugins communicate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "type": "installable" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "name": "@sourceacademy/runner-module-loader", | ||
| "version": "0.0.1", | ||
| "packageManager": "yarn@4.12.0", | ||
| "description": "The runner plugin for the module loader in Source Academy", | ||
| "scripts": { | ||
| "build": "rollup -c", | ||
| "prepack": "yarn build" | ||
| }, | ||
| "license": "ISC", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "main": "dist/index.cjs", | ||
| "module": "dist/index.mjs", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.cjs", | ||
| "types": "./dist/index.d.ts" | ||
| } | ||
| }, | ||
| "peerDependencies": { | ||
| "@sourceacademy/conductor": ">=0.3.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@rollup/plugin-node-resolve": "^16.0.3", | ||
| "@rollup/plugin-terser": "^1.0.0", | ||
| "@rollup/plugin-typescript": "^12.3.0", | ||
| "@sourceacademy/common-module-loader": "workspace:*", | ||
| "@sourceacademy/conductor": ">=0.3.0", | ||
| "@sourceacademy/runner-remote-execution": "workspace:*", | ||
| "@vitest/coverage-istanbul": "^4.1.9", | ||
| "rollup": "^4.60.2", | ||
| "tslib": "^2.8.1", | ||
| "typescript": "^6.0.3", | ||
| "vitest": "^4.1.9" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import nodeResolve from "@rollup/plugin-node-resolve"; | ||
| import terser from "@rollup/plugin-terser"; | ||
| import typescript from "@rollup/plugin-typescript"; | ||
|
|
||
| /** @type {import('rollup').RollupOptions} */ | ||
| export default { | ||
| input: "src/index.ts", | ||
| output: [ | ||
| { | ||
| file: "dist/index.cjs", | ||
| format: "cjs", | ||
| }, | ||
| { | ||
| file: "dist/index.mjs", | ||
| format: "esm", | ||
| }, | ||
| ], | ||
| plugins: [nodeResolve(), typescript(), terser()], | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.