Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --esm flag and the follow up logic #58

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export interface Options {
*/
coi?: boolean;

/**
* If set, serves the page with ESM usage.
*/
esm?: boolean;

/**
* @deprecated. Use `printServerLog` instead.
*/
Expand Down Expand Up @@ -160,7 +165,8 @@ export async function runTests(options: Options & { extensionTestsPath: string }
printServerLog: options.printServerLog ?? options.hideServerLog === false,
extensionPaths: options.extensionPaths,
extensionIds: options.extensionIds,
coi: !!options.coi
coi: !!options.coi,
esm: !!options.esm
};


Expand Down Expand Up @@ -221,7 +227,8 @@ export async function open(options: Options): Promise<Disposable> {
printServerLog: options.printServerLog ?? options.hideServerLog === false,
extensionPaths: options.extensionPaths,
extensionIds: options.extensionIds,
coi: !!options.coi
coi: !!options.coi,
esm: !!options.esm
};

const host = options.host ?? 'localhost';
Expand Down Expand Up @@ -497,6 +504,7 @@ interface CommandLineOptions {
port?: string;
verbose?: boolean;
coi?: boolean;
esm?: boolean;
help?: boolean;
}

Expand All @@ -511,6 +519,7 @@ function showHelp() {
console.log(` --headless: Whether to hide the browser. Defaults to true when an extensionTestsPath is provided, otherwise false. [Optional]`);
console.log(` --permission: Permission granted in the opened browser: e.g. 'clipboard-read', 'clipboard-write'. [Optional, Multiple]`);
console.log(` --coi: Enables cross origin isolation [Optional]`);
console.log(` --esm: Serve the ESM variant of VS Code [Optional]`);
console.log(` --folder-uri: workspace to open VS Code on. Ignored when folderPath is provided. [Optional]`);
console.log(` --extensionPath: A path pointing to a folder containing additional extensions to include [Optional, Multiple]`);
console.log(` --extensionId: The id of an extension include. The format is '\${publisher}.\${name}'. Append '@prerelease' to use a prerelease version [Optional, Multiple]`);
Expand All @@ -536,7 +545,7 @@ async function cliMain(): Promise<void> {

const options: minimist.Opts = {
string: ['extensionDevelopmentPath', 'extensionTestsPath', 'browser', 'browserType', 'quality', 'version', 'waitForDebugger', 'folder-uri', 'permission', 'extensionPath', 'extensionId', 'sourcesPath', 'host', 'port'],
boolean: ['open-devtools', 'headless', 'hideServerLog', 'printServerLog', 'help', 'verbose', 'coi'],
boolean: ['open-devtools', 'headless', 'hideServerLog', 'printServerLog', 'help', 'verbose', 'coi', 'esm'],
unknown: arg => {
if (arg.startsWith('-')) {
console.log(`Unknown argument ${arg}`);
Expand Down Expand Up @@ -567,6 +576,7 @@ async function cliMain(): Promise<void> {
const port = validatePortNumber(args.port);
const host = validateStringOrUndefined(args, 'host');
const coi = validateBooleanOrUndefined(args, 'coi');
const esm = validateBooleanOrUndefined(args, 'esm');

const waitForDebugger = validatePortNumber(args.waitForDebugger);

Expand Down Expand Up @@ -602,6 +612,7 @@ async function cliMain(): Promise<void> {
extensionIds,
vsCodeDevPath,
verbose,
esm,
coi,
host,
port
Expand All @@ -625,6 +636,7 @@ async function cliMain(): Promise<void> {
extensionIds,
vsCodeDevPath,
verbose,
esm,
coi,
host,
port
Expand Down
2 changes: 1 addition & 1 deletion src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IConfig {
readonly folderMountPath: string | undefined;
readonly printServerLog: boolean;
readonly coi: boolean;
readonly esm: boolean;
}

export interface GalleryExtensionInfo {
Expand Down Expand Up @@ -54,4 +55,3 @@ export async function runServer(host: string, port: number | undefined, config:
throw e;
}
}

21 changes: 12 additions & 9 deletions src/server/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function asJSON(value: unknown): string {
}

class Workbench {
constructor(readonly baseUrl: string, readonly dev: boolean, private readonly builtInExtensions: IScannedBuiltinExtension[] = []) { }
constructor(readonly baseUrl: string, readonly dev: boolean, readonly esm: boolean, private readonly builtInExtensions: IScannedBuiltinExtension[] = []) { }

async render(workbenchWebConfiguration: IWorkbenchOptions): Promise<string> {
const values: { [key: string]: string } = {
Expand All @@ -46,17 +46,21 @@ class Workbench {
};

try {
const workbenchTemplate = (await fs.readFile(path.resolve(__dirname, '../../views/workbench.html'))).toString();
const workbenchTemplate = (await fs.readFile(path.resolve(__dirname, `../../views/workbench${this.esm ? '-esm' : ''}.html`))).toString();
return workbenchTemplate.replace(/\{\{([^}]+)\}\}/g, (_, key) => values[key] ?? 'undefined');
} catch (e) {
return String(e);
}
}

getMain() {
return this.dev
? `<script> require(['vs/code/browser/workbench/workbench'], function() {}); </script>`
: `<script src="${this.baseUrl}/out/vs/workbench/workbench.web.main.nls.js"></script>`
if (this.esm) {
return `<script type="module" src="${this.baseUrl}/out/vs/code/browser/workbench/workbench.js"></script>`;
}
if (this.dev) {
return `<script> require(['vs/code/browser/workbench/workbench'], function() {}); </script>`;
}
return `<script src="${this.baseUrl}/out/vs/workbench/workbench.web.main.nls.js"></script>`
+ `<script src="${this.baseUrl}/out/vs/workbench/workbench.web.main.js"></script>`
+ `<script src="${this.baseUrl}/out/vs/code/browser/workbench/workbench.js"></script>`;
}
Expand Down Expand Up @@ -129,11 +133,11 @@ export default function (config: IConfig): Router.Middleware {
router.use(async (ctx, next) => {
if (config.build.type === 'sources') {
const builtInExtensions = await getScannedBuiltinExtensions(config.build.location);
ctx.state.workbench = new Workbench(`${ctx.protocol}://${ctx.host}/static/sources`, true, builtInExtensions);
ctx.state.workbench = new Workbench(`${ctx.protocol}://${ctx.host}/static/sources`, true, config.esm, builtInExtensions);
} else if (config.build.type === 'static') {
ctx.state.workbench = new Workbench(`${ctx.protocol}://${ctx.host}/static/build`, false);
ctx.state.workbench = new Workbench(`${ctx.protocol}://${ctx.host}/static/build`, false, config.esm);
} else if (config.build.type === 'cdn') {
ctx.state.workbench = new Workbench(config.build.uri, false);
ctx.state.workbench = new Workbench(config.build.uri, false, config.esm);
}
await next();
});
Expand All @@ -153,4 +157,3 @@ export default function (config: IConfig): Router.Middleware {

return router.routes();
}

46 changes: 46 additions & 0 deletions views/workbench-esm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- Copyright (C) Microsoft Corporation. All rights reserved. -->
<!DOCTYPE html>
<html>
<head>
<script>
performance.mark('code/didStartRenderer')
</script>
<meta charset="utf-8" />

<!-- Mobile tweaks -->
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-title" content="Code">
<link rel="apple-touch-icon" href="{{WORKBENCH_WEB_BASE_URL}}/code-192.png" />

<!-- Disable pinch zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">

<!-- Workbench Configuration -->
<meta id="vscode-workbench-web-configuration" data-settings="{{WORKBENCH_WEB_CONFIGURATION}}">

<!-- Workbench Auth Session -->
<meta id="vscode-workbench-auth-session" data-settings="{{WORKBENCH_AUTH_SESSION}}">

<!-- Builtin Extensions (running out of sources) -->
<meta id="vscode-workbench-builtin-extensions" data-settings="{{WORKBENCH_BUILTIN_EXTENSIONS}}">

<!-- Workbench Icon/Manifest/CSS -->
<link rel="icon" href="{{WORKBENCH_WEB_BASE_URL}}/favicon.ico" type="image/x-icon" />
<link rel="manifest" href="{{WORKBENCH_WEB_BASE_URL}}/manifest.json">
</head>

<body aria-label="">
</body>

<!-- Startup (do not modify order of script tags!) -->
<script>
// BASE URL
const baseUrl = new URL('{{WORKBENCH_WEB_BASE_URL}}', window.location.origin).toString();
globalThis._VSCODE_FILE_ROOT = baseUrl + '/out/';
</script>
<script>
performance.mark('code/willLoadWorkbenchMain');
</script>
{{WORKBENCH_MAIN}}
</html>