├── .prettierignore ├── .eslintignore ├── .gitattributes ├── prod.Dockerfile ├── docker-compose.yml ├── plugins └── PluginTemplate.js ├── .github └── workflows │ ├── dev.yml │ ├── format.yml │ └── release.yml ├── LICENSE ├── package.json ├── cli.js ├── .gitignore ├── CHANGELOG.md ├── README.md └── server.js /.prettierignore: -------------------------------------------------------------------------------- 1 | CHANGELOG.md 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | plugins/PluginTemplate.js 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Autodetect text files 2 | * text=auto 3 | 4 | # Ensure cli.js always uses LF 5 | cli.js text eol=lf 6 | -------------------------------------------------------------------------------- /prod.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:14 as base 2 | 3 | WORKDIR /app 4 | 5 | RUN apt-get update 6 | RUN apt-get -y install apt-transport-https ca-certificates 7 | 8 | COPY ./package.json . 9 | RUN npm i 10 | COPY ./cli.js . 11 | COPY ./server.js . -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.9" 2 | services: 3 | plugin_server: 4 | ports: 5 | - ${WEB_LISTEN_PORT_BACKEND:-2222}:${WEB_OUTPUT_PORT_BACKEND:-2222} 6 | container_name: plugin_server 7 | build: 8 | context: . 9 | target: base 10 | dockerfile: prod.Dockerfile 11 | command: node cli.js 12 | restart: unless-stopped 13 | volumes: 14 | - ./plugins:/app/plugins 15 | networks: 16 | - plugin_network 17 | 18 | networks: 19 | plugin_network: 20 | -------------------------------------------------------------------------------- /plugins/PluginTemplate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Remember, you have access these globals: 3 | * 1. df - Just like the df object in your console. 4 | * 2. ui - For interacting with the game's user interface. 5 | * 6 | * Let's log these to the console when you run your plugin! 7 | */ 8 | console.log(df, ui); 9 | 10 | class PluginTemplate { 11 | constructor() {} 12 | 13 | /** 14 | * Called when plugin is launched with the "run" button. 15 | */ 16 | async render(container) {} 17 | 18 | /** 19 | * Called when plugin modal is closed. 20 | */ 21 | destroy() {} 22 | } 23 | 24 | /** 25 | * And don't forget to export it! 26 | */ 27 | export default PluginTemplate; 28 | -------------------------------------------------------------------------------- /.github/workflows/dev.yml: -------------------------------------------------------------------------------- 1 | name: Development Workflow 2 | 3 | on: 4 | push: 5 | branches: [master, main] 6 | pull_request: 7 | 8 | jobs: 9 | test: 10 | timeout-minutes: 20 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | os: [ubuntu-latest, macos-latest, windows-latest] 15 | node-version: [14.x, 16.x, 18.x] 16 | 17 | runs-on: ${{ matrix.os }} 18 | 19 | steps: 20 | - name: Checkout project 21 | uses: actions/checkout@v2 22 | 23 | - name: Setup Node/Yarn 24 | uses: actions/setup-node@v2 25 | with: 26 | node-version: ${{ matrix.node-version }} 27 | 28 | - name: Install dependencies 29 | run: npm i 30 | 31 | - name: Run linters 32 | run: npm run lint 33 | 34 | - name: Run tests 35 | run: npm test 36 | -------------------------------------------------------------------------------- /.github/workflows/format.yml: -------------------------------------------------------------------------------- 1 | name: Format 2 | 3 | on: [push] 4 | 5 | jobs: 6 | format: 7 | timeout-minutes: 20 8 | strategy: 9 | fail-fast: false 10 | matrix: 11 | os: [ubuntu-latest] 12 | node-version: [16.x] 13 | 14 | runs-on: ${{ matrix.os }} 15 | 16 | steps: 17 | - name: Checkout project 18 | uses: actions/checkout@v2 19 | with: 20 | ref: ${{ github.head_ref }} 21 | 22 | - name: Setup Node/Yarn 23 | uses: actions/setup-node@v2 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | 27 | - name: Install dependencies 28 | run: npm i 29 | 30 | - name: Format project 31 | run: npm run format 32 | 33 | - name: Commit format updates 34 | uses: phated/git-auto-commit-action@v4 35 | with: 36 | commit_message: "chore: Fix formatting" 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Blaine Bublitz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: [master, main] 6 | 7 | jobs: 8 | release-please: 9 | name: Create Release 10 | timeout-minutes: 20 11 | runs-on: ubuntu-latest 12 | outputs: 13 | release_created: ${{ steps.release.outputs.release_created }} 14 | steps: 15 | - uses: GoogleCloudPlatform/release-please-action@v2 16 | id: release 17 | with: 18 | token: ${{ secrets.GITHUB_TOKEN }} 19 | release-type: node 20 | 21 | npm-release: 22 | needs: [release-please] 23 | if: ${{ needs.release-please.outputs.release_created }} 24 | name: Publish to npm registry 25 | timeout-minutes: 20 26 | runs-on: ubuntu-latest 27 | steps: 28 | - name: Checkout project 29 | uses: actions/checkout@v2 30 | 31 | - name: Setup NodeJS 32 | uses: actions/setup-node@v2 33 | with: 34 | node-version: "16" 35 | registry-url: "https://registry.npmjs.org" 36 | 37 | - name: Prepare for publish 38 | run: npm i 39 | 40 | - name: Publish to npm 41 | run: npm publish 42 | env: 43 | NODE_AUTH_TOKEN: ${{ secrets.NPM_SOPHONBOT }} 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@projectsophon/df-plugin-dev-server", 3 | "version": "1.5.0", 4 | "description": "An ESBuild server for Dark Forest plugin development.", 5 | "author": "Blaine Bublitz (https://github.com/phated)", 6 | "repository": "projectsophon/df-plugin-dev-server", 7 | "license": "MIT", 8 | "engines": { 9 | "node": ">=14" 10 | }, 11 | "type": "module", 12 | "main": "server.js", 13 | "bin": { 14 | "df-plugin-dev-server": "cli.js" 15 | }, 16 | "files": [ 17 | "cli.js", 18 | "server.js" 19 | ], 20 | "scripts": { 21 | "lint": "eslint .", 22 | "test": "exit 0", 23 | "format": "prettier --write ." 24 | }, 25 | "dependencies": { 26 | "chalk": "^4.1.1", 27 | "esbuild": "^0.12.1", 28 | "fast-glob": "^3.2.6", 29 | "get-port": "^5.1.1", 30 | "yargs": "^17.0.1" 31 | }, 32 | "devDependencies": { 33 | "@projectsophon/eslint-config": "0.0.2", 34 | "@projectsophon/prettier-config": "0.0.0", 35 | "eslint": "^7.27.0", 36 | "prettier": "^2.3.0", 37 | "typescript": "^4.2.4" 38 | }, 39 | "prettier": "@projectsophon/prettier-config", 40 | "eslintConfig": { 41 | "extends": "@projectsophon/eslint-config" 42 | }, 43 | "keywords": [] 44 | } 45 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import { default as yargs } from "yargs"; 4 | 5 | import { start, bundle } from "./server.js"; 6 | 7 | const parser = yargs() 8 | .scriptName("df-plugin-dev-server") 9 | .command( 10 | "$0", 11 | "Start a Dark Forest plugin development server.", 12 | (yargs) => { 13 | return yargs.options({ 14 | dir: { 15 | desc: "The directory to load", 16 | type: "string", 17 | deprecated: "use --glob instead", 18 | }, 19 | ext: { 20 | desc: "Extensions to process", 21 | type: "array", 22 | deprecated: "use --glob instead", 23 | }, 24 | glob: { 25 | desc: "Glob for finding plugins", 26 | type: "array", 27 | default: ["(plugins|content)/**/*.(js|jsx|ts|tsx)"], 28 | }, 29 | preact: { 30 | desc: "Enabled custom preact support", 31 | type: "boolean", 32 | default: false, 33 | }, 34 | }); 35 | }, 36 | start 37 | ) 38 | .command( 39 | "bundle", 40 | "Produce bundles for Dark Forest plugins.", 41 | (yargs) => { 42 | return yargs.options({ 43 | glob: { 44 | desc: "Glob for finding plugins", 45 | type: "array", 46 | default: ["plugins/*.(js|jsx|ts|tsx)"], 47 | }, 48 | preact: { 49 | desc: "Enabled custom preact support", 50 | type: "boolean", 51 | default: false, 52 | }, 53 | outdir: { 54 | desc: "The directory to output files", 55 | type: "string", 56 | required: true, 57 | }, 58 | }); 59 | }, 60 | bundle 61 | ); 62 | 63 | parser.parse(process.argv.slice(2)); 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | # npm packages after install 107 | node_modules 108 | package-lock.json -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [1.5.0](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.4.0...v1.5.0) (2022-06-15) 4 | 5 | 6 | ### Features 7 | 8 | * Update default glob so CLI also works in plugin repo ([#15](https://www.github.com/projectsophon/df-plugin-dev-server/issues/15)) ([fabc225](https://www.github.com/projectsophon/df-plugin-dev-server/commit/fabc225308c254d9ef19939a2953c36700a5d7a4)) 9 | 10 | ## [1.4.0](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.3.2...v1.4.0) (2021-08-14) 11 | 12 | 13 | ### Features 14 | 15 | * Implement bundle command ([#16](https://www.github.com/projectsophon/df-plugin-dev-server/issues/16)) ([4c83232](https://www.github.com/projectsophon/df-plugin-dev-server/commit/4c832324e988d424359423bc4d73ab2d09ac006f)) 16 | 17 | ### [1.3.2](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.3.1...v1.3.2) (2021-07-04) 18 | 19 | 20 | ### Bug Fixes 21 | 22 | * Use require.resolve to find preact modules ([#13](https://www.github.com/projectsophon/df-plugin-dev-server/issues/13)) ([7b9d670](https://www.github.com/projectsophon/df-plugin-dev-server/commit/7b9d670a81cb538bdfb5446d726032a782d4d71d)) 23 | 24 | ### [1.3.1](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.3.0...v1.3.1) (2021-07-03) 25 | 26 | 27 | ### Bug Fixes 28 | 29 | * Correct typo `const` -> `let` ([#11](https://www.github.com/projectsophon/df-plugin-dev-server/issues/11)) ([75b541d](https://www.github.com/projectsophon/df-plugin-dev-server/commit/75b541d47c2c5a7c92406f0feadc3646d22dc4b8)) 30 | 31 | ## [1.3.0](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.2.0...v1.3.0) (2021-06-30) 32 | 33 | 34 | ### Features 35 | 36 | * Accept `.jsx` and `.tsx` extensions ([976b72b](https://www.github.com/projectsophon/df-plugin-dev-server/commit/976b72b1e5c0aac3a9777b04601e1f31c6de1e5b)) 37 | * Add custom Preact JSX support behind flag ([9797450](https://www.github.com/projectsophon/df-plugin-dev-server/commit/97974509bee29dc12dd23f04147e6b0c37eadac8)) 38 | * Add support for http/https imports ([8bc5e04](https://www.github.com/projectsophon/df-plugin-dev-server/commit/8bc5e046b72f1e2d06cde59c309dc7ff029edc37)) 39 | 40 | ## [1.2.0](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.1.0...v1.2.0) (2021-06-28) 41 | 42 | 43 | ### Features 44 | 45 | * Add docker image and docker-compose setup ([#5](https://www.github.com/projectsophon/df-plugin-dev-server/issues/5)) ([b9fd252](https://www.github.com/projectsophon/df-plugin-dev-server/commit/b9fd2523c38ca5036bf03b05b8369291fe1129a1)) 46 | * Support globbing for plugins ([#7](https://www.github.com/projectsophon/df-plugin-dev-server/issues/7)) ([9112c6f](https://www.github.com/projectsophon/df-plugin-dev-server/commit/9112c6f8aa30aa6ed9bf9b2ac6f853517e276e80)) 47 | 48 | ## [1.1.0](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.0.2...v1.1.0) (2021-05-29) 49 | 50 | 51 | ### Features 52 | 53 | * Split CLI from server for programatic API ([dcc9c0b](https://www.github.com/projectsophon/df-plugin-dev-server/commit/dcc9c0b3c6f604d5a0e2f0429562e26ce0321c8e)) 54 | 55 | ### [1.0.2](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.0.1...v1.0.2) (2021-05-26) 56 | 57 | 58 | ### Bug Fixes 59 | 60 | * Check for the existence of plugin methods before calling ([2cf5362](https://www.github.com/projectsophon/df-plugin-dev-server/commit/2cf536276c433c964c21cf0e2520362535c9267b)) 61 | 62 | ### [1.0.1](https://www.github.com/projectsophon/df-plugin-dev-server/compare/v1.0.0...v1.0.1) (2021-05-23) 63 | 64 | 65 | ### Bug Fixes 66 | 67 | * **docs:** Add note about Adblock and Allow Insecure ([fb86e68](https://www.github.com/projectsophon/df-plugin-dev-server/commit/fb86e685b5a7c7cd766ba9dee14aa70225987dd6)) 68 | 69 | ## 1.0.0 (2021-05-23) 70 | 71 | 72 | ### Features 73 | 74 | * Add docs ([8920670](https://www.github.com/projectsophon/df-plugin-dev-server/commit/8920670853192e6fdd014d666ae4624173c9507f)) 75 | * Intial implementation ([dbc7b9e](https://www.github.com/projectsophon/df-plugin-dev-server/commit/dbc7b9e996a0f6a38a8244e2567e1cdce3785abe)) 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @projectsophon/df-plugin-dev-server 2 | 3 | An ESBuild server for Dark Forest plugin development. 4 | 5 | ## Installation 6 | 7 | You can install the command globally using: 8 | 9 | ```bash 10 | npm i -g @projectsophon/df-plugin-dev-server 11 | ``` 12 | 13 | ## Usage 14 | 15 | Once installed, you should have access to the command: 16 | 17 | ```bash 18 | df-plugin-dev-server 19 | ``` 20 | 21 | You can see the supported flags by running: 22 | 23 | ```bash 24 | df-plugin-dev-server 25 | 26 | Start a Dark Forest plugin development server. 27 | 28 | Options: 29 | --help Show help [boolean] 30 | --version Show version number [boolean] 31 | --dir The directory to load [deprecated: use --glob instead] [string] 32 | --ext Extensions to process [deprecated: use --glob instead] [array] 33 | --glob Glob for finding plugins [array] [default: ["plugins/*.(js|ts)"]] 34 | ``` 35 | 36 | And then run the server: 37 | 38 | ```bash 39 | df-plugin-dev-server 40 | ESBuild for /Users/user/sophon-plugins/plugins on port 2221 41 | Development server started on http://127.0.0.1:2222/ 42 | ``` 43 | 44 | ## Adblock & Security 45 | 46 | I noticed that Brave Browser's built-in Adblock stops any requests to `127.0.0.1`, so you'll need to disable that if you are building plugins against https://zkga.me directly. 47 | 48 | You'll also need to "Allow Insecure Content" since this development server doesn't provide an HTTPS certificate. 49 | 50 | ## Plugins 51 | 52 | The easiest way to load plugins while developing would be to use something like this in the Dark Forest UI: 53 | 54 | ```js 55 | // This maps to ./plugins/PluginTemplate.js or ./plugins/PluginTemplate.ts by default 56 | export { default } from "http://127.0.0.1:2222/PluginTemplate.js"; 57 | ``` 58 | 59 | However, Dark Forest plugins are cached, so you'd need to do some sort of cache busting each time you make a change. Luckily, we've taken care of that for you! To get free cache busting, you can add `?dev` to the end of your plugin URL. 60 | 61 | ```js 62 | // Notice the ?dev 63 | export { default } from "http://127.0.0.1:2222/PluginTemplate.js?dev"; 64 | ``` 65 | 66 | Doing the above will proxy your plugin through a cache busting plugin! 67 | 68 | ## Docker image installation and running 69 | 70 | From main directory you can execute commands to build and start docker image: 71 | 72 | To build & run without getting prompted for docker output simply run (with -d): 73 | 74 | `docker-compose -f docker-compose.yml up -d --build --remove-orphans` 75 | 76 | To run an existing image without building, remove `--build --remove-orphans`: 77 | 78 | `docker-compose -f docker-compose.yml up -d` 79 | 80 | To stop you should use `stop`. To stop and delete use `down`: 81 | 82 | ```bash 83 | docker-compose -f docker-compose.yml stop 84 | docker-compose -f docker-compose.yml down 85 | ``` 86 | 87 | To only build the docker image: 88 | 89 | `docker-compose -f docker-compose.yml build ` 90 | 91 | If your image is already built, you can use the `docker` command directly: 92 | 93 | ```bash 94 | docker start plugin_server 95 | docker stop plugin_server 96 | ``` 97 | 98 | ### Security notes for docker containers 99 | 100 | Docker is working on another layer meaning your internet connection from docker containers is bridged through your network interface by default. Starting this container with default configuration using commands from above opens port to this docker container to any incoming IP subnet existing (bind 0.0.0.0). Default system firewall configuration will not prevent access from outside world if your router does not block ports by default. KEEP THIS IN MIND. Storing vulnerable data inside container is not recommended with this configuration. You should edit docker network of `plugin_server` to make it harden. The easiest way is to set flag `--internal` for specific docker network, to prevent access from other computers. 101 | 102 | ## License 103 | 104 | MIT 105 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | import * as path from "path"; 2 | import * as http from "http"; 3 | import { URL } from "url"; 4 | import * as fs from "fs/promises"; 5 | import * as esbuild from "esbuild"; 6 | import getPort from "get-port"; 7 | import { default as chalk } from "chalk"; 8 | import fastGlob from "fast-glob"; 9 | import { createRequire } from "module"; 10 | 11 | const require = createRequire(import.meta.url); 12 | 13 | const { bold, underline } = chalk; 14 | 15 | // Custom resolver that externalizes http:// and https:// imports 16 | const httpExternalResolver = { 17 | name: "http-external", 18 | setup(build) { 19 | // Mark all paths starting with "http://" or "https://" as external 20 | build.onResolve({ filter: /^https?:\/\// }, (args) => { 21 | return { path: args.path, external: true }; 22 | }); 23 | }, 24 | }; 25 | 26 | // Custom resolver that rewrites react->preact/compat 27 | const preactResolver = { 28 | name: "preact", 29 | setup(build) { 30 | build.onResolve({ filter: /^react-dom\/test-utils$/ }, (_args) => { 31 | return { path: require.resolve("preact/test-utils") }; 32 | }); 33 | 34 | build.onResolve({ filter: /^react-dom$/ }, (_args) => { 35 | return { path: require.resolve("preact/compat") }; 36 | }); 37 | 38 | build.onResolve({ filter: /^react$/ }, (_args) => { 39 | return { path: require.resolve("preact/compat") }; 40 | }); 41 | }, 42 | }; 43 | 44 | function getDefaultConfig() { 45 | return { 46 | bundle: true, 47 | format: "esm", 48 | target: ["es2020"], 49 | }; 50 | } 51 | 52 | function getExtraConfig(preact) { 53 | let jsxConfig = {}; 54 | const plugins = [httpExternalResolver]; 55 | if (preact) { 56 | plugins.push(preactResolver); 57 | jsxConfig = { 58 | jsxFactory: "h", 59 | jsxFragment: "Fragment", 60 | }; 61 | } 62 | 63 | return { 64 | plugins, 65 | ...jsxConfig, 66 | }; 67 | } 68 | 69 | export async function bundle({ glob, preact, outdir }) { 70 | const entryPoints = await fastGlob(glob); 71 | const printLocation = glob.map((g) => underline(path.join(process.cwd(), g))).join(", "); 72 | 73 | try { 74 | console.log(`ESBuild bundling ${printLocation} into ${underline(outdir)} directory`); 75 | await esbuild.build({ 76 | entryPoints: entryPoints, 77 | outdir, 78 | ...getDefaultConfig(), 79 | ...getExtraConfig(preact), 80 | }); 81 | } catch (err) { 82 | console.error("Error occurred during bundling", err); 83 | // eslint-disable-next-line no-process-exit 84 | process.exit(1); 85 | } 86 | } 87 | 88 | export async function start({ dir, ext, glob, preact }) { 89 | const proxyPort = await getPort({ port: 2222 }); 90 | const esbuildPort = await getPort({ port: 2221 }); 91 | 92 | let entryPoints; 93 | let printLocation; 94 | if (dir) { 95 | // Deprecated options code path 96 | ext = ext || [".js", ".ts"]; 97 | const pluginDir = path.join(process.cwd(), dir); 98 | 99 | const allFiles = await fs.readdir(pluginDir); 100 | entryPoints = allFiles 101 | .filter((filename) => { 102 | return ext.includes(path.extname(filename)); 103 | }) 104 | .map((filename) => { 105 | return path.join(dir, filename); 106 | }); 107 | printLocation = underline(pluginDir); 108 | } else { 109 | entryPoints = await fastGlob(glob); 110 | printLocation = glob.map((g) => underline(path.join(process.cwd(), g))).join(", "); 111 | } 112 | 113 | const esbuildServeConfig = { 114 | port: esbuildPort, 115 | }; 116 | 117 | const esbuildConfig = { 118 | entryPoints: entryPoints, 119 | ...getDefaultConfig(), 120 | ...getExtraConfig(preact), 121 | }; 122 | 123 | const { host, port } = await esbuild.serve(esbuildServeConfig, esbuildConfig); 124 | 125 | const proxyServer = http.createServer((req, res) => { 126 | const { pathname, searchParams } = new URL(req.url, `http://${req.headers.host}`); 127 | 128 | if (searchParams.has("dev")) { 129 | res.writeHead(200, { 130 | "content-type": "application/javascript", 131 | "access-control-allow-origin": "*", 132 | }); 133 | res.end(templateForPathname(pathname)); 134 | return; 135 | } 136 | 137 | const options = { 138 | hostname: host, 139 | port: port, 140 | path: req.url, 141 | method: req.method, 142 | headers: req.headers, 143 | }; 144 | 145 | // Forward each incoming request to esbuild 146 | const proxyReq = http.request(options, (proxyRes) => { 147 | res.writeHead(proxyRes.statusCode, { 148 | ...proxyRes.headers, 149 | "access-control-allow-origin": "*", 150 | }); 151 | proxyRes.pipe(res, { end: true }); 152 | }); 153 | 154 | // Forward the body of the request to esbuild 155 | req.pipe(proxyReq, { end: true }); 156 | }); 157 | 158 | proxyServer.listen(proxyPort, () => { 159 | console.log(`ESBuild for ${printLocation} on port ${underline(esbuildPort)}`); 160 | console.log(`Development server started on ${bold(`http://127.0.0.1:${proxyPort}/`)}`); 161 | }); 162 | } 163 | 164 | function templateForPathname(pathname) { 165 | return `// Development plugin with auto-reload 166 | class Plugin { 167 | constructor() { 168 | this.plugin = null; 169 | } 170 | async render(container) { 171 | const cacheBust = Date.now(); 172 | const modulePath = "${pathname}?" + cacheBust; 173 | const { default: RevealMarket } = await import(modulePath); 174 | this.plugin = new RevealMarket(); 175 | await this.plugin?.render?.(container); 176 | } 177 | draw(ctx) { 178 | this.plugin?.draw?.(ctx); 179 | } 180 | destroy() { 181 | this.plugin?.destroy?.(); 182 | } 183 | } 184 | 185 | export default Plugin; 186 | `; 187 | } 188 | --------------------------------------------------------------------------------