Skip to content

Commit

Permalink
Experiment with allowing absolute paths for imports in core
Browse files Browse the repository at this point in the history
For microsoft#236644

Only works for dev right now
  • Loading branch information
mjbvz committed Dec 19, 2024
1 parent 41793c2 commit 670d50a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"main": "./out/main.js",
"type": "module",
"private": true,
"imports": {
"#vs/*.js": "./out/vs/*.js"
},
"scripts": {
"test": "echo Please run any of the test scripts from the scripts folder.",
"test-browser": "npx playwright install && node test/unit/browser/index.js",
Expand Down
30 changes: 17 additions & 13 deletions src/bootstrap-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
globalThis._VSCODE_FILE_ROOT = baseUrl.toString();

// Dev only: CSS import map tricks
setupCSSImportMaps<T>(configuration, baseUrl);
setupDevImportMaps<T>(configuration, baseUrl);

// ESM Import
try {
Expand Down Expand Up @@ -181,13 +181,18 @@
return uri.replace(/#/g, '%23');
}

function setupCSSImportMaps<T extends ISandboxConfiguration>(configuration: T, baseUrl: URL) {
function setupDevImportMaps<T extends ISandboxConfiguration>(configuration: T, baseUrl: URL) {

// DEV ---------------------------------------------------------------------------------------
// DEV: This is for development and enables loading CSS via import-statements via import-maps.
// DEV: For each CSS modules that we have we defined an entry in the import map that maps to
// DEV: a blob URL that loads the CSS via a dynamic @import-rule.
// DEV ---------------------------------------------------------------------------------------
const importMap: { imports: Record<string, string> } = {
imports: {
'#vs/': baseUrl.href + '/vs/'
}
};

if (Array.isArray(configuration.cssModules) && configuration.cssModules.length > 0) {
performance.mark('code/willAddCssLoader');
Expand All @@ -202,25 +207,24 @@
style.textContent += `@import url(${url});\n`;
};

const importMap: { imports: Record<string, string> } = { imports: {} };
for (const cssModule of configuration.cssModules) {
const cssUrl = new URL(cssModule, baseUrl).href;
const jsSrc = `globalThis._VSCODE_CSS_LOAD('${cssUrl}');\n`;
const blob = new Blob([jsSrc], { type: 'application/javascript' });
importMap.imports[cssUrl] = URL.createObjectURL(blob);
}
}

const ttp = window.trustedTypes?.createPolicy('vscode-bootstrapImportMap', { createScript(value) { return value; }, });
const importMapSrc = JSON.stringify(importMap, undefined, 2);
const importMapScript = document.createElement('script');
importMapScript.type = 'importmap';
importMapScript.setAttribute('nonce', '0c6a828f1297');
// @ts-ignore
importMapScript.textContent = ttp?.createScript(importMapSrc) ?? importMapSrc;
document.head.appendChild(importMapScript);
const ttp = window.trustedTypes?.createPolicy('vscode-bootstrapImportMap', { createScript(value) { return value; }, });
const importMapSrc = JSON.stringify(importMap, undefined, 2);
const importMapScript = document.createElement('script');
importMapScript.type = 'importmap';
importMapScript.setAttribute('nonce', '0c6a828f1297');
// @ts-ignore
importMapScript.textContent = ttp?.createScript(importMapSrc) ?? importMapSrc;
document.head.appendChild(importMapScript);

performance.mark('code/didAddCssLoader');
}
performance.mark('code/didAddCssLoader');
}

(globalThis as any).MonacoBootstrapWindow = { load };
Expand Down
2 changes: 1 addition & 1 deletion src/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"vs/*": [
"#vs/*": [
"./vs/*"
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/common/codecs/baseToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IRange, Range } from '../../../editor/common/core/range.js';
import { IRange, Range } from '#vs/editor/common/core/range.js';

/**
* Base class for all tokens with a `range` that
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/log/common/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as nls from '../../../nls.js';
import * as nls from '#vs/nls.js';
import { toErrorMessage } from '../../../base/common/errorMessage.js';
import { Emitter, Event } from '../../../base/common/event.js';
import { hash } from '../../../base/common/hash.js';
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/policy/node/nativePolicyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IStringDictionary } from '../../../base/common/collections.js';
import { Throttler } from '../../../base/common/async.js';
import type { PolicyUpdate, Watcher } from '@vscode/policy-watcher';
import { MutableDisposable } from '../../../base/common/lifecycle.js';
import { ILogService } from '../../log/common/log.js';
import { ILogService } from '#vs/platform/log/common/log.js';

export class NativePolicyService extends AbstractPolicyService implements IPolicyService {

Expand Down

0 comments on commit 670d50a

Please sign in to comment.