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

Update ESM detection for running fom source and CSS module creation #136

Merged
merged 2 commits into from
Sep 3, 2024
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
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.vscode
.npmignore
.editorconfig
.eslintrc.js
.eslint.config.js
.prettierrc
.github/
build/
Expand Down
43 changes: 38 additions & 5 deletions src/server/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class Workbench {
WORKBENCH_AUTH_SESSION: '',
WORKBENCH_WEB_BASE_URL: this.baseUrl,
WORKBENCH_BUILTIN_EXTENSIONS: asJSON(this.builtInExtensions),
WORKBENCH_MAIN: this.getMain(),
WORKBENCH_DEV_CSS_MODULES: JSON.stringify(this.devCSSModules)
WORKBENCH_MAIN: this.getMain()
};

try {
Expand All @@ -58,7 +57,30 @@ class Workbench {

getMain() {
if (this.esm) {
return `<script type="module" src="${this.baseUrl}/out/vs/code/browser/workbench/workbench.js"></script>`;
const lines = this.devCSSModules.length > 0 ? [
"<script>",
`globalThis._VSCODE_CSS_MODULES = ${JSON.stringify(this.devCSSModules)};`,
"</script>",
"<script>",
"const sheet = document.getElementById('vscode-css-modules').sheet;",
"globalThis._VSCODE_CSS_LOAD = function (url) { sheet.insertRule(`@import url(${url});`); };",
"",
"const importMap = { imports: {} };",
"for (const cssModule of globalThis._VSCODE_CSS_MODULES) {",
" const cssUrl = new URL(cssModule, globalThis._VSCODE_FILE_ROOT).href;",
" const jsSrc = `globalThis._VSCODE_CSS_LOAD('${cssUrl}');\\n`;",
" const blob = new Blob([jsSrc], { type: 'application/javascript' });",
" importMap.imports[cssUrl] = URL.createObjectURL(blob);",
"}",
"const importMapElement = document.createElement('script');",
"importMapElement.type = 'importmap';",
"importMapElement.setAttribute('nonce', '1nline-m4p');",
"importMapElement.textContent = JSON.stringify(importMap, undefined, 2);",
"document.head.appendChild(importMapElement);",
"</script>"
] : [];
lines.push(`<script type="module" src="${this.baseUrl}/out/vs/code/browser/workbench/workbench.js"></script>`);
return lines.join('\n');
}
if (this.dev) {
return `<script> require(['vs/code/browser/workbench/workbench'], function() {}); </script>`;
Expand Down Expand Up @@ -137,8 +159,10 @@ export default function (config: IConfig): Router.Middleware {
if (config.build.type === 'sources') {
const builtInExtensions = await getScannedBuiltinExtensions(config.build.location);
const productOverrides = await getProductOverrides(config.build.location);
const devCSSModules = config.esm ? await getDevCssModules(config.build.location) : [];
ctx.state.workbench = new Workbench(`${ctx.protocol}://${ctx.host}/static/sources`, true, config.esm, devCSSModules, builtInExtensions, {
const esm = config.esm || await isESM(config.build.location);
console.log('Using ESM loader:', esm);
const devCSSModules = esm ? await getDevCssModules(config.build.location) : [];
ctx.state.workbench = new Workbench(`${ctx.protocol}://${ctx.host}/static/sources`, true, esm, devCSSModules, builtInExtensions, {
...productOverrides,
webEndpointUrlTemplate: `${ctx.protocol}://{{uuid}}.${ctx.host}/static/sources`,
webviewContentExternalBaseUrlTemplate: `${ctx.protocol}://{{uuid}}.${ctx.host}/static/sources/out/vs/workbench/contrib/webview/browser/pre/`
Expand Down Expand Up @@ -183,3 +207,12 @@ async function getDevCssModules(vsCodeDevLocation: string): Promise<string[]> {
const glob = await import('glob')
return glob.glob('**/*.css', { cwd: path.join(vsCodeDevLocation, 'out') });
}

async function isESM(vsCodeDevLocation: string): Promise<boolean> {
try {
const packageJSON = await fs.readFile(path.join(vsCodeDevLocation, 'out', 'package.json'));
return JSON.parse(packageJSON.toString()).type === 'module';
} catch (e) {
return false;
}
}
20 changes: 0 additions & 20 deletions views/workbench-esm.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@
const baseUrl = new URL('{{WORKBENCH_WEB_BASE_URL}}', window.location.origin).toString();
globalThis._VSCODE_FILE_ROOT = baseUrl + '/out/';
</script>
<script>
const sheet = document.getElementById('vscode-css-modules').sheet;
globalThis._VSCODE_CSS_LOAD = function (url) {
sheet.insertRule(`@import url(${url});`);
};

const importMap = { imports: {} };
const cssModules = JSON.parse('{{WORKBENCH_DEV_CSS_MODULES}}');
for (const cssModule of cssModules) {
const cssUrl = new URL(cssModule, globalThis._VSCODE_FILE_ROOT).href;
const jsSrc = `globalThis._VSCODE_CSS_LOAD('${cssUrl}');\n`;
const blob = new Blob([jsSrc], { type: 'application/javascript' });
importMap.imports[cssUrl] = URL.createObjectURL(blob);
}
const importMapElement = document.createElement('script');
importMapElement.type = 'importmap';
importMapElement.setAttribute('nonce', '1nline-m4p')
importMapElement.textContent = JSON.stringify(importMap, undefined, 2);
document.head.appendChild(importMapElement);
</script>
<script>
performance.mark('code/willLoadWorkbenchMain');
</script>
Expand Down
Loading