Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47f56e9

Browse files
committed
perf(-): remove jquery dependencies
reduce bundle size(minified): 136kb -> 66kb
1 parent a7a51d5 commit 47f56e9

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"main": "index.js",
66
"dependencies": {
77
"deep-diff": "^0.3.4",
8-
"jquery": "^3.1.0",
98
"lodash": "^4.15.0",
109
"rxjs": "^5.0.0-beta.11"
1110
},

src/app.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import $ from 'jquery/dist/jquery.slim';
21
import clone from 'lodash/clone';
32
import template from 'lodash/template';
43
import filter from 'lodash/filter';
54
import findIndex from 'lodash/findIndex';
5+
66
import DeepDiff from 'deep-diff';
7+
78
import { Subject } from 'rxjs/Subject';
89
import { Observable } from 'rxjs/Observable';
910
import 'rxjs/add/observable/fromEvent';
@@ -19,10 +20,10 @@ import 'rxjs/add/operator/scan';
1920
import 'rxjs/add/operator/mergeMap';
2021

2122
function readyElements(el, templates) {
22-
const $select = $(".subnav-spacer-right .select-menu-list", el);
23+
const selectElement = el.querySelector('.subnav-spacer-right .select-menu-list');
2324
return {
24-
$input: injectInputform($select, templates),
25-
$select
25+
inputElement: injectInputform(selectElement, templates),
26+
selectElement
2627
}
2728
}
2829

@@ -59,11 +60,11 @@ function createProps(el) {
5960
}
6061

6162
function intent({ element }) {
62-
const { $input, $select } = element;
63+
const { inputElement, selectElement } = element;
6364

6465
const error$ = new Subject();
6566
const enterInput$ = Observable
66-
.fromEvent($input, 'keypress')
67+
.fromEvent(inputElement, 'keypress')
6768
.filter(e => e.key === 'Enter')
6869
.filter(() => {
6970
const isFilterInput = !!location.search;
@@ -74,7 +75,7 @@ function intent({ element }) {
7475

7576
return isFilterInput;
7677
})
77-
.filter(e => $.trim(e.target.value) !== '')
78+
.filter(e => e.target.value.trim() !== '')
7879
.do(e => {
7980
e.preventDefault();
8081
e.stopPropagation();
@@ -85,7 +86,7 @@ function intent({ element }) {
8586
return value;
8687
});
8788
const clickDelBtn$ = Observable
88-
.fromEvent($select, 'click')
89+
.fromEvent(selectElement, 'click')
8990
.filter(({ target }) => {
9091
return target.classList.contains('gfe--deleteBtn')
9192
|| target.parentNode.classList.contains('gfe--deleteBtn');
@@ -151,28 +152,29 @@ function view(state$, { element, template }) {
151152

152153
function updateItem(path, rhs) {
153154
const [ idx, key ] = path;
154-
const $target = $('.gfe--filterItem').eq(idx);
155+
const targetElement = document.getElementsByClassName('gfe--filterItem')[idx];
155156

156157
if (key === 'name') {
157-
$target.find('.select-menu-item-text').text(rhs);
158-
$target.find('svg').attr('data-name', rhs);
159-
$target.attr('id', rhs);
158+
targetElement.querySelector('.select-menu-item-text').textContent = rhs;
159+
targetElement.querySelector('svg').setAttribute('data-name', rhs);
160+
targetElement.id = rhs;
160161
} else if (key === 'path') {
161-
$target.attr('href', rhs);
162+
targetElement.href = rhs;
162163
}
163164
}
164165

165166
function removeItem(name) {
166-
$(`#${name}`).remove();
167+
const targetNode = document.getElementById(name);
168+
targetNode.parentNode.removeChild(targetNode);
167169
}
168170

169171
function patch(diff, element, template) {
170172
const { kind, item, rhs, lhs, path } = diff;
171-
const { $input } = element;
173+
const { inputElement } = element;
172174
const { tplItem } = template;
173175

174176
if(kind === 'N') {
175-
prependItem(tplItem, $input, rhs);
177+
prependItem(tplItem, inputElement, rhs);
176178
} else if(kind === 'A') {
177179
patch(item, element, template);
178180
} else if(kind === 'E') {
@@ -182,9 +184,9 @@ function patch(diff, element, template) {
182184
}
183185
}
184186

185-
function prependItem(tplItem, $target, data) {
187+
function prependItem(tplItem, targetElement, data) {
186188
const itemHtml = template(tplItem)(data);
187-
$target.before(itemHtml);
189+
targetElement.insertAdjacentHTML('beforebegin', itemHtml);
188190
}
189191

190192
function main(props) {
@@ -214,10 +216,13 @@ function run(main, el) {
214216
DOM.subscribe();
215217
}
216218

217-
function injectInputform($select, { tplInput }) {
218-
const $input = $(tplInput);
219-
$select.children().last().before($input);
220-
return $input;
219+
function injectInputform(selectNode, { tplInput }) {
220+
const childNods = selectNode.children;
221+
const lastIdx = childNods.length - 1;
222+
223+
childNods[lastIdx].insertAdjacentHTML('beforebegin', tplInput);
224+
225+
return selectNode.children[lastIdx];
221226
}
222227

223228
function isOnInvalidPath() {
@@ -230,7 +235,13 @@ function isOnInvalidPath() {
230235
return isNotGithubPage || hasNoFilterOnPage;
231236
}
232237

233-
$(document).ready(() => {
238+
;(function ready(fn) {
239+
if (document.readyState != 'loading'){
240+
fn();
241+
} else {
242+
document.addEventListener('DOMContentLoaded', fn);
243+
}
244+
})(() => {
234245
const runner = run.bind(null, main, document);
235246
chrome.extension.onMessage.addListener(runner);
236247
runner.call(this);

0 commit comments

Comments
 (0)