From fba1c86cda4a0ee500923c383bf30bf7669f1711 Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 12 Jan 2023 13:31:10 +0100 Subject: [PATCH] add `--esm` flag and the follow up logic --- src/index.ts | 18 +++++++++++++--- src/server/main.ts | 2 +- src/server/workbench.ts | 21 ++++++++++-------- views/workbench-esm.html | 46 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 views/workbench-esm.html diff --git a/src/index.ts b/src/index.ts index 9bf40ad..cfad57b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -75,6 +75,11 @@ export interface Options { */ coi?: boolean; + /** + * If set, serves the page with ESM usage. + */ + esm?: boolean; + /** * @deprecated. Use `printServerLog` instead. */ @@ -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 }; @@ -221,7 +227,8 @@ export async function open(options: Options): Promise { 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'; @@ -497,6 +504,7 @@ interface CommandLineOptions { port?: string; verbose?: boolean; coi?: boolean; + esm?: boolean; help?: boolean; } @@ -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]`); @@ -536,7 +545,7 @@ async function cliMain(): Promise { 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}`); @@ -567,6 +576,7 @@ async function cliMain(): Promise { 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); @@ -602,6 +612,7 @@ async function cliMain(): Promise { extensionIds, vsCodeDevPath, verbose, + esm, coi, host, port @@ -625,6 +636,7 @@ async function cliMain(): Promise { extensionIds, vsCodeDevPath, verbose, + esm, coi, host, port diff --git a/src/server/main.ts b/src/server/main.ts index a8861d8..b41f027 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -15,6 +15,7 @@ export interface IConfig { readonly folderMountPath: string | undefined; readonly printServerLog: boolean; readonly coi: boolean; + readonly esm: boolean; } export interface GalleryExtensionInfo { @@ -54,4 +55,3 @@ export async function runServer(host: string, port: number | undefined, config: throw e; } } - diff --git a/src/server/workbench.ts b/src/server/workbench.ts index 9a25d3a..f9969ad 100644 --- a/src/server/workbench.ts +++ b/src/server/workbench.ts @@ -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 { const values: { [key: string]: string } = { @@ -46,7 +46,7 @@ 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); @@ -54,9 +54,13 @@ class Workbench { } getMain() { - return this.dev - ? `` - : `` + if (this.esm) { + return ``; + } + if (this.dev) { + return ``; + } + return `` + `` + ``; } @@ -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(); }); @@ -153,4 +157,3 @@ export default function (config: IConfig): Router.Middleware { return router.routes(); } - diff --git a/views/workbench-esm.html b/views/workbench-esm.html new file mode 100644 index 0000000..3213f8b --- /dev/null +++ b/views/workbench-esm.html @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {{WORKBENCH_MAIN}} +