-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
81 lines (66 loc) · 2.04 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
workspace,
window,
commands,
type ExtensionContext,
type TextDocument,
} from 'vscode';
import setText from 'vscode-set-text';
import setSelectedText from 'vscode-set-selected-text';
import {minify} from 'csso';
type Options = csso.MinifyOptions & csso.CompressOptions;
function isCss({languageId}: TextDocument): boolean {
return languageId === 'css';
}
function getPluginConfig(): Options {
const cssoConfig = workspace.getConfiguration('svgo');
const pluginConfig: Options = {
restructure: cssoConfig.get('restructure'),
};
return pluginConfig;
}
async function commandMinify(): Promise<void> {
if (!window.activeTextEditor) {
return;
}
if (!isCss(window.activeTextEditor.document)) {
return;
}
try {
const config: Options = getPluginConfig();
const {document} = window.activeTextEditor;
const result = minify(document.getText(), config);
await setText(result.css, window.activeTextEditor);
await window.showInformationMessage('Minified current CSS file');
} catch (error: unknown) {
console.error(error);
if (error instanceof Error) {
await window.showErrorMessage(error.message);
}
}
}
async function commandMinifySelected(): Promise<void> {
if (!window.activeTextEditor) {
return;
}
try {
const config: Options = getPluginConfig();
const {document, selection} = window.activeTextEditor;
const result = minify(document.getText(selection), config);
await setSelectedText(result.css, window.activeTextEditor);
await window.showInformationMessage('Minified selected CSS');
} catch (error: unknown) {
console.error(error);
if (error instanceof Error) {
await window.showErrorMessage(error.message);
}
}
}
export function activate(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand('csso.minify', commandMinify),
commands.registerCommand('csso.minify-selected', commandMinifySelected),
);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
export function deactivate() {}