Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize codebase #307

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"plugin:jsonc/recommended-with-json"
],
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "script",
"ecmaVersion": 11,
"sourceType": "module",
"ecmaFeatures": {
"globalReturn": false,
"impliedStrict": true
Expand Down Expand Up @@ -50,6 +50,7 @@
"no-case-declarations": "error",
"no-const-assign": "error",
"no-constant-condition": "off",
"no-console": "warn",
"no-global-assign": "error",
"no-param-reassign": "off",
"no-prototype-builtins": "error",
Expand Down Expand Up @@ -250,6 +251,7 @@
"jsdoc/multiline-blocks": "error",
"jsdoc/no-bad-blocks": "error",
"jsdoc/no-multi-asterisks": "error",
"jsdoc/no-undefined-types": 1,
"jsdoc/require-asterisk-prefix": "error",
"jsdoc/require-hyphen-before-param-description": [
"error",
Expand Down Expand Up @@ -384,25 +386,7 @@
"ext/js/accessibility/google-docs.js",
"ext/js/**/sandbox/**/*.js"
],
"globals": {
"serializeError": "readonly",
"deserializeError": "readonly",
"isObject": "readonly",
"stringReverse": "readonly",
"promiseTimeout": "readonly",
"escapeRegExp": "readonly",
"deferPromise": "readonly",
"clone": "readonly",
"deepEqual": "readonly",
"generateId": "readonly",
"promiseAnimationFrame": "readonly",
"invokeMessageHandler": "readonly",
"log": "readonly",
"DynamicProperty": "readonly",
"EventDispatcher": "readonly",
"EventListenerCollection": "readonly",
"Logger": "readonly"
}
"globals": {}
},
{
"files": [
Expand Down Expand Up @@ -445,6 +429,14 @@
"webextensions": false
}
},
{
"files": [
"ext/js/language/dictionary-worker-main.js"
],
"parserOptions": {
"sourceType": "module"
}
},
{
"files": [
"playwright.config.js"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dictionaries/
/playwright/.cache/
/test/playwright/__screenshots__/
ext/manifest.json
ext/lib/
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"markdown.extension.toc.levels": "1..3",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.addMissingImports": true,
"source.organizeImports": true,
"source.fixAll.eslint": true,
},
"eslint.format.enable": true,
"playwright.env": {
"PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS": 1
}
},
"javascript.preferences.importModuleSpecifierEnding": "js",
}
60 changes: 23 additions & 37 deletions dev/build-libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,35 @@

const fs = require('fs');
const path = require('path');
const browserify = require('browserify');
const esbuild = require('esbuild');

async function buildParse5() {
const parse5Path = require.resolve('parse5');
const cwd = process.cwd();
try {
const baseDir = path.dirname(parse5Path);
process.chdir(baseDir); // This is necessary to ensure relative source map file names are consistent
return await new Promise((resolve, reject) => {
browserify({
entries: [parse5Path],
standalone: 'parse5',
debug: true,
baseDir
}).bundle((error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
} finally {
process.chdir(cwd);
}
}

function getBuildTargets() {
const extLibPath = path.join(__dirname, '..', 'ext', 'lib');
return [
{path: path.join(extLibPath, 'parse5.js'), build: buildParse5}
];
async function buildLib(p) {
await esbuild.build({
entryPoints: [p],
bundle: true,
minify: false,
sourcemap: true,
target: 'es2020',
format: 'esm',
outfile: path.join(__dirname, '..', 'ext', 'lib', path.basename(p)),
external: ['fs']
});
}

async function main() {
for (const {path: path2, build} of getBuildTargets()) {
const content = await build();
fs.writeFileSync(path2, content);
async function buildLibs() {
const devLibPath = path.join(__dirname, 'lib');
const files = await fs.promises.readdir(devLibPath, {
withFileTypes: true
});
for (const f of files) {
if (f.isFile()) {
await buildLib(path.join(devLibPath, f.name));
}
}
}

if (require.main === module) { main(); }
if (require.main === module) { buildLibs(); }

module.exports = {
getBuildTargets
buildLibs
};
18 changes: 17 additions & 1 deletion dev/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const childProcess = require('child_process');
const util = require('./util');
const {getAllFiles, getArgs, testMain} = util;
const {ManifestUtil} = require('./manifest-util');

const Ajv = require('ajv');
const standaloneCode = require('ajv/dist/standalone').default;
const buildLibs = require('./build-libs.js').buildLibs;

async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, onUpdate, dryRun) {
try {
Expand Down Expand Up @@ -130,6 +132,19 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath,
process.stdout.write(message);
};

process.stdout.write('Building schema validators using ajv\n');
const schemaDir = path.join(extDir, 'data/schemas/');
const schemaFileNames = fs.readdirSync(schemaDir);
const schemas = schemaFileNames.map((schemaFileName) => JSON.parse(fs.readFileSync(path.join(schemaDir, schemaFileName))));
const ajv = new Ajv({schemas: schemas, code: {source: true, esm: true}});
const moduleCode = standaloneCode(ajv);

// https://github.com/ajv-validator/ajv/issues/2209
const patchedModuleCode = moduleCode.replaceAll('require("ajv/dist/runtime/ucs2length").default', 'import("/lib/ucs2length.js").default');

fs.writeFileSync(path.join(extDir, 'lib/validate-schemas.js'), patchedModuleCode);


process.stdout.write(`Version: ${yomitanVersion}...\n`);

for (const variantName of variantNames) {
Expand Down Expand Up @@ -201,6 +216,7 @@ async function main(argv) {
const manifestPath = path.join(extDir, 'manifest.json');

try {
await buildLibs();
const variantNames = (
argv.length === 0 || args.get('all') ?
manifestUtil.getVariants().filter(({buildable}) => buildable !== false).map(({name}) => name) :
Expand Down
33 changes: 8 additions & 25 deletions dev/data/manifest-variants.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"default_popup": "action-popup.html"
},
"background": {
"service_worker": "sw.js"
"service_worker": "sw.js",
"type": "module"
},
"content_scripts": [
{
Expand All @@ -41,28 +42,7 @@
"match_about_blank": true,
"all_frames": true,
"js": [
"js/core.js",
"js/yomichan.js",
"js/app/frontend.js",
"js/app/popup.js",
"js/app/popup-factory.js",
"js/app/popup-proxy.js",
"js/app/popup-window.js",
"js/app/theme-controller.js",
"js/comm/api.js",
"js/comm/cross-frame-api.js",
"js/comm/frame-ancestry-handler.js",
"js/comm/frame-client.js",
"js/comm/frame-offset-forwarder.js",
"js/data/sandbox/string-util.js",
"js/dom/dom-text-scanner.js",
"js/dom/document-util.js",
"js/dom/text-source-element.js",
"js/dom/text-source-range.js",
"js/input/hotkey-handler.js",
"js/language/text-scanner.js",
"js/script/dynamic-loader.js",
"js/app/content-script-main.js"
"js/app/content-script-wrapper.js"
]
}
],
Expand Down Expand Up @@ -118,7 +98,8 @@
{
"resources": [
"popup.html",
"template-renderer.html"
"template-renderer.html",
"js/*"
],
"matches": [
"<all_urls>"
Expand Down Expand Up @@ -187,7 +168,9 @@
"path": [
"permissions"
],
"items": ["clipboardRead"]
"items": [
"clipboardRead"
]
}
]
},
Expand Down
17 changes: 17 additions & 0 deletions dev/lib/dexie-export-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2023 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export * from 'dexie-export-import';
17 changes: 17 additions & 0 deletions dev/lib/dexie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2023 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export * from 'dexie';
18 changes: 18 additions & 0 deletions dev/lib/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2023 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export {Handlebars} from './handlebars/src/handlebars.js';

29 changes: 29 additions & 0 deletions dev/lib/handlebars/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
The MIT License (MIT)

Copyright (c) Elasticsearch BV
Copyright (c) Copyright (C) 2011-2019 by Yehuda Katz

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This software consists of voluntary contributions made by many
individuals. For exact contribution history, see the revision history
available at the following locations:
- https://github.com/handlebars-lang/handlebars.js
- https://github.com/elastic/kibana/tree/main/packages/kbn-handlebars
Loading
Loading