Skip to content

Commit

Permalink
refactor: startup entry
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 13, 2022
1 parent 2ee31fb commit 8378270
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 51 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-monkey",
"version": "0.2.14",
"version": "1.0.0-alpha.0",
"description": "vite plugin for Tampermonkey, Violentmonkey and Greasemonkey",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -49,6 +49,7 @@
"@commitlint/config-conventional": "^17.0.3",
"@types/cross-spawn": "^6.0.2",
"@types/node": "14.18.12",
"@types/node-fetch": "2.6.2",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"eslint": "^8.19.0",
Expand Down Expand Up @@ -80,6 +81,8 @@
},
"dependencies": {
"cross-spawn": "7.0.3",
"htmlparser2": "8.0.1",
"node-fetch": "2.6.7",
"open": "8.4.0",
"picocolors": "1.0.0"
},
Expand Down
118 changes: 118 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 71 additions & 7 deletions src/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ export const delay = async (n = 0) => {
};

/**
* @link https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0
* @link https://stackoverflow.com/questions/7616461/
*/
export function hashCode(s: string) {
let h = 0;
for (let i = 0; i < s.length; i++) {
h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;
export const hashCode = (str = '', seed = 0) => {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
return h;
}
h1 =
Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^
Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 =
Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^
Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};

export const validUrl = (s: string) => {
try {
Expand Down Expand Up @@ -154,3 +163,58 @@ export const compatResolve = (() => {
return hostRequire.resolve(id);
};
})();

export const lazy = <T extends object>(fn: () => T) => {
let temp: object | undefined = undefined;
return new Proxy<T>({} as T, {
set(_, p, value, receiver) {
if (!temp) {
temp = fn();
}
return Reflect.set(temp, p, value, receiver);
},
get(_, p, receiver) {
if (!temp) {
temp = fn();
}
return Reflect.get(temp, p, receiver);
},
apply(_, thisArg, argArray) {
if (!temp) {
temp = fn();
}
// @ts-ignore
return Reflect.apply(temp, thisArg, argArray);
},
ownKeys(_) {
if (!temp) {
temp = fn();
}
return Reflect.ownKeys(temp);
},
});
};

export const traverse = <T>(
target: T,
getChildren: (target: T) => T[],
action: (target: T) => void | true
) => {
const stack = [target];
while (stack.length > 0) {
const top = stack.pop()!;
if (action(top)) {
break;
}
stack.push(...getChildren(top));
}
};

import fs from 'fs/promises';
export const existFile = async (path: string) => {
try {
return (await fs.stat(path)).isFile();
} catch {
return false;
}
};
Loading

0 comments on commit 8378270

Please sign in to comment.