Skip to content

Commit

Permalink
chore: use esbuild bundle in pkg (#3490)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Dec 18, 2023
1 parent ec8995c commit b1a51c9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/package_arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Build for arm64
run: |
npm run pkg -- --skip-build --arch=arm64
npm run pkg -- --bundle --skip-build --arch=arm64
- name: Upload artifacts
if: github.event_name == 'workflow_dispatch'
Expand Down
43 changes: 43 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
const esbuild = require('esbuild')
const { cp, stat, readFile, writeFile } = require('fs/promises')
const { exists, emptyDir } = require('fs-extra')
const { join } = require('path')

const outputDir = 'build'

function cleanPkgJson(json) {
delete json.devDependencies
delete json['release-it']
delete json.optionalDependencies
delete json.dependencies
return json
}

/**
* Remove useless fields from package.json, this is needed mostly for `pkg`
* otherwise it will try to bundle dependencies
*/
async function patchPkgJson(path) {
const pkgJsonPath = join(outputDir, path, 'package.json')
const pkgJson = require('./' + pkgJsonPath)
cleanPkgJson(pkgJson)
delete pkgJson.scripts
delete pkgJson.exports
await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2))
}

// from https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
const nativeNodeModulesPlugin = {
name: 'native-node-modules',
Expand Down Expand Up @@ -134,6 +156,27 @@ async function main() {
console.log(`Asset "${path}" does not exist. Skipping...`)
}
}

// create main patched packege.json
const pkgJson = require('./package.json')
cleanPkgJson(pkgJson)

pkgJson.scripts = {
start: 'node index.js',
}

pkgJson.bin = 'index.js'
pkgJson.pkg = {
assets: ['dist/**', 'snippets/**', 'node_modules/**'],
}

await writeFile(
`${outputDir}/package.json`,
JSON.stringify(pkgJson, null, 2),
)

await patchPkgJson('node_modules/@zwave-js/config')
await patchPkgJson('node_modules/zwave-js')
}

main().catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"requireCleanWorkingDir": false
},
"hooks": {
"before:release": "./package.sh --release"
"before:release": "./package.sh --bundle"
},
"npm": {
"publish": true
Expand Down
17 changes: 14 additions & 3 deletions package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ ask() {
done
}

pkg() {
echo "Executing command: npx pkg $@"
npx pkg $@
}

APP=$(node -p "require('./package.json').name")
PKG_FOLDER="pkg"

Expand Down Expand Up @@ -67,14 +72,20 @@ if [ ! -z "$1" ]; then
echo "## Skipping build..."
fi

# if --bundle is passed as argument, cd to `build` folder
if [[ "$@" == *"--bundle"* ]]; then
echo "## Building bundle..."
echo ''
npm run bundle
echo "## Changing directory to build folder"
cd build
fi

if [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
echo "Executing command: pkg package.json -t node$NODE_MAJOR-linux-arm64 --out-path $PKG_FOLDER"
pkg package.json -t node$NODE_MAJOR-linux-arm64 --out-path $PKG_FOLDER
elif [ "$ARCH" = "armv7" ]; then
echo "Executing command: pkg package.json -t node$NODE_MAJOR-linux-armv7 --out-path $PKG_FOLDER"
pkg package.json -t node$NODE_MAJOR-linux-armv7 --out-path $PKG_FOLDER --public-packages=*
else
echo "Executing command: pkg package.json -t node$NODE_MAJOR-linux-x64,node$NODE_MAJOR-win-x64 --out-path $PKG_FOLDER"
pkg package.json -t node$NODE_MAJOR-linux-x64,node$NODE_MAJOR-win-x64 --out-path $PKG_FOLDER
fi

Expand Down

0 comments on commit b1a51c9

Please sign in to comment.