move linter worker loaders to edit/

This commit is contained in:
tophf 2018-07-10 10:55:16 +03:00
parent 9cdacc85ec
commit 26f673f30d
4 changed files with 66 additions and 6 deletions

View File

@ -1,7 +1,9 @@
/* global parserlib CSSLint parseMozFormat */ /* global importScripts parserlib CSSLint parseMozFormat */
'use strict'; 'use strict';
self.importScripts('./parserlib.js'); const CSSLINT_PATH = '/vendor-overwrites/csslint/';
importScripts(CSSLINT_PATH + 'parserlib.js');
parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false; parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false;
self.onmessage = ({data}) => { self.onmessage = ({data}) => {
@ -14,7 +16,7 @@ self.onmessage = ({data}) => {
return; return;
} }
if (!self.CSSLint) self.importScripts('./csslint.js'); if (!self.CSSLint) self.importScripts(CSSLINT_PATH + 'csslint.js');
switch (action) { switch (action) {
case 'getAllRuleIds': case 'getAllRuleIds':

View File

@ -20,8 +20,8 @@ var linterConfig = {
stylelint: 'editorStylelintConfig', stylelint: 'editorStylelintConfig',
}, },
worker: { worker: {
csslint: {path: '/vendor-overwrites/csslint/csslint-loader.js'}, csslint: {path: '/edit/csslint-loader.js'},
stylelint: {path: '/vendor-overwrites/stylelint/stylelint-bundle.min.js'}, stylelint: {path: '/edit/stylelint-loader.js'},
}, },
allRuleIds: { allRuleIds: {
csslint: null, csslint: null,

58
edit/stylelint-loader.js Normal file
View File

@ -0,0 +1,58 @@
/* global require importScripts */
'use strict';
importScripts('/vendor/stylelint-bundle/stylelint-bundle.min.js');
const stylelint = require('stylelint');
self.onmessage = ({data: {action = 'run', code, config}}) => {
switch (action) {
case 'getAllRuleIds':
// the functions are non-tranferable
self.postMessage(Object.keys(stylelint.rules));
return;
case 'getAllRuleOptions':
self.postMessage(getAllRuleOptions());
return;
case 'run':
stylelint.lint({code, config}).then(results =>
self.postMessage(results));
return;
}
};
function getAllRuleOptions() {
const options = {};
const rxPossible = /\bpossible:("(?:[^"]*?)"|\[(?:[^\]]*?)\]|\{(?:[^}]*?)\})/g;
const rxString = /"([-\w\s]{3,}?)"/g;
for (const id of Object.keys(stylelint.rules)) {
const ruleCode = String(stylelint.rules[id]);
const sets = [];
let m, mStr;
while ((m = rxPossible.exec(ruleCode))) {
const possible = m[1];
const set = [];
while ((mStr = rxString.exec(possible))) {
const s = mStr[1];
if (s.includes(' ')) {
set.push(...s.split(/\s+/));
} else {
set.push(s);
}
}
if (possible.includes('ignoreAtRules')) {
set.push('ignoreAtRules');
}
if (possible.includes('ignoreShorthands')) {
set.push('ignoreShorthands');
}
if (set.length) {
sets.push(set);
}
}
if (sets.length) {
options[id] = sets;
}
}
return options;
}

View File

@ -600,7 +600,7 @@ var usercss = (() => {
function invokeWorker(message) { function invokeWorker(message) {
if (!worker.queue) { if (!worker.queue) {
worker.instance = new Worker('/vendor-overwrites/csslint/csslint-loader.js'); worker.instance = new Worker('/edit/csslint-loader.js');
worker.queue = []; worker.queue = [];
worker.instance.onmessage = ({data}) => { worker.instance.onmessage = ({data}) => {
worker.queue.shift().resolve(data.__ERROR__ ? Promise.reject(data.__ERROR__) : data); worker.queue.shift().resolve(data.__ERROR__ ? Promise.reject(data.__ERROR__) : data);