Path: blob/main/src/vs/platform/cssDev/node/cssDevService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { spawn } from 'child_process';6import { relative } from '../../../base/common/path.js';7import { FileAccess } from '../../../base/common/network.js';8import { StopWatch } from '../../../base/common/stopwatch.js';9import { IEnvironmentService } from '../../environment/common/environment.js';10import { createDecorator } from '../../instantiation/common/instantiation.js';11import { ILogService } from '../../log/common/log.js';1213export const ICSSDevelopmentService = createDecorator<ICSSDevelopmentService>('ICSSDevelopmentService');1415export interface ICSSDevelopmentService {16_serviceBrand: undefined;17isEnabled: boolean;18getCssModules(): Promise<string[]>;19}2021export class CSSDevelopmentService implements ICSSDevelopmentService {2223declare _serviceBrand: undefined;2425private _cssModules?: Promise<string[]>;2627constructor(28@IEnvironmentService private readonly envService: IEnvironmentService,29@ILogService private readonly logService: ILogService30) { }3132get isEnabled(): boolean {33return !this.envService.isBuilt;34}3536getCssModules(): Promise<string[]> {37this._cssModules ??= this.computeCssModules();38return this._cssModules;39}4041private async computeCssModules(): Promise<string[]> {42if (!this.isEnabled) {43return [];44}4546const rg = await import('@vscode/ripgrep');47return await new Promise<string[]>((resolve) => {4849const sw = StopWatch.create();5051const chunks: Buffer[] = [];52const basePath = FileAccess.asFileUri('').fsPath;53const process = spawn(rg.rgPath, ['-g', '**/*.css', '--files', '--no-ignore', basePath], {});5455process.stdout.on('data', data => {56chunks.push(data);57});58process.on('error', err => {59this.logService.error('[CSS_DEV] FAILED to compute CSS data', err);60resolve([]);61});62process.on('close', () => {63const data = Buffer.concat(chunks).toString('utf8');64const result = data.split('\n').filter(Boolean).map(path => relative(basePath, path).replace(/\\/g, '/')).filter(Boolean).sort();65if (result.some(path => path.indexOf('vs/') !== 0)) {66this.logService.error(`[CSS_DEV] Detected invalid paths in css modules, raw output: ${data}`);67}68resolve(result);69this.logService.info(`[CSS_DEV] DONE, ${result.length} css modules (${Math.round(sw.elapsed())}ms)`);70});71});72}73}747576