From fb863f1a59fc1dd057090adb801022900f4608db Mon Sep 17 00:00:00 2001 From: "Yuichiro Tachibana (Tsuchiya)" Date: Thu, 15 Dec 2022 11:23:24 +0900 Subject: [PATCH] feature/self contained desktop package (#439) * Bundle yargs and fs-extra into desktop/bin/dump_artifacts.js but leave pyodide and node-fetch as externals because they must be necessarily installed at runtime anyway as the Wasm files shipped with pyodide are used at runtime and node-fetch is one of the dependencies of the pyodide package * Create scripts/build_bin.js --- packages/desktop/package.json | 9 +++++++-- packages/desktop/scripts/build_bin.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100755 packages/desktop/scripts/build_bin.js diff --git a/packages/desktop/package.json b/packages/desktop/package.json index 09d044c12..26993903d 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -20,7 +20,7 @@ "start:electron": "tsc -p electron -w", "build:electron": "tsc -p electron", "build:pyodide": "curl -L https://github.com/pyodide/pyodide/releases/download/0.21.3/pyodide-build-0.21.3.tar.bz2 | tar xj -C ./build --files-from=./pyodide-files.txt", - "build:bin": "esbuild ./bin/dump_artifacts.ts --bundle --minify --platform=node --external:@stlite/kernel --external:node-fetch --external:pyodide --external:yargs --external:fs-extra --outfile=./bin/dump_artifacts.js && sed -i'' -e '1 s/^#!.*$/#!\\/usr\\/bin\\/env node/' ./bin/*.js", + "build:bin": "./scripts/build_bin.js && sed -i'' -e '1 s/^#!.*$/#!\\/usr\\/bin\\/env node/' ./bin/*.js", "typecheck": "yarn tsc --noEmit -p electron", "start": "concurrently \"cross-env BROWSER=none yarn start:web\" \"wait-on http://localhost:3000 && yarn start:electron\" \"wait-on http://localhost:3000 && tsc -p electron && cross-env NODE_ENV=\"development\" electron .\"", "build:app": "yarn build:web && yarn build:electron && yarn build:pyodide", @@ -48,6 +48,11 @@ "last 1 electron version" ] }, + "//": "The packages not bundled with bin/dump_artifacts.js must be specified here as the runtime dependencies. See `scripts/build_bin.js` for the details.", + "dependencies": { + "node-fetch": "2", + "pyodide": "^0.21.3" + }, "devDependencies": { "@craco/craco": "^6.1.2", "@stlite/common": "^0.20.0", @@ -74,7 +79,7 @@ "typescript": "^4.6.3", "yargs": "^17.5.1" }, - "//": "build.productName is necessary because electron-builder uses the package name for its purpose but the scoped name including '@' makes a problem: https://github.com/electron-userland/electron-builder/issues/3230", + "///": "build.productName is necessary because electron-builder uses the package name for its purpose but the scoped name including '@' makes a problem: https://github.com/electron-userland/electron-builder/issues/3230", "build": { "appId": "your.id", "mac": { diff --git a/packages/desktop/scripts/build_bin.js b/packages/desktop/scripts/build_bin.js new file mode 100755 index 000000000..744705a74 --- /dev/null +++ b/packages/desktop/scripts/build_bin.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node + +// Build script using esbuild like https://esbuild.github.io/getting-started/#build-scripts + +require("esbuild") + .build({ + entryPoints: ["./bin/dump_artifacts.ts"], + bundle: true, + minify: true, + platform: "node", + external: [ + "@stlite/kernel", // Don't touch `require.resolve("@stlite/kernel") at the bundle time. + "pyodide", // The `pyodide` package must be installed at runtime for the included Wasm files, so there is no reason to bundle it here. + "node-fetch", // `node-fetch` will be installed at runtime anyway because it is one dependency of the `pyodide` package, so there is no reason to bundle it here. + ], + outfile: "./bin/dump_artifacts.js", + logLevel: "info", + }) + .catch(() => process.exit(1));