Skip to content

Commit

Permalink
Switch to vitest for ESM support; other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
djahandarie committed Nov 7, 2023
1 parent ef79eab commit 7d2e9e6
Show file tree
Hide file tree
Showing 69 changed files with 14,701 additions and 11,491 deletions.
121 changes: 45 additions & 76 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"plugin:jsonc/recommended-with-json"
],
"parserOptions": {
"ecmaVersion": 11,
"ecmaVersion": 2022,
"sourceType": "module",
"ecmaFeatures": {
"globalReturn": false,
Expand All @@ -14,7 +14,7 @@
},
"env": {
"browser": true,
"es2018": true,
"es2022": true,
"webextensions": true
},
"plugins": [
Expand All @@ -24,7 +24,8 @@
"jsonc"
],
"ignorePatterns": [
"/ext/lib/"
"/ext/lib/",
"/dev/lib/handlebars/"
],
"rules": {
"arrow-parens": [
Expand Down Expand Up @@ -181,6 +182,7 @@
"after": true
}
],
"no-implicit-globals": "error",
"no-trailing-spaces": "error",
"no-whitespace-before-property": "error",
"object-curly-spacing": [
Expand Down Expand Up @@ -377,31 +379,6 @@
"webextensions": false
}
},
{
"files": [
"ext/**/*.js"
],
"excludedFiles": [
"ext/js/core.js",
"ext/js/accessibility/google-docs.js",
"ext/js/**/sandbox/**/*.js"
],
"globals": {}
},
{
"files": [
"ext/**/*.js"
],
"excludedFiles": [
"ext/js/core.js",
"ext/js/accessibility/google-docs.js",
"ext/js/yomichan.js",
"ext/js/**/sandbox/**/*.js"
],
"globals": {
"yomichan": "readonly"
}
},
{
"files": [
"ext/js/yomichan.js"
Expand All @@ -415,57 +392,26 @@
"test/**/*.js",
"dev/**/*.js"
],
"excludedFiles": [
"test/data/html/*.js"
],
"parserOptions": {
"ecmaVersion": 8,
"sourceType": "module"
},
"env": {
"browser": false,
"es2017": true,
"node": true,
"webextensions": false
}
},
{
"files": [
"ext/js/language/dictionary-worker-main.js"
"test/data/html/*.js"
],
"parserOptions": {
"sourceType": "module"
}
},
{
"files": [
"playwright.config.js"
],
"env": {
"browser": false,
"es2017": true,
"node": true,
"webextensions": false
"sourceType": "script"
},
"rules": {
"no-undefined": "off"
}
},
{
"files": [
"integration.spec.js",
"playwright-util.js",
"visual.spec.js"
],
"env": {
"browser": false,
"es2017": true,
"node": true,
"browser": true,
"node": false,
"webextensions": false
},
"rules": {
"no-undefined": "off",
"no-empty-pattern": "off"
"no-implicit-globals": "off"
}
},
{
Expand Down Expand Up @@ -506,13 +452,13 @@
"env": {
"browser": false,
"serviceworker": true,
"es2017": true,
"webextensions": true
},
"globals": {
"FileReader": "readonly",
"Intl": "readonly",
"crypto": "readonly"
"crypto": "readonly",
"AbortController": "readonly"
}
},
{
Expand All @@ -530,29 +476,52 @@
"env": {
"browser": false,
"worker": true,
"es2017": true,
"webextensions": true
}
},
{
"files": [
"ext/js/**/*.js"
],
"excludedFiles": [
"ext/js/core.js",
"ext/js/**/*main.js"
"playwright.config.js"
],
"env": {
"browser": false,
"node": true,
"webextensions": false
},
"rules": {
"no-implicit-globals": "error"
"no-undefined": "off"
}
},
{
"files": [
"ext/js/**/*.js"
"integration.spec.js",
"playwright-util.js",
"visual.spec.js"
],
"globals": {
"AbortController": "readonly"
"env": {
"browser": false,
"node": true,
"webextensions": false
},
"rules": {
"no-undefined": "off",
"no-empty-pattern": "off"
}
},
{
"files": [
"test/**"
],
"plugins": [
"vitest"
],
"extends": [
"plugin:vitest/recommended"
],
"rules": {
"vitest/prefer-to-be": "off"
},
"env": {}
}
]
}
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Lint
run: npm run test-lint
- name: Lint JS
run: npm run test-lint-js
env:
CI: true

Expand Down
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
.DS_Store

node_modules/

builds/
.DS_Store

dictionaries/

/test-results/
/playwright-report/
/playwright/.cache/
/test/playwright/__screenshots__/

ext/manifest.json
ext/lib/

ext/lib/*
!ext/lib/__mocks__/
21 changes: 21 additions & 0 deletions dev/bin/build-libs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2023 Yomitan Authors
* Copyright (C) 2020-2022 Yomichan 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/>.
*/

import {buildLibs} from '../build-libs.js';

buildLibs();
42 changes: 18 additions & 24 deletions dev/build.js → dev/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const readline = require('readline');
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;
import Ajv from 'ajv';
import standaloneCode from 'ajv/dist/standalone/index.js';
import assert from 'assert';
import childProcess from 'child_process';
import fs from 'fs';
import path from 'path';
import readline from 'readline';
import {fileURLToPath} from 'url';
import {buildLibs} from '../build-libs.js';
import {ManifestUtil} from '../manifest-util.js';
import {getAllFiles, getArgs, testMain} from '../util.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));

async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, onUpdate, dryRun) {
try {
Expand Down Expand Up @@ -61,7 +63,7 @@ async function createZip(directory, excludeFiles, outputFileName, sevenZipExes,
}

async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun) {
const JSZip = util.JSZip;
const JSZip = null;
const files = getAllFiles(directory);
removeItemsFromArray(files, excludeFiles);
const zip = new JSZip();
Expand Down Expand Up @@ -140,7 +142,7 @@ async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath,
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');
const patchedModuleCode = "import {ucs2length} from './ucs2length.js';" + moduleCode.replaceAll('require("ajv/dist/runtime/ucs2length").default', 'ucs2length');

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

Expand Down Expand Up @@ -193,7 +195,7 @@ function ensureFilesExist(directory, files) {
}


async function main(argv) {
export async function main(argv) {
const args = getArgs(argv, new Map([
['all', false],
['default', false],
Expand All @@ -210,7 +212,7 @@ async function main(argv) {

const manifestUtil = new ManifestUtil();

const rootDir = path.join(__dirname, '..');
const rootDir = path.join(dirname, '..', '..');
const extDir = path.join(rootDir, 'ext');
const buildDir = path.join(rootDir, 'builds');
const manifestPath = path.join(extDir, 'manifest.json');
Expand All @@ -234,12 +236,4 @@ async function main(argv) {
}
}


if (require.main === module) {
testMain(main, process.argv.slice(2));
}


module.exports = {
main
};
testMain(main, process.argv.slice(2));
36 changes: 17 additions & 19 deletions test/test-build-libs.js → dev/bin/dictionary-validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2023 Yomitan Authors
* Copyright (C) 2022 Yomichan Authors
* Copyright (C) 2020-2022 Yomichan 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
Expand All @@ -16,27 +16,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

const fs = require('fs');
const assert = require('assert');
const {getBuildTargets} = require('../dev/build-libs');
import {testDictionaryFiles} from '../dictionary-validate.js';

async function main() {
try {
for (const {path: path2, build} of getBuildTargets()) {
let expectedContent = await build();
if (typeof expectedContent !== 'string') {
// Buffer
expectedContent = expectedContent.toString('utf8');
}
const actualContent = fs.readFileSync(path2, {encoding: 'utf8'});
assert.strictEqual(actualContent, expectedContent);
}
} catch (e) {
console.error(e);
process.exit(-1);
const dictionaryFileNames = process.argv.slice(2);
if (dictionaryFileNames.length === 0) {
console.log([

Check warning on line 24 in dev/bin/dictionary-validate.js

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement

Check warning on line 24 in dev/bin/dictionary-validate.js

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement
'Usage:',
' node dictionary-validate [--ajv] <dictionary-file-names>...'
].join('\n'));
return;
}
process.exit(0);

let mode = null;
if (dictionaryFileNames[0] === '--ajv') {
mode = 'ajv';
dictionaryFileNames.splice(0, 1);
}

await testDictionaryFiles(mode, dictionaryFileNames);
}

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

0 comments on commit 7d2e9e6

Please sign in to comment.