chore: only copy dep in current platform to reduce size

This commit is contained in:
linonetwo 2023-07-17 17:22:43 +08:00 committed by lin onetwo
parent 66ea5a2c34
commit 2c189d5c32
4 changed files with 61 additions and 31 deletions

View file

@ -1,3 +1,11 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable unicorn/import-style */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable unicorn/prevent-abbreviations */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/**
* Remove all .lproj files
* Based on https://ganeshrvel.medium.com/electron-builder-afterpack-configuration-5c2c986be665
@ -7,7 +15,7 @@ const path = require('path');
const glob = require('glob');
const fs = require('fs-extra');
const util = require('util');
const packageJSON = require('../package.json')
const packageJSON = require('../package.json');
const exec = util.promisify(require('child_process').exec);
/**
@ -15,7 +23,7 @@ const exec = util.promisify(require('child_process').exec);
*/
const keepingLprojRegEx = /(en|zh_CN)\.lproj/g;
/**
*
* Running postMake hook
* @param {*} buildPath /var/folders/qj/7j0zx32d0l75zmnrl1w3m3b80000gn/T/electron-packager/darwin-x64/TidGi-darwin-x64/Electron.app/Contents/Resources/app
* @param {*} electronVersion 12.0.6
* @param {*} platform darwin
@ -35,42 +43,63 @@ exports.default = async (buildPath, electronVersion, platform, arch, callback) =
if (platform === 'darwin') {
await Promise.all(pathsToRemove.map((dir) => fs.remove(dir)));
}
/** copy npm packages with node-worker dependencies with binary or __filename usages, which can't be prepare properly by webpack */
/** copy npm packages with node-worker dependencies with binary (dugite, llama-node) or __filename usages (tiddlywiki), which can't be prepare properly by webpack */
const tasks = [];
if (['production', 'test'].includes(process.env.NODE_ENV)) {
console.log('Copying tiddlywiki dependency to dist');
tasks.push(fs.copy(path.join(projectRoot, 'node_modules', 'zx'), path.join(cwd, 'node_modules', 'zx'), { dereference: true }).then(async () => {
// not using pnpm, because after using it, it always causing problem here, causing `Error: spawn /bin/sh ENOENT` in github actions
// it can probably being "working directory didn't exist" in https://github.com/nodejs/node/issues/9644#issuecomment-282060923
// await exec(`pnpm i --shamefully-hoist --prod --ignore-scripts`, { cwd: path.join(cwd, 'node_modules', 'zx'), shell });
await exec(`npm i --legacy-building`, { cwd: path.join(cwd, 'node_modules', 'zx'), shell });
await exec(`npm i --legacy-building`, { cwd: path.join(cwd, 'node_modules', 'zx', 'node_modules', 'globby'), shell });
await exec(`npm i --legacy-building --ignore-scripts`, { cwd: path.join(cwd, 'node_modules', 'zx', 'node_modules', 'node-fetch'), shell });
}));
tasks.push(
fs.copy(path.join(projectRoot, 'node_modules', 'zx'), path.join(cwd, 'node_modules', 'zx'), { dereference: true }).then(async () => {
// not using pnpm, because after using it, it always causing problem here, causing `Error: spawn /bin/sh ENOENT` in github actions
// it can probably being "working directory didn't exist" in https://github.com/nodejs/node/issues/9644#issuecomment-282060923
// await exec(`pnpm i --shamefully-hoist --prod --ignore-scripts`, { cwd: path.join(cwd, 'node_modules', 'zx'), shell });
await exec(`npm i --legacy-building --production`, { cwd: path.join(cwd, 'node_modules', 'zx'), shell });
await exec(`npm i --legacy-building --production`, { cwd: path.join(cwd, 'node_modules', 'zx', 'node_modules', 'globby'), shell });
await exec(`npm i --legacy-building --production --ignore-scripts`, { cwd: path.join(cwd, 'node_modules', 'zx', 'node_modules', 'node-fetch'), shell });
}),
);
const packagePathsToCopyDereferenced = [
['@tiddlygit', 'tiddlywiki'],
['llama-node'],
['@llama-node','llama-cpp'],
['@llama-node','core'],
['@llama-node','rwkv-cpp'],
['better-sqlite3','build','Release','better_sqlite3.node'],
]
[`sqlite-vss-${process.platform}-${process.arch}`],
['@tiddlygit', 'tiddlywiki', 'package.json'],
['@tiddlygit', 'tiddlywiki', 'boot'],
['@tiddlygit', 'tiddlywiki', 'core'],
['@tiddlygit', 'tiddlywiki', 'plugins'],
['@tiddlygit', 'tiddlywiki', 'themes'],
['@tiddlygit', 'tiddlywiki', 'tiddlywiki.js'],
// llama-node and @llama-node/core etc. include too many source code, so only copy its binary
['llama-node', 'dist'],
['llama-node', 'package.json'],
['llama-node', '20B_tokenizer.json'],
['@llama-node', 'core', 'index.js'],
['@llama-node', 'core', 'package.json'],
// it by default pack files for all platforms, we only copy what current platform needs
['@llama-node', 'core', '@llama-node', `core.${process.platform}-${process.arch}.node`],
['@llama-node', 'rwkv-cpp', 'index.js'],
['@llama-node', 'rwkv-cpp', 'package.json'],
['@llama-node', 'rwkv-cpp', '@llama-node', `rwkv-cpp.${process.platform}-${process.arch}.node`],
['@llama-node', 'llama-cpp', 'index.js'],
['@llama-node', 'llama-cpp', 'package.json'],
['@llama-node', 'llama-cpp', '@llama-node', `llama-cpp.${process.platform}-${process.arch}.node`],
['better-sqlite3', 'build', 'Release', 'better_sqlite3.node'],
// we only need its `main` binary, no need its dependency and code, because we already copy it to src/services/native/externalApp
['app-path', 'main'],
];
for (const packagePathInNodeModules of packagePathsToCopyDereferenced) {
// some binary may not exist in other platforms, so allow failing here.
tasks.push(fs.copy(path.resolve(projectRoot, 'node_modules', ...packagePathInNodeModules), path.resolve(cwd, 'node_modules', ...packagePathInNodeModules), { dereference: true }));
}
const sqliteVssPackages = ['sqlite-vss-linux-x64', 'sqlite-vss-darwin-x64', 'sqlite-vss-darwin-arm64']
for (const sqliteVssPackage of sqliteVssPackages) {
// some binary may not exist in other platforms, so allow failing here.
tasks.push(fs.copy(path.resolve(projectRoot, 'node_modules', sqliteVssPackage), path.resolve(cwd, 'node_modules', sqliteVssPackage), { dereference: true }).catch(() => {}));
tasks.push(
fs.copy(path.resolve(projectRoot, 'node_modules', ...packagePathInNodeModules), path.resolve(cwd, 'node_modules', ...packagePathInNodeModules), { dereference: true })
.catch(error => {
// some binary may not exist in other platforms, so allow failing here.
console.error(`Error copying ${packagePathInNodeModules.join('/')} to dist, in afterPack.js, Error: ${error.message}`);
}),
);
}
// it has things like `git/bin/libexec/git-core/git-add` link to `git/bin/libexec/git-core/git`, to reduce size, so can't use `dereference: true` here.
// And pnpm will have node_modules/dugite to be a shortcut, can't just copy it with `dereference: false`, have to copy from .pnpm folder
tasks.push(fs.copy(path.join(projectRoot, 'node_modules', '.pnpm', `dugite@${packageJSON.dependencies.dugite}`, 'node_modules', 'dugite'), path.join(cwd, 'node_modules', 'dugite'), { dereference: false }));
// we only need its `main` binary, no need its dependency and code, because we already copy it to src/services/native/externalApp
tasks.push(fs.mkdirp(path.join(cwd, 'node_modules', 'app-path')).then(async () => {
await fs.copy(path.join(projectRoot, 'node_modules', 'app-path', 'main'), path.join(cwd, 'node_modules', 'app-path', 'main'), { dereference: true })
}));
tasks.push(
fs.copy(path.join(projectRoot, 'node_modules', '.pnpm', `dugite@${packageJSON.dependencies.dugite}`, 'node_modules', 'dugite'), path.join(cwd, 'node_modules', 'dugite'), {
dereference: false,
}),
);
}
/** sign it for mac m1 https://www.zhihu.com/question/431722091/answer/1592339574 (only work if user run this.)
* And have error