Skip to content

Commit 31b64c7

Browse files
authored
fix(vscode): isolate package target outputs (#298)
2 parents 9ec160d + 6ce6595 commit 31b64c7

3 files changed

Lines changed: 108 additions & 6 deletions

File tree

editors/vscode/scripts/package/manifest.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ export function stagePackageJsonForTarget(
3232
context: PackageContext,
3333
plan: PackagePlan,
3434
): string | undefined {
35-
if (!plan.targetSpec.removeBrowserEntry && plan.profileTrace) {
36-
return undefined;
37-
}
38-
3935
const packagePath = packageJsonPath(context);
4036
const originalPackageJson = fs.readFileSync(packagePath, 'utf8');
4137
const packageJson = JSON.parse(originalPackageJson) as {
38+
main?: unknown;
4239
browser?: unknown;
4340
contributes?: { commands?: Array<{ command?: unknown }> };
4441
};
45-
if (plan.targetSpec.removeBrowserEntry) {
42+
if (plan.targetSpec.kind === 'web') {
43+
delete packageJson.main;
44+
} else if (plan.targetSpec.removeBrowserEntry) {
4645
delete packageJson.browser;
4746
}
4847
if (!plan.profileTrace) {
@@ -55,6 +54,16 @@ export function stagePackageJsonForTarget(
5554
return originalPackageJson;
5655
}
5756

57+
export function stageDistFilesForTarget(context: PackageContext, plan: PackagePlan): void {
58+
const distDir = path.join(context.vscodeDir, 'dist');
59+
if (plan.targetSpec.kind === 'web') {
60+
fs.rmSync(path.join(distDir, 'extension.js'), { force: true });
61+
return;
62+
}
63+
64+
fs.rmSync(path.join(distDir, 'browser'), { recursive: true, force: true });
65+
}
66+
5867
export function stageProfileTraceAssets(context: PackageContext, plan: PackagePlan): void {
5968
const speedscopeDir = path.join(context.vscodeDir, 'dist', 'speedscope');
6069
if (!plan.profileTrace) {

editors/vscode/scripts/package/packageExtension.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'node:path';
44
import { type PackageContext, createPackageContext } from './context';
55
import {
66
restorePackageJson,
7+
stageDistFilesForTarget,
78
stageProfileTraceAssets,
89
stagePackageJsonForTarget,
910
syncReadmeFromRepoRoot,
@@ -25,7 +26,13 @@ export function packageExtension(
2526

2627
if (plan.targetSpec.kind === 'web') {
2728
cleanRuntimeServerFiles(context);
28-
runVscePackage(context, plan);
29+
stageDistFilesForTarget(context, plan);
30+
const originalPackageJson = stagePackageJsonForTarget(context, plan);
31+
try {
32+
runVscePackage(context, plan);
33+
} finally {
34+
restorePackageJson(context, originalPackageJson);
35+
}
2936
return path.join(context.vscodeDir, plan.vsixFile);
3037
}
3138

@@ -38,6 +45,7 @@ export function packageExtension(
3845
);
3946
cleanRuntimeServerFiles(context);
4047
const runtimeServerPath = stageRuntimeServer(context, targetServerPath, plan.targetSpec);
48+
stageDistFilesForTarget(context, plan);
4149
const originalPackageJson = stagePackageJsonForTarget(context, plan);
4250

4351
try {

editors/vscode/test/package.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { PackageContext } from '../scripts/package/context';
88
import { parsePackageCliArgs } from '../scripts/package/cli';
99
import {
1010
restorePackageJson,
11+
stageDistFilesForTarget,
1112
stagePackageJsonForTarget,
1213
stageProfileTraceAssets,
1314
} from '../scripts/package/manifest';
@@ -88,6 +89,90 @@ describe('package staging', () => {
8889
);
8990
});
9091

92+
it('removes desktop entry points from web packages', () => {
93+
const context = temporaryPackageContext();
94+
fs.writeFileSync(
95+
path.join(context.vscodeDir, 'package.json'),
96+
`${JSON.stringify(
97+
{
98+
main: './dist/extension.js',
99+
browser: './dist/browser/extension.js',
100+
contributes: {
101+
commands: [
102+
{ command: 'vide.profileDiagnostics' },
103+
{ command: 'vide.showOutput' },
104+
],
105+
},
106+
},
107+
null,
108+
2,
109+
)}\n`,
110+
);
111+
112+
const plan = createPackagePlan({
113+
target: 'web',
114+
profile: 'release',
115+
serverMode: 'build',
116+
});
117+
const originalPackageJson = stagePackageJsonForTarget(context, plan);
118+
const packageJson = JSON.parse(
119+
fs.readFileSync(path.join(context.vscodeDir, 'package.json'), 'utf8'),
120+
) as {
121+
main?: unknown;
122+
browser?: unknown;
123+
contributes?: { commands?: Array<{ command?: unknown }> };
124+
};
125+
126+
assert.equal(packageJson.main, undefined);
127+
assert.equal(packageJson.browser, './dist/browser/extension.js');
128+
assert.deepEqual(packageJson.contributes?.commands, [{ command: 'vide.showOutput' }]);
129+
130+
restorePackageJson(context, originalPackageJson);
131+
const restored = fs.readFileSync(path.join(context.vscodeDir, 'package.json'), 'utf8');
132+
assert.match(restored, /"main"/);
133+
assert.match(restored, /vide\.profileDiagnostics/);
134+
});
135+
136+
it('removes stale desktop bundle from web packages', () => {
137+
const context = temporaryPackageContext();
138+
const nodeBundle = path.join(context.vscodeDir, 'dist', 'extension.js');
139+
const browserBundle = path.join(context.vscodeDir, 'dist', 'browser', 'extension.js');
140+
fs.mkdirSync(path.dirname(nodeBundle), { recursive: true });
141+
fs.mkdirSync(path.dirname(browserBundle), { recursive: true });
142+
fs.writeFileSync(nodeBundle, '');
143+
fs.writeFileSync(browserBundle, '');
144+
145+
const plan = createPackagePlan({
146+
target: 'web',
147+
profile: 'release',
148+
serverMode: 'build',
149+
});
150+
stageDistFilesForTarget(context, plan);
151+
152+
assert.equal(fs.existsSync(nodeBundle), false);
153+
assert.equal(fs.existsSync(browserBundle), true);
154+
});
155+
156+
it('removes stale browser bundle from native packages', () => {
157+
const context = temporaryPackageContext();
158+
const nodeBundle = path.join(context.vscodeDir, 'dist', 'extension.js');
159+
const browserBundle = path.join(context.vscodeDir, 'dist', 'browser', 'extension.js');
160+
fs.mkdirSync(path.dirname(nodeBundle), { recursive: true });
161+
fs.mkdirSync(path.dirname(browserBundle), { recursive: true });
162+
fs.writeFileSync(nodeBundle, '');
163+
fs.writeFileSync(browserBundle, '');
164+
165+
const plan = createPackagePlan({
166+
target: 'linux-x64',
167+
profile: 'release',
168+
serverMode: 'build',
169+
});
170+
stageDistFilesForTarget(context, plan);
171+
172+
assert.equal(fs.existsSync(nodeBundle), true);
173+
assert.equal(fs.existsSync(browserBundle), false);
174+
});
175+
91176
it('removes stale profile trace assets when profile trace is disabled', () => {
92177
const context = temporaryPackageContext();
93178
const speedscopeDir = path.join(context.vscodeDir, 'dist', 'speedscope');

0 commit comments

Comments
 (0)