From 7a69ac83e06f1090810f62a9ab2f3afec0163df1 Mon Sep 17 00:00:00 2001 From: bytedream Date: Tue, 14 Jun 2022 23:39:26 +0200 Subject: [PATCH] I hate nodejs. --- src/tsconfig.json | 3 ++- tasks/build.ts | 46 ++++++++++++++++++++++++++++++++++++++++------ tasks/bundle.ts | 6 +++--- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/tsconfig.json b/src/tsconfig.json index 0e88f44..f28cb2b 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -3,13 +3,14 @@ "module": "esnext", "target": "es2019", "removeComments": true, + "esModuleInterop": true, "lib": [ "dom", "es5", "scripthost", "es2015.collection", "es2015.promise" - ] + ], }, "exclude": [ "../node_modules" diff --git a/tasks/build.ts b/tasks/build.ts index d3846d7..3f54d64 100644 --- a/tasks/build.ts +++ b/tasks/build.ts @@ -9,22 +9,55 @@ const sass = require('node-sass') const sassPluginNodeImport = require('node-sass-package-importer') const typescript = require('typescript') -// because of javascript magic, require and import cannot be used at the same time -/*async function build_manifest() { +async function build_manifest() { const manifest = JSON.parse(await fs.readFileSync('src/manifest.json')) - const manifestMatches = [] + manifest['version'] = process.env.npm_package_version + // because nodejs is nodejs, the simple commented out code below cannot be used. + // thus, the following bloated regexes must be used + /*const manifestMatches = [] for (const m of matches) { for (const domain of m.domains) { manifestMatches.push(`*://*.${domain}/*`) } } + manifest['content_scripts']['matches'] = manifestMatches*/ - manifest['content_scripts']['matches'] = manifestMatches + manifest['content_scripts']['matches'] = [] - await fs.writeFileSync('src/manifest.json', JSON.stringify(manifest)) -}*/ + const matchesRegex = new RegExp(/export\s+const\s+matches\s+=\s+(?\[.*?])/gms) + const matchesClassesRegex = new RegExp(/new\s+(?\w+)\(\)/gms) + + const matchTs = fs.readFileSync('src/match/match.ts') + const jsMatches = matchesRegex.exec(matchTs).groups.matches + let m + while ((m = matchesClassesRegex.exec(jsMatches))) { + if (m.index === matchesClassesRegex.lastIndex) { + matchesClassesRegex.lastIndex++ + } + + if (m.groups.class !== undefined) { + const classDomainsRegex = new RegExp('class\\s+' + m.groups.class + '.*?domains\\s*=\\s*(?\\[.*?])', 'gms') + let mm + while ((mm = classDomainsRegex.exec(matchTs))) { + if (mm.index === classDomainsRegex.lastIndex) { + classDomainsRegex.lastIndex++ + } + + if (mm.groups.domains !== undefined) { + const matches = [] + for (const domain of JSON.parse(mm.groups.domains.replaceAll('\'', '"', -1))) { + matches.push(`*://*.${domain}/*`) + } + manifest['content_scripts'][0]['matches'] = manifest['content_scripts'][0]['matches'].concat(matches) + } + } + } + } + + await fs.writeFileSync('src/manifest.json', JSON.stringify(manifest, null, 2)) +} async function build_misc() { const files = { @@ -101,6 +134,7 @@ async function build_js() { } async function build() { + await build_manifest() await build_misc() await build_html() await build_css() diff --git a/tasks/bundle.ts b/tasks/bundle.ts index 08d8e21..61ceb7f 100644 --- a/tasks/bundle.ts +++ b/tasks/bundle.ts @@ -1,6 +1,6 @@ -const fs = require('fs') -const path = require('path') -const yazl = require('yazl') +import * as fs from "fs"; +import * as path from "path"; +import * as yazl from "yazl"; function walkDirectory(dir, callback) { for (const file of fs.readdirSync(dir)) {