Skip to content

Commit

Permalink
Merge pull request #35 from AminoffZ/wsl-build-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mAmineChniti authored Mar 16, 2024
2 parents ce5ecdc + 011a02f commit 96bfc65
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ module.exports = {
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
ignorePatterns: ['node_modules/', 'github-repo-extension/'],
ignorePatterns: ['node_modules/', 'github-repo-size-extension/'],
rules: {},
};
25 changes: 12 additions & 13 deletions extract-inline.mjs → extract-inline.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const glob = require('tiny-glob');
const path = require('path');
const fs = require('fs');
import { readFileSync, writeFileSync } from 'fs';
import { join, resolve } from 'path';
import glob from 'tiny-glob';

function hash(value) {
function hash(value: string) {
let hash = 5381;
let i = value.length;
while (i) hash = (hash * 33) ^ value.charCodeAt(--i);
return (hash >>> 0).toString(36);
}

async function removeInlineScriptAndStyle(directory) {
async function removeInlineScriptAndStyle(directory: string) {
console.log('Removing Inline Scripts and Styles');
const scriptRegx = /<script[^>]*>([\s\S]+?)<\/script>/g;
const styleRegx = /<style[^>]*>([\s\S]+?)<\/style>/g;
const files = await glob('**/*.html', {
cwd: directory,
dot: true,
aboslute: true,
absolute: false,
filesOnly: true,
});

console.log(`Found ${files.length} files`);

for (const file of files.map((f) => path.join(directory, f))) {
for (const file of files.map((f) => join(directory, f))) {
console.log(`Edit file: ${file}`);
let f = fs.readFileSync(file, { encoding: 'utf-8' });
let f = readFileSync(file, { encoding: 'utf-8' });

let script;
while ((script = scriptRegx.exec(f))) {
Expand All @@ -40,7 +39,7 @@ async function removeInlineScriptAndStyle(directory) {
script[0], // Using script[0] to replace the entire matched script tag
`<script type="module" src="${fn}"></script>`
);
fs.writeFileSync(`${directory}${fn}`, inlineScriptContent);
writeFileSync(`${directory}${fn}`, inlineScriptContent);
console.log(`Inline script extracted and saved at: ${directory}${fn}`);
}

Expand All @@ -52,14 +51,14 @@ async function removeInlineScriptAndStyle(directory) {
style[0], // Using style[0] to replace the entire matched style tag
`<link rel="stylesheet" href="${fn}" />`
);
fs.writeFileSync(`${directory}${fn}`, inlineStyleContent);
writeFileSync(`${directory}${fn}`, inlineStyleContent);
console.log(`Inline style extracted and saved at: ${directory}${fn}`);
}

fs.writeFileSync(file, f);
writeFileSync(file, f);
}
}

removeInlineScriptAndStyle(
path.resolve(import.meta.dir, 'github-repo-size-extension')
resolve(import.meta.dir, 'github-repo-size-extension')
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build:firefox": "bun run versionCTRL firefox && bun run astro build && bun run csp && bun bundler.ts",
"preview": "astro preview",
"astro": "astro",
"csp": "bun -b extract-inline.mjs",
"csp": "bun -b extract-inline.ts",
"versionCTRL": "bun -b update-version.ts",
"format": "prettier --write '**/*.{js,jsx,json,mjs,cjs,ts,tsx,md,yml,yaml}' '!github-repo-size-extension/**' '!node_modules/**' --config ./.prettierrc",
"checkFormat": "prettier --check '**/*.{js,jsx,mjs,cjs,ts,tsx,md,yml,yaml}' '!github-repo-size-extension/**' '!node_modules/**' --config ./.prettierrc",
Expand Down
38 changes: 30 additions & 8 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,51 @@
"version": "0.2.7",
"web_accessible_resources": [
{
"resources": ["script.js"],
"matches": ["https://github.com/*"]
"resources": [
"script.js"
],
"matches": [
"https://github.com/*"
]
}
],
"content_scripts": [
{
"matches": ["https://github.com/*"],
"js": ["content.js"],
"matches": [
"https://github.com/*"
],
"js": [
"content.js"
],
"run_at": "document_end",
"css": ["content.css"]
"css": [
"content.css"
]
}
],
"action": {
"default_popup": "index.html"
},
"permissions": ["storage", "tabs", "webNavigation"],
"permissions": [
"storage",
"tabs",
"webNavigation"
],
"background": {
"service_worker": "background.js"
"scripts": [
"background.js"
]
},
"icons": {
"16": "images/icon16.png",
"32": "images/icon32.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
},
"browser_specific_settings": {
"gecko": {
"id": "mouiylus@gmail.com",
"strict_min_version": "121.0.1"
}
}
}
}
20 changes: 10 additions & 10 deletions update-version.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs').promises;
const path = require('path');
const chalk = require('chalk');
import { join } from 'path';
import { readFile, writeFile } from 'fs/promises';
import chalk from 'chalk';

async function updateManifestVersion() {
try {
const arg = process.argv[2];
const browser = arg && arg === 'firefox' ? 'firefox' : 'chrome';

const packageJsonPath = path.join(import.meta.dir, 'package.json');
const packageJsonPath = join(import.meta.dir, 'package.json');
const { version } = await import(packageJsonPath);

const manifestPath = path.join(import.meta.dir, 'public', 'manifest.json');
const manifestData = await fs.readFile(manifestPath, 'utf-8');
const manifestPath = join(import.meta.dir, 'public', 'manifest.json');
const manifestData = await readFile(manifestPath, 'utf-8');
const manifest = JSON.parse(manifestData);

manifest.version = version;
Expand All @@ -33,7 +33,7 @@ async function updateManifestVersion() {
};
delete manifest.browser_specific_settings;
}
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
await writeFile(manifestPath, JSON.stringify(manifest, null, 2));

console.log(
chalk.green(
Expand All @@ -42,8 +42,8 @@ async function updateManifestVersion() {
' Browser!'
)
);
} catch (error: unknown) {
console.error(chalk.red('Error updating version: ' + error.message));
} catch (error) {
console.error(chalk.red('Error updating version: ' + error));
process.exit(1);
}
}
Expand Down

0 comments on commit 96bfc65

Please sign in to comment.