7 Commits

Author SHA1 Message Date
75309d0945 fix build
Some checks failed
Publish documentation / docs (push) Failing after 2s
Check Semantic Commit / Validate PR Title (pull_request) Failing after 2s
Test / Test (22.12.x) (pull_request) Has been cancelled
2025-12-12 17:11:16 +01:00
6f0968cdce Merge branch 'main' into esm-asar-entrypoints 2025-12-12 14:42:02 +01:00
Erick Zhao
b3059564b7 Merge branch 'main' into esm-asar-entrypoints 2024-06-21 16:12:39 -07:00
Erick Zhao
f7d15b8d34 Merge remote-tracking branch 'origin' into esm-asar-entrypoints 2024-06-17 15:18:33 -07:00
Erick Zhao
1c55526cdb Update package.json
Co-authored-by: Erik Moura <erikian@erikian.dev>
2024-06-16 21:04:57 -07:00
Erick Zhao
e9a5812213 add no-asar 2024-06-12 20:50:35 -07:00
Erick Zhao
ed1efe60a0 beep boop 2024-06-12 20:34:54 -07:00
11 changed files with 133 additions and 38 deletions

6
.gitignore vendored
View File

@@ -1,7 +1,9 @@
node_modules node_modules
dist dist
entry-asar/*.js* entry-asar/cjs/*.js*
entry-asar/*.ts entry-asar/cjs/*.d.ts
entry-asar/esm/*.?js*
entry-asar/esm/*.d.?ts
*.app *.app
test/fixtures/apps test/fixtures/apps
coverage coverage

5
.npmignore Normal file
View File

@@ -0,0 +1,5 @@
# npmignore overrides .gitignore for yarn pack
# Only exclude source files, not built files
entry-asar/**/*.mts
entry-asar/**/*.ts
entry-asar/**/tsconfig.json

View File

@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": ".",
},
"include": [
".",
"../ambient.d.ts"
],
"exclude": []
}

View File

@@ -0,0 +1,28 @@
import { app } from 'electron';
import { createRequire } from 'node:module';
import path from 'node:path';
if (process.arch === 'arm64') {
await setPaths('arm64');
} else {
await setPaths('x64');
}
async function setPaths(platform: string) {
// This should return the full path, ending in something like
// Notion.app/Contents/Resources/app.asar
const appPath = app.getAppPath();
const asarFile = `app-${platform}.asar`;
// Maybe we'll handle this in Electron one day
if (path.basename(appPath) === 'app.asar') {
const platformAppPath = path.join(path.dirname(appPath), asarFile);
// This is an undocumented API. It exists.
app.setAppPath(platformAppPath);
}
const require = createRequire(import.meta.url);
process._archPath = require.resolve(`../${asarFile}`);
await import(process._archPath);
}

View File

@@ -0,0 +1,29 @@
import { app } from 'electron';
import { createRequire } from 'node:module';
import path from 'node:path';
if (process.arch === 'arm64') {
await setPaths('arm64');
} else {
await setPaths('x64');
}
async function setPaths(platform: string) {
// This should return the full path, ending in something like
// Notion.app/Contents/Resources/app
const appPath = app.getAppPath();
const appFolder = `app-${platform}`;
// Maybe we'll handle this in Electron one day
if (path.basename(appPath) === 'app') {
const platformAppPath = path.join(path.dirname(appPath), appFolder);
// This is an undocumented private API. It exists.
app.setAppPath(platformAppPath);
}
const require = createRequire(import.meta.url);
process._archPath = require.resolve(`../${appFolder}`);
await import(process._archPath);
}

View File

@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"target":"ESNext",
"outDir": ".",
"moduleResolution": "bundler"
},
"include": [
".",
"../ambient.d.ts"
],
"exclude": []
}

View File

@@ -19,8 +19,12 @@
}, },
"files": [ "files": [
"dist/*", "dist/*",
"entry-asar/*", "entry-asar/**/*",
"!entry-asar/**/*.ts", "!entry-asar/**/has-asar.ts",
"!entry-asar/**/no-asar.ts",
"!entry-asar/**/has-asar.mts",
"!entry-asar/**/no-asar.mts",
"!entry-asar/**/tsconfig.json",
"README.md" "README.md"
], ],
"author": "Samuel Attard", "author": "Samuel Attard",
@@ -28,7 +32,7 @@
"provenance": true "provenance": true
}, },
"scripts": { "scripts": {
"build": "tsc -p tsconfig.json && tsc -p tsconfig.entry-asar.json", "build": "tsc -p tsconfig.json && tsc -p entry-asar/esm/tsconfig.json && tsc -p entry-asar/cjs/tsconfig.json",
"build:docs": "npx typedoc", "build:docs": "npx typedoc",
"lint": "prettier --check \"{src,entry-asar,test}/**/*.ts\" \"*.ts\"", "lint": "prettier --check \"{src,entry-asar,test}/**/*.ts\" \"*.ts\"",
"prettier:write": "prettier --write \"{src,entry-asar,test}/**/*.ts\" \"*.ts\"", "prettier:write": "prettier --write \"{src,entry-asar,test}/**/*.ts\" \"*.ts\"",

View File

@@ -254,17 +254,30 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise<void> =
const entryAsar = path.resolve(tmpDir, 'entry-asar'); const entryAsar = path.resolve(tmpDir, 'entry-asar');
await fs.promises.mkdir(entryAsar, { recursive: true }); await fs.promises.mkdir(entryAsar, { recursive: true });
await fs.promises.cp(
path.resolve(import.meta.dirname, '..', 'entry-asar', 'no-asar.js'),
path.resolve(entryAsar, 'index.js'),
);
let pj = JSON.parse( let pj = JSON.parse(
await fs.promises.readFile( await fs.promises.readFile(
path.resolve(opts.x64AppPath, 'Contents', 'Resources', 'app', 'package.json'), path.resolve(opts.x64AppPath, 'Contents', 'Resources', 'app', 'package.json'),
'utf8', 'utf8',
), ),
); );
pj.main = 'index.js';
// Load a shim that redirects to the correct folder for the architecture.
// This needs to be a different file depending on if the app entrypoint is CommonJS or ESM.
if (pj.type === 'module' || pj.main.endsWith('.mjs')) {
await fs.promises.cp(
path.resolve(import.meta.dirname, '..', 'entry-asar', 'esm', 'no-asar.mjs'),
path.resolve(entryAsar, 'index.mjs'),
);
pj.main = 'index.mjs';
} else {
await fs.promises.cp(
path.resolve(import.meta.dirname, '..', 'entry-asar', 'cjs', 'no-asar.js'),
path.resolve(entryAsar, 'index.js'),
);
pj.main = 'index.js';
}
await fs.promises.writeFile( await fs.promises.writeFile(
path.resolve(entryAsar, 'package.json'), path.resolve(entryAsar, 'package.json'),
JSON.stringify(pj) + '\n', JSON.stringify(pj) + '\n',
@@ -337,10 +350,6 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise<void> =
const entryAsar = path.resolve(tmpDir, 'entry-asar'); const entryAsar = path.resolve(tmpDir, 'entry-asar');
await fs.promises.mkdir(entryAsar, { recursive: true }); await fs.promises.mkdir(entryAsar, { recursive: true });
await fs.promises.cp(
path.resolve(import.meta.dirname, '..', 'entry-asar', 'has-asar.js'),
path.resolve(entryAsar, 'index.js'),
);
let pj = JSON.parse( let pj = JSON.parse(
( (
await asar.extractFile( await asar.extractFile(
@@ -349,7 +358,23 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise<void> =
) )
).toString('utf8'), ).toString('utf8'),
); );
pj.main = 'index.js';
// Load a shim that redirects to the correct `app.asar` for the architecture.
// This needs to be a different file depending on if the app entrypoint is CommonJS or ESM.
if (pj.type === 'module' || pj.main.endsWith('.mjs')) {
await fs.promises.cp(
path.resolve(import.meta.dirname, '..', 'entry-asar', 'esm', 'has-asar.mjs'),
path.resolve(entryAsar, 'index.mjs'),
);
pj.main = 'index.mjs';
} else {
await fs.promises.cp(
path.resolve(import.meta.dirname, '..', 'entry-asar', 'cjs', 'has-asar.js'),
path.resolve(entryAsar, 'index.js'),
);
pj.main = 'index.js';
}
await fs.promises.writeFile( await fs.promises.writeFile(
path.resolve(entryAsar, 'package.json'), path.resolve(entryAsar, 'package.json'),
JSON.stringify(pj) + '\n', JSON.stringify(pj) + '\n',

View File

@@ -1,23 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"lib": [
"es2017"
],
"sourceMap": true,
"strict": true,
"outDir": "entry-asar",
"types": [
"node",
],
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": false
},
"include": [
"entry-asar"
],
"exclude": []
}