This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
115 lines (98 loc) · 2.73 KB
/
index.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/** @babel */
import {CompositeDisposable} from 'atom';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
import postcssSafeParser from 'postcss-safe-parser';
import postcssScss from 'postcss-scss';
import unprefix from 'postcss-unprefix';
const SUPPORTED_SCOPES = new Set([
'source.css',
'source.css.scss'
]);
async function init(editor, onSave, type) {
const selectedText = onSave ? null : editor.getSelectedText();
const text = selectedText || editor.getText();
const options = {};
if (editor.getGrammar().scopeName === 'source.css') {
options.parser = postcssSafeParser;
} else {
options.syntax = postcssScss;
}
try {
let result;
if (type === 'prefix') {
result = await postcss(autoprefixer(atom.config.get('autoprefixer'))).process(text, options);
} else {
result = await postcss([unprefix()]).process(text, options);
}
result.warnings().forEach(x => {
console.warn(x.toString());
atom.notifications.addWarning('Autoprefixer', {
detail: x.toString()
});
});
if (selectedText) {
editor.setTextInBufferRange(editor.getSelectedBufferRange(), result.css);
} else {
editor.getBuffer().setTextViaDiff(result.css);
}
} catch (error) {
if (error.name === 'CssSyntaxError') {
error.message += error.showSourceCode();
}
console.error(error);
atom.notifications.addError('Autoprefixer', {detail: error.message});
}
}
export const config = {
browsers: {
title: 'Supported Browsers',
description: 'Using the [following syntax](https://github.com/ai/browserslist#queries).',
type: 'array',
default: autoprefixer.defaults,
items: {
type: 'string'
}
},
cascade: {
title: 'Cascade Prefixes',
type: 'boolean',
default: true
},
remove: {
title: 'Remove Unneeded Prefixes',
type: 'boolean',
default: true
},
runOnSave: {
title: 'Run on Save',
type: 'boolean',
default: false
}
};
export function deactivate() {
this.subscriptions.dispose();
}
export function activate() {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.workspace.observeTextEditors(editor => {
editor.getBuffer().onWillSave(async () => {
const isCSS = SUPPORTED_SCOPES.has(editor.getGrammar().scopeName);
if (isCSS && atom.config.get('autoprefixer.runOnSave')) {
await init(editor, true, 'prefix');
}
});
}));
this.subscriptions.add(atom.commands.add('atom-workspace', 'autoprefixer:prefix', () => {
const editor = atom.workspace.getActiveTextEditor();
if (editor) {
init(editor, false, 'prefix');
}
}));
this.subscriptions.add(atom.commands.add('atom-workspace', 'autoprefixer:remove-prefixes', () => {
const editor = atom.workspace.getActiveTextEditor();
if (editor) {
init(editor, false, 'removePrefixes');
}
}));
}