From c2c85581c411d0cb65c0da7b17b4f86112705253 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 1 Oct 2022 06:18:42 +0800 Subject: [PATCH] ui: seperate module build --- .gitignore | 1 + build/prepare.js | 63 +++++++++++++++------------ packages/hydrooj/bin/hydrooj.js | 14 +++++- packages/hydrooj/src/interface.ts | 2 +- packages/hydrooj/src/model/opcount.ts | 1 - packages/hydrooj/src/plugin-api.ts | 1 + packages/migrate/scripts/vijos.ts | 9 ++-- packages/sonic/service.ts | 2 +- 8 files changed, 57 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 7759c0f70..6d3111ac3 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ globalConfig.json /config.json yarn.lock tsconfig.json +tsconfig.*.json # Project Rules ## Core diff --git a/build/prepare.js b/build/prepare.js index 7a44a0e9c..e66345e68 100644 --- a/build/prepare.js +++ b/build/prepare.js @@ -20,7 +20,9 @@ const compilerOptionsBase = { const baseOutDir = path.resolve(__dirname, '../.cache/ts-out'); const config = { compilerOptions: compilerOptionsBase, - references: [], + references: [ + { path: 'tsconfig.ui.json' }, + ], files: [], }; const configSrc = (name) => ({ @@ -36,37 +38,14 @@ const configSrc = (name) => ({ 'dist', ], }); -const configFlat = (name) => (name === 'packages/ui-default' ? { - exclude: [ - './public', - ], - include: ['**/*.ts'], - compilerOptions: { - experimentalDecorators: true, - resolveJsonModule: true, - jsx: 'react', - module: 'commonjs', - skipLibCheck: true, - allowSyntheticDefaultImports: true, - target: 'es2020', - baseUrl: '.', - outDir: path.join(baseOutDir, name), - moduleResolution: 'node', - types, - paths: { - 'vj/*': [ - './*', - ], - }, - }, -} : { +const configFlat = (name) => ({ compilerOptions: { ...compilerOptionsBase, outDir: path.join(baseOutDir, name), rootDir: '.', }, include: ['**/*.ts'], - exclude: [], + exclude: ['public'], }); if (!fs.existsSync(path.resolve(process.cwd(), 'plugins'))) { @@ -77,7 +56,36 @@ const modules = [ 'packages/hydrooj', ...fs.readdirSync(path.resolve(process.cwd(), 'packages')).map((i) => `packages/${i}`), ...fs.readdirSync(path.resolve(process.cwd(), 'plugins')).map((i) => `plugins/${i}`), -].filter((i) => !i.includes('/.')).filter((i) => fs.statSync(path.resolve(process.cwd(), i)).isDirectory()); +].filter((i) => !i.includes('/.') && !i.includes('ui-default')).filter((i) => fs.statSync(path.resolve(process.cwd(), i)).isDirectory()); + +const UIConfig = { + exclude: [ + 'packages/ui-default/public', + ], + include: [ + 'packages/ui-default/**/*.ts', + 'packages/**/public/**/*.ts', + 'plugins/**/public/**/*.ts', + ], + compilerOptions: { + experimentalDecorators: true, + resolveJsonModule: true, + jsx: 'react', + module: 'commonjs', + skipLibCheck: true, + allowSyntheticDefaultImports: true, + target: 'es2020', + baseUrl: '.', + outDir: path.join(baseOutDir, 'ui'), + moduleResolution: 'node', + types, + paths: { + 'vj/*': [ + './packages/ui-default/*', + ], + }, + }, +}; for (const package of modules) { config.references.push({ path: package }); @@ -99,4 +107,5 @@ for (const package of modules) { } } } +fs.writeFileSync(path.resolve(process.cwd(), 'tsconfig.ui.json'), JSON.stringify(UIConfig)); fs.writeFileSync(path.resolve(process.cwd(), 'tsconfig.json'), JSON.stringify(config)); diff --git a/packages/hydrooj/bin/hydrooj.js b/packages/hydrooj/bin/hydrooj.js index 79c7966ab..49a6f2376 100755 --- a/packages/hydrooj/bin/hydrooj.js +++ b/packages/hydrooj/bin/hydrooj.js @@ -2,6 +2,18 @@ require('@hydrooj/utils/lib/register'); const { default: hook } = require('require-resolve-hook'); -const { bypass } = hook(/^(hydrooj|@hydrooj\/utils|cordis|schemastery|lodash|js-yaml)/, (id) => bypass(() => require.resolve(id))); +const { bypass } = hook(/^(hydrooj|@hydrooj\/utils|cordis|schemastery|lodash|js-yaml)/, (id) => { + if (id.startsWith('hydrooj/src')) { + console.log('module require via %s is deprecated.', id); + if (process.env.DEV) { + console.log( + new Error().stack.split('\n') + .filter((i) => !i.includes('node:internal') && i.startsWith(' ')) + .join('\n'), + ); + } + } + return bypass(() => require.resolve(id)); +}); require('./commands'); diff --git a/packages/hydrooj/src/interface.ts b/packages/hydrooj/src/interface.ts index b161c0072..f952dc06a 100644 --- a/packages/hydrooj/src/interface.ts +++ b/packages/hydrooj/src/interface.ts @@ -631,7 +631,7 @@ export interface Model { builtin: typeof import('./model/builtin'), contest: typeof import('./model/contest'), DiscussionModel: typeof import('./model/discussion'), - document: typeof import('./model/document'), + document: Omit, domain: typeof import('./model/domain').default, message: typeof import('./model/message').default, opcount: typeof import('./model/opcount'), diff --git a/packages/hydrooj/src/model/opcount.ts b/packages/hydrooj/src/model/opcount.ts index b8ad4dd4e..983e53d71 100644 --- a/packages/hydrooj/src/model/opcount.ts +++ b/packages/hydrooj/src/model/opcount.ts @@ -1,5 +1,4 @@ import { OpcountExceededError } from '../error'; -import * as bus from '../service/bus'; import db from '../service/db'; const coll = db.collection('opcount'); diff --git a/packages/hydrooj/src/plugin-api.ts b/packages/hydrooj/src/plugin-api.ts index a65c946d7..1e6307860 100644 --- a/packages/hydrooj/src/plugin-api.ts +++ b/packages/hydrooj/src/plugin-api.ts @@ -42,6 +42,7 @@ export { default as paginate } from './lib/paginate'; export * from './service/decorators'; export { Handler, ConnectionHandler } from './service/server'; export { UiContextBase } from './service/layers/base'; +export * as StorageService from './service/storage'; export { EventMap } from './service/bus'; export { db, Schema, yaml, fs, AdmZip, superagent, diff --git a/packages/migrate/scripts/vijos.ts b/packages/migrate/scripts/vijos.ts index af929459e..6f4c75acc 100644 --- a/packages/migrate/scripts/vijos.ts +++ b/packages/migrate/scripts/vijos.ts @@ -2,13 +2,12 @@ /* eslint-disable no-await-in-loop */ import mongodb, { Cursor, Db } from 'mongodb'; import { - DiscussionModel, DiscussionTailReplyDoc, MessageDoc, - RecordDoc, Schema, TestCase, TrainingNode, + db as dst, + DiscussionModel, DiscussionTailReplyDoc, DocumentModel, + MessageDoc, RecordDoc, Schema, TestCase, TrainingNode, } from 'hydrooj'; -const dst = global.Hydro.service.db; const map = {}; - const pid = (id) => { if (map[id.toString()]) return map[id.toString()]; return id; @@ -74,7 +73,7 @@ const tasks = { parent_doc_id: { field: 'parentId', processer: (parentId, doc) => { - if (doc.parent_doc_type === global.Hydro.model.document.TYPE_PROBLEM) { + if (doc.parent_doc_type === DocumentModel.TYPE_PROBLEM) { return pid(parentId); } return parentId; diff --git a/packages/sonic/service.ts b/packages/sonic/service.ts index 32cb9115a..18c0b7b87 100644 --- a/packages/sonic/service.ts +++ b/packages/sonic/service.ts @@ -13,7 +13,7 @@ declare module 'hydrooj' { 'sonic.auth': string; } interface Context { - sonic: SonicService; + sonic?: SonicService; } }