Skip to content

Commit 6e69e2d

Browse files
authored
feat!: enable asar by default (#1841)
* feat!: enable `asar` by default * fixups * Update usage.txt with asar default behavior details Clarified default behavior of --asar option regarding native node modules.
1 parent 623a49c commit 6e69e2d

File tree

6 files changed

+37
-46
lines changed

6 files changed

+37
-46
lines changed

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function parseArgs(argv: string[]) {
3030
'tmpdir',
3131
],
3232
default: {
33+
asar: true,
3334
junk: true,
3435
prune: true,
3536
tmpdir: true,

src/common.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,22 @@ export function subOptionWarning(
5555
properties[parameter] = value;
5656
}
5757

58+
/**
59+
* Creates the final `@electron/asar` options for final packaging function based on user options.
60+
* @param opts The processed options for the packager.
61+
* @returns The ASAR options or false if ASAR is disabled.
62+
*/
5863
export function createAsarOpts(
5964
opts: ProcessedOptionsWithSinglePlatformArch,
6065
): false | AsarOptions {
61-
let asarOptions;
62-
if (opts.asar === true) {
63-
asarOptions = {};
66+
let asarOptions: AsarOptions;
67+
if (opts.asar === true || opts.asar === undefined) {
68+
asarOptions = {
69+
unpack: '**/{.**,**}/**/*.node',
70+
};
6471
} else if (typeof opts.asar === 'object') {
6572
asarOptions = opts.asar;
66-
} else if (opts.asar === false || opts.asar === undefined) {
73+
} else if (opts.asar === false) {
6774
return false;
6875
} else {
6976
warning(

src/platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'graceful-fs';
22
import path from 'node:path';
33
import {
4-
createPackageWithOptions,
4+
createPackageWithOptions as createASARWithOptions,
55
FileRecord,
66
getRawHeader,
77
} from '@electron/asar';
@@ -340,7 +340,7 @@ export class App {
340340
this.hookArgsWithOriginalResourcesAppDir,
341341
);
342342

343-
await createPackageWithOptions(
343+
await createASARWithOptions(
344344
this.originalResourcesAppDir,
345345
this.appAsarPath,
346346
this.asarOptions,

test/common.spec.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,26 @@ describe('validateElectronApp', () => {
130130
});
131131

132132
describe('createAsarOpts', () => {
133-
it('returns false if asar is not set', () => {
134-
expect(createAsarOpts({} as ProcessedOptionsWithSinglePlatformArch)).toBe(
135-
false,
136-
);
133+
it('unpacks native node modules if true', () => {
134+
expect(
135+
createAsarOpts({ asar: true } as ProcessedOptionsWithSinglePlatformArch),
136+
).toEqual({
137+
unpack: '**/{.**,**}/**/*.node',
138+
});
137139
});
138-
it('returns false if asar is false', () => {
140+
141+
it('returns true if asar is not set', () => {
139142
expect(
140-
createAsarOpts({ asar: false } as ProcessedOptionsWithSinglePlatformArch),
141-
).toBe(false);
143+
createAsarOpts({} as ProcessedOptionsWithSinglePlatformArch),
144+
).toEqual({
145+
unpack: '**/{.**,**}/**/*.node',
146+
});
142147
});
143148

144-
it('sets asar options to {} if true', () => {
149+
it('returns false if asar is false', () => {
145150
expect(
146-
createAsarOpts({ asar: true } as ProcessedOptionsWithSinglePlatformArch),
147-
).toEqual({});
151+
createAsarOpts({ asar: false } as ProcessedOptionsWithSinglePlatformArch),
152+
).toBe(false);
148153
});
149154

150155
it('sets asar options to the value if it is an object', () => {

test/packager.spec.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,7 @@ describe('packager', () => {
9292
),
9393
).toBe(false);
9494

95-
// packages main.js
96-
const inputMain = path.join(__dirname, 'fixtures', 'basic', 'main.js');
97-
const outputMain = path.join(resourcesPath, 'app', 'main.js');
98-
99-
expect(fs.readFileSync(inputMain, 'utf8')).toEqual(
100-
fs.readFileSync(outputMain, 'utf8'),
101-
);
102-
103-
// packages subdirectory resources
104-
const inputResource = path.join(
105-
__dirname,
106-
'fixtures',
107-
'basic',
108-
'ignore',
109-
'this.txt',
110-
);
111-
const outputResource = path.join(
112-
resourcesPath,
113-
'app',
114-
'ignore',
115-
'this.txt',
116-
);
117-
118-
expect(fs.readFileSync(inputResource, 'utf8')).toEqual(
119-
fs.readFileSync(outputResource, 'utf8'),
120-
);
121-
95+
expect(fs.existsSync(path.join(resourcesPath, 'app.asar'))).toBe(true);
12296
expect(fs.existsSync(path.join(resourcesPath, 'default_app'))).toBe(false);
12397
expect(fs.existsSync(path.join(resourcesPath, 'default_app.asar'))).toBe(
12498
false,
@@ -213,6 +187,7 @@ describe('packager', () => {
213187
derefSymlinks: false,
214188
platform: 'linux',
215189
arch: 'x64',
190+
asar: false,
216191
} as const;
217192

218193
const src = path.join(opts.dir, 'main.js');
@@ -569,19 +544,19 @@ describe('packager', () => {
569544
'beforeCopy',
570545
'afterCopy',
571546
'afterPrune',
547+
'afterAsar',
572548
'afterInitialize',
573549
'afterComplete',
574550
],
575551
},
576552
{
577-
testOpts: { asar: true },
553+
testOpts: { asar: false },
578554
expectedOutput: [
579555
'afterFinalizePackageTargets',
580556
'afterExtract',
581557
'beforeCopy',
582558
'afterCopy',
583559
'afterPrune',
584-
'afterAsar',
585560
'afterInitialize',
586561
'afterComplete',
587562
],
@@ -593,6 +568,7 @@ describe('packager', () => {
593568
'afterExtract',
594569
'beforeCopy',
595570
'afterCopy',
571+
'afterAsar',
596572
'afterInitialize',
597573
'afterComplete',
598574
],
@@ -717,6 +693,7 @@ describe('packager', () => {
717693
}) => {
718694
const opts = {
719695
...baseOpts,
696+
asar: false,
720697
};
721698

722699
const paths = await packager(opts);

usage.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ arch all, or one or more of: ia32, x64, armv7l, arm64, mips64el, u
2828
asar whether to package the source code within your app into an archive. You can either
2929
pass --asar by itself to use the default configuration, OR use dot notation to
3030
configure a list of sub-properties, e.g. --asar.unpackDir=sub_dir - do not use
31-
--asar and its sub-properties simultaneously.
31+
--asar and its sub-properties simultaneously. Defaults to `true`, which sets `asar.unpack`
32+
to '**/{.**,**}/**/*.node' to automatically unpack native node modules.
3233

3334
Properties supported include:
3435
- ordering: path to an ordering file for file packing

0 commit comments

Comments
 (0)