Skip to content

Commit

Permalink
Merge pull request titaniumnetwork-dev#151 from titaniumnetwork-dev/p…
Browse files Browse the repository at this point in the history
…erformance-improvemts

Performance Improvements
  • Loading branch information
Percslol authored Jul 12, 2024
2 parents eb501ec + ecf5952 commit 52e3cee
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 377 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
*.tgz
*.tgz
metafile.json
9 changes: 7 additions & 2 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { rimraf } from 'rimraf';
import { copyFile, mkdir, readFile } from 'node:fs/promises';
import { copyFile, mkdir, readFile, writeFile } from 'node:fs/promises';
import { build } from 'esbuild';

// read version from package.json
Expand All @@ -15,7 +15,7 @@ await mkdir('dist');
await copyFile('src/sw.js', 'dist/sw.js');
await copyFile('src/uv.config.js', 'dist/uv.config.js');

await build({
let builder = await build({
platform: 'browser',
sourcemap: true,
minify: !isDevelopment,
Expand All @@ -31,6 +31,11 @@ await build({
),
},
bundle: true,
treeShaking: true,
metafile: isDevelopment,
logLevel: 'info',
outdir: 'dist/',
});
if (isDevelopment) {
await writeFile('metafile.json', JSON.stringify(builder.metafile));
}
115 changes: 17 additions & 98 deletions package-lock.json

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

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
},
"devDependencies": {
"@mercuryworkshop/bare-mux": "^2.0.1",
"css-tree": "^2.3.1",
"astring": "^1.8.6",
"esbuild": "^0.18.11",
"eslint": "^8.28.0",
"esotope-hammerhead": "^0.6.4",
"events": "^3.3.0",
"idb": "^7.1.1",
"meriyah": "^4.3.7",
"mime-db": "^1.52.0",
"parse5": "^7.1.2",
"prettier": "^2.8.0",
"rimraf": "^5.0.1",
Expand Down
24 changes: 8 additions & 16 deletions src/rewrite/css.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { parse, walk, generate } from 'css-tree';

import EventEmitter from 'events';

class CSS extends EventEmitter {
constructor(ctx) {
super();
this.ctx = ctx;
this.meta = ctx.meta;
this.parse = parse;
this.walk = walk;
this.generate = generate;
}
rewrite(str, options) {
return this.recast(str, options, 'rewrite');
Expand All @@ -19,18 +16,13 @@ class CSS extends EventEmitter {
recast(str, options, type) {
if (!str) return str;
str = new String(str).toString();
try {
const ast = this.parse(str, {
...options,
parseCustomProperty: true,
});
this.walk(ast, (node) => {
this.emit(node.type, node, options, type);
});
return this.generate(ast);
} catch (e) {
return str;
}
str = str.replace(/(?<=url\("?'?)[^"'][\S]*[^"'](?="?'?\);?)/gm, (match) => {
return type === "rewrite" ? this.ctx.rewriteUrl(match) : this.ctx.sourceUrl(match);
});
str = str.replace(/@import\s+(['"])?([^'"\);]+)\1?\s*(?:;|$)/gm, (match, quote, url) => {
return `@import ${quote || ""}${type === "rewrite" ? this.ctx.rewriteUrl(url) : this.ctx.sourceUrl(url)}${quote || ""};`;
});
return str;
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/rewrite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import CSS from './css.js';
import JS from './js.js';
import setCookie from 'set-cookie-parser';
import { xor, base64, plain } from './codecs.js';
import * as mimeTypes from './mime.js';
import {
validateCookie,
db,
Expand All @@ -23,7 +22,6 @@ import {
createHtmlInject,
createJsInject,
} from './rewrite.html.js';
import { importStyle, url } from './rewrite.css.js';
//import { call, destructureDeclaration, dynamicImport, getProperty, importDeclaration, setProperty, sourceMethods, wrapEval, wrapIdentifier } from './rewrite.script.js';
import {
dynamicImport,
Expand Down Expand Up @@ -157,9 +155,6 @@ class Ultraviolet {
attributes(this);
text(this);
injectHead(this);
// CSS
url(this);
importStyle(this);
// JS
importDeclaration(this);
dynamicImport(this);
Expand Down Expand Up @@ -187,7 +182,6 @@ class Ultraviolet {
return this.js.source.bind(this.js);
}
static codec = { xor, base64, plain };
static mime = mimeTypes;
static setCookie = setCookie;
static openDB = openDB;
static BareClient = BareClient;
Expand Down
2 changes: 1 addition & 1 deletion src/rewrite/js.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseScript } from 'meriyah';
// import { parse } from 'acorn-hammerhead';
import { generate } from 'esotope-hammerhead';
import { generate } from 'astring';
import EventEmitter from 'events';

class JS extends EventEmitter {
Expand Down
Loading

0 comments on commit 52e3cee

Please sign in to comment.