diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index d147cec6..1582f313 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -50,10 +50,6 @@ jobs: - run: pnpm install - run: pnpm run build - run: pnpm run vscode:build-tpl - - name: rename vscode vsx - # working-directory 用于指定在哪个目录下执行命令 - working-directory: ./packages/vscode - run: pnpm run build:rename # https://github.com/HaaLeo/publish-vscode-extension - name: build vscode vsx diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index ae462007..275e4c2f 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -50,10 +50,6 @@ jobs: - run: pnpm install - run: pnpm run build - run: pnpm run vscode:build-tpl - - name: rename vscode vsx - # working-directory 用于指定在哪个目录下执行命令 - working-directory: ./packages/vscode - run: pnpm run build:rename # https://github.com/HaaLeo/publish-vscode-extension - name: build vscode vsx diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a72f9831..dd281a6e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,12 +54,6 @@ jobs: if: matrix.os == 'windows-latest' run: pnpm run vscode:build-tpl - - name: rename vscode vsx - if: matrix.os == 'windows-latest' - # working-directory 用于指定在哪个目录下执行命令 - working-directory: ./packages/vscode - run: pnpm run build:rename - # https://github.com/HaaLeo/publish-vscode-extension - name: build vscode vsx if: matrix.os == 'windows-latest' diff --git a/package.json b/package.json index 0f3536bd..aa78c363 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "cross-env": "^7.0.3", "husky": "^8.0.0", "npm-run-all": "^4.1.5", + "tsup": "^8.1.2", "tsx": "^4.16.2", "unbuild": "^2.0.0" }, diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 7659185b..44228ada 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -5,8 +5,6 @@ "lib": ["esnext", "DOM"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true } diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index ce31835e..57fddb24 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -5,8 +5,6 @@ "lib": ["esnext", "DOM"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true } diff --git a/packages/epub/tsconfig.json b/packages/epub/tsconfig.json index c426229b..3fb21793 100644 --- a/packages/epub/tsconfig.json +++ b/packages/epub/tsconfig.json @@ -6,8 +6,6 @@ "lib": ["esnext", "DOM"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true, "paths": {} diff --git a/packages/rule-utils/tsconfig.json b/packages/rule-utils/tsconfig.json index ce31835e..57fddb24 100644 --- a/packages/rule-utils/tsconfig.json +++ b/packages/rule-utils/tsconfig.json @@ -5,8 +5,6 @@ "lib": ["esnext", "DOM"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true } diff --git a/packages/server/tsconfig.json b/packages/server/tsconfig.json index affa020d..662ad9d0 100644 --- a/packages/server/tsconfig.json +++ b/packages/server/tsconfig.json @@ -6,8 +6,6 @@ "lib": ["esnext", "DOM"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true } diff --git a/packages/shared/tsconfig.json b/packages/shared/tsconfig.json index 7c1df36f..29e952b3 100644 --- a/packages/shared/tsconfig.json +++ b/packages/shared/tsconfig.json @@ -1,17 +1,11 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - // https://github.com/typeorm/typeorm - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "target": "es2018", "module": "esnext", "lib": ["esnext"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true } diff --git a/packages/utools/tsconfig.json b/packages/utools/tsconfig.json index affa020d..662ad9d0 100644 --- a/packages/utools/tsconfig.json +++ b/packages/utools/tsconfig.json @@ -6,8 +6,6 @@ "lib": ["esnext", "DOM"], "moduleResolution": "node", "esModuleInterop": true, - "strict": true, - "strictNullChecks": true, "resolveJsonModule": true, "skipLibCheck": true } diff --git a/packages/vscode/.vscode/launch.json b/packages/vscode/.vscode/launch.json index cafb514b..6ed0940f 100644 --- a/packages/vscode/.vscode/launch.json +++ b/packages/vscode/.vscode/launch.json @@ -9,26 +9,9 @@ "name": "Run Extension", "type": "extensionHost", "request": "launch", - "args": [ - "--disable-extensions", - "--extensionDevelopmentPath=${workspaceFolder}" - ], + "args": ["--disable-extensions", "--extensionDevelopmentPath=${workspaceFolder}"], "outFiles": ["${workspaceFolder}/dist/**/*.js"], - "preLaunchTask": "${defaultBuildTask}" - }, - { - "name": "Extension Tests", - "type": "extensionHost", - "request": "launch", - "args": [ - "--extensionDevelopmentPath=${workspaceFolder}", - "--extensionTestsPath=${workspaceFolder}/out/test/suite/index" - ], - "outFiles": [ - "${workspaceFolder}/out/**/*.js", - "${workspaceFolder}/dist/**/*.js" - ], - "preLaunchTask": "tasks: watch-tests" + "preLaunchTask": "npm: dev" } ] } diff --git a/packages/vscode/.vscode/tasks.json b/packages/vscode/.vscode/tasks.json index bb452adf..b56d8996 100644 --- a/packages/vscode/.vscode/tasks.json +++ b/packages/vscode/.vscode/tasks.json @@ -5,33 +5,22 @@ "tasks": [ { "type": "npm", - "script": "watch", - "problemMatcher": ["$ts-webpack-watch", "$tslint-webpack-watch"], + "script": "dev", "isBackground": true, "presentation": { - "reveal": "never", - "group": "watchers" - }, - "group": { - "kind": "build", - "isDefault": true - } - }, - { - "type": "npm", - "script": "watch-tests", - "problemMatcher": "$tsc-watch", - "isBackground": true, - "presentation": { - "reveal": "never", - "group": "watchers" + "reveal": "never" }, + "problemMatcher": [ + { + "base": "$ts-webpack-watch", + "background": { + "activeOnStart": true, + "beginsPattern": "Build start", + "endsPattern": "Build success" + } + } + ], "group": "build" - }, - { - "label": "tasks: watch-tests", - "dependsOn": ["npm: watch", "npm: watch-tests"], - "problemMatcher": [] } ] } diff --git a/packages/vscode/.vscodeignore b/packages/vscode/.vscodeignore index c6136798..12440aa7 100644 --- a/packages/vscode/.vscodeignore +++ b/packages/vscode/.vscodeignore @@ -11,3 +11,4 @@ vsc-extension-quickstart.md **/.eslintrc.json **/*.map **/*.ts +**/*.cjs diff --git a/packages/vscode/package.json b/packages/vscode/package.json index 3e7a0824..e64366ca 100644 --- a/packages/vscode/package.json +++ b/packages/vscode/package.json @@ -35,18 +35,10 @@ ], "main": "./dist/extension.js", "scripts": { - "build:rename": "tsx ./scripts/publish.cjs", - "build:vsce": "vsce package --no-dependencies", - "build": "npm-run-all build:rename build:vsce", - "vscode:prepublish": "pnpm run package", - "compile": "webpack", - "watch": "webpack --watch", - "package": "webpack --mode production --devtool hidden-source-map", - "compile-tests": "tsc -p . --outDir out", - "watch-tests": "tsc -p . -w --outDir out", - "pretest": "pnpm run compile-tests && pnpm run compile && pnpm run lint", - "lint": "eslint src --ext ts", - "test": "node ./out/test/runTest.js" + "build": "tsup", + "dev": "tsx ./scripts/publish.cjs && tsup --watch src", + "vscode:prepublish": "tsx ./scripts/publish.cjs", + "pack": "vsce package --no-dependencies" }, "contributes": { "commands": [ diff --git a/packages/vscode/scripts/publish.cjs b/packages/vscode/scripts/publish.cjs index a7f0675d..d59b3598 100644 --- a/packages/vscode/scripts/publish.cjs +++ b/packages/vscode/scripts/publish.cjs @@ -1,8 +1,25 @@ const { dirname, join } = require('path'); const fs = require('fs-extra'); +const path = require('path'); const root = dirname(__dirname); +function parseVersion(version) { + const match = version.match(/^v?(\d{1,5})\.(\d{1,5})\.(\d{1,5})(?:-([0-9A-Za-z]+))?(?:\.([0-9A-Za-z-.]+))?$/); + if (!match) { + throw new Error('Unable to parse: ' + version); + } + var res = { + major: parseInt(match[1], 10), + minor: parseInt(match[2], 10), + patch: parseInt(match[3], 10), + pre: match[4] || '', + build: parseInt(match[5], 10) + }; + + return res; +} + async function publish() { const pkgPath = join(root, 'package.json'); const rawJSON = await fs.readFile(pkgPath, 'utf-8'); @@ -11,10 +28,13 @@ async function publish() { if (pkg.version.includes('-')) { // 非正式版 - const version = pkg.version.replace(/\-.*?$/, ''); - const patch = 10000 + version.split('.').pop(); - pkg.version = pkg.version.replace(/\.\d+\-.*?$/, '.' + patch); + const version = parseVersion(pkg.version); + pkg.version = pkg.version.replace(/\.\d+\-.*?$/, '.' + (10000 + version.build)); } + + // sql-wasm.wasm + fs.copyFileSync(path.resolve(root, '../../node_modules/sql.js/dist/sql-wasm.wasm'), path.join(root, 'dist', 'sql-wasm.wasm')); + pkg.devDependencies = {}; await fs.writeJSON(pkgPath, pkg, { spaces: 2 }); } diff --git a/packages/vscode/src/App.ts b/packages/vscode/src/App.ts index af04707a..c171fa4b 100644 --- a/packages/vscode/src/App.ts +++ b/packages/vscode/src/App.ts @@ -3,7 +3,7 @@ import { stringify } from 'qs'; import { ensureDirSync } from 'fs-extra'; import { openExplorer } from 'explorer-opener'; import { CONSTANTS } from '@any-reader/shared'; -import { COMMANDS, BOOK_SOURCE_PATH } from './constants'; +import { COMMANDS } from './constants'; import { webviewProvider } from './sidebar/webviewProvider'; import { WebView } from './webview'; import { getConfig } from './utils/config'; @@ -18,7 +18,6 @@ class App { // 注册命令 const registerCommand = vscode.commands.registerCommand; [ - registerCommand(COMMANDS.editBookSource, this.editBookSource, this), registerCommand(COMMANDS.searchBook, this.searchBook, this), registerCommand(COMMANDS.getChapter, this.getChapter, this), registerCommand(COMMANDS.discover, this.discover, this), @@ -34,13 +33,6 @@ class App { vscode.window.registerWebviewViewProvider('any-reader-webview', webviewProvider); } - // 书源编辑 - editBookSource() { - vscode.workspace.openTextDocument(vscode.Uri.file(BOOK_SOURCE_PATH)).then((doc) => { - vscode.window.showTextDocument(doc); - }); - } - // 搜索 async searchBook() { this.webView.navigateTo('/search'); diff --git a/packages/vscode/src/config.ts b/packages/vscode/src/config.ts deleted file mode 100644 index 4fa5d005..00000000 --- a/packages/vscode/src/config.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { workspace } from 'vscode'; - -export type ReadPageMode = 'Editor' | 'Sidebar'; - -function store() { - return workspace.getConfiguration('any-reader'); -} - -export class Config { - static get readPageMode(): ReadPageMode { - return store().get('read-page-mode') as ReadPageMode; - } - - set readPageMode(v: ReadPageMode) { - store().update('read-page-mode', v); - } -} diff --git a/packages/vscode/src/constants.ts b/packages/vscode/src/constants.ts index 828a825c..1287dea4 100644 --- a/packages/vscode/src/constants.ts +++ b/packages/vscode/src/constants.ts @@ -2,7 +2,6 @@ import * as os from 'os'; import * as path from 'path'; export enum COMMANDS { - editBookSource = 'any-reader.editBookSource', searchBook = 'any-reader.search', getChapter = 'any-reader.getChapter', discover = 'any-reader.discover', @@ -13,10 +12,4 @@ export enum COMMANDS { } export const ROOT_PATH = path.join(os.homedir(), '.any-reader'); - -// 规则路径 -export const BOOK_SOURCE_PATH = path.join(ROOT_PATH, 'book-source.json'); -// 历史记录路径 -export const HISTORY_PATH = path.join(ROOT_PATH, 'history.json'); -// 收藏配置路径 -export const FAVORITES_PATH = path.join(ROOT_PATH, 'favorites.json'); +export const CONFIG_PATH = path.join(ROOT_PATH, 'config.vscode.json'); diff --git a/packages/vscode/src/editorProvider/CustomEditorProvider.ts b/packages/vscode/src/editorProvider/CustomEditorProvider.ts index 532716ef..adaea4f8 100644 --- a/packages/vscode/src/editorProvider/CustomEditorProvider.ts +++ b/packages/vscode/src/editorProvider/CustomEditorProvider.ts @@ -25,8 +25,10 @@ export class CustomEditorProvider implements vscode.CustomReadonlyEditorProvider this.webviewPanel.webview.options = { enableScripts: true }; + // 绑定事件 useWebviewEvent(this.webviewPanel.webview, this._context.extensionPath); + // 打开模板 this.navigateTo( '/chapter?' + diff --git a/packages/vscode/src/sidebar/webviewProvider.ts b/packages/vscode/src/sidebar/webviewProvider.ts index 69809ae3..5aca501c 100644 --- a/packages/vscode/src/sidebar/webviewProvider.ts +++ b/packages/vscode/src/sidebar/webviewProvider.ts @@ -7,6 +7,7 @@ export class WebviewProvider implements vscode.WebviewViewProvider { private _webviewView!: vscode.WebviewView; private _extensionPath!: string; private _event!: WebviewEvent; + private route?: string; setExtensionPath(extensionPath: string) { this._extensionPath = extensionPath; @@ -17,8 +18,22 @@ export class WebviewProvider implements vscode.WebviewViewProvider { this.webview.options = { enableScripts: true }; + this.webview.html = getWebViewContent(path.join('template-dist', 'index.html'), this._extensionPath, this.webview); this._event = useWebviewEvent(this.webview, this._extensionPath); + + // 保存上下文 + this._event.pm.answer('post@vscode/saveRoute', async (data) => { + this.route = data.fullPath; + return true; + }); + + webviewView.onDidChangeVisibility(() => { + if (webviewView.visible && this.route) { + let injectScript = `window.__vscode$initialize_page = '${this.route}';`; + this.webview.html = getWebViewContent(path.join('template-dist', 'index.html'), this._extensionPath, this.webview, injectScript); + } + }); } get webviewView() { diff --git a/packages/vscode/src/utils/config.ts b/packages/vscode/src/utils/config.ts index 4e13b59f..b8fe8dcf 100644 --- a/packages/vscode/src/utils/config.ts +++ b/packages/vscode/src/utils/config.ts @@ -1,9 +1,7 @@ import * as os from 'node:os'; import * as path from 'node:path'; import * as fs from 'fs-extra'; - -export const ROOT_PATH = path.join(os.homedir(), '.any-reader'); -export const CONFIG_PATH = path.join(ROOT_PATH, 'config.vscode.json'); +import { CONFIG_PATH } from '../constants'; fs.ensureFileSync(CONFIG_PATH); diff --git a/packages/vscode/src/webview/WebviewEvent.ts b/packages/vscode/src/webview/WebviewEvent.ts index eaa46420..b8c80128 100644 --- a/packages/vscode/src/webview/WebviewEvent.ts +++ b/packages/vscode/src/webview/WebviewEvent.ts @@ -2,35 +2,28 @@ * webview 消息处理 */ -import * as os from 'node:os'; import * as path from 'node:path'; import * as vscode from 'vscode'; -import * as EasyPostMessage from 'easy-post-message'; +import EasyPostMessage from 'easy-post-message'; import { createApp } from '@any-reader/shared'; import { createAdapter } from '../utils/easyPostMessage'; -export const ROOT_PATH = path.join(os.homedir(), '.any-reader'); -export const CONFIG_PATH = path.join(ROOT_PATH, 'config.vscode.json'); +import { CONFIG_PATH } from '../constants'; export class WebviewEvent { - private _pm: any; + private _pm!: EasyPostMessage; get pm() { return this._pm; } constructor(webview: vscode.Webview, extensionPath: string) { - // @ts-ignore this._pm = new EasyPostMessage(createAdapter(webview)); - - // 消息通信, 为了以后兼容 XHR, 模板分离出独立可运行浏览器版本 - - // vsc this._pm.answer('post@vscode/executeCommand', this.executeCommand.bind(this)); - const sql = require('sql.js/dist/sql-wasm'); + createApp({ configPath: CONFIG_PATH, dataSourceOptions: { - driver: sql?.Module || sql, + driver: require('sql.js/dist/sql-wasm'), sqlJsConfig: { locateFile: (file: string) => { console.log('[locateFile]', path.join(extensionPath, 'dist', file)); diff --git a/packages/vscode/tsconfig.json b/packages/vscode/tsconfig.json index 019fa1f4..18ed4902 100644 --- a/packages/vscode/tsconfig.json +++ b/packages/vscode/tsconfig.json @@ -1,15 +1,10 @@ { + "extends": "../../tsconfig.json", "compilerOptions": { "forceConsistentCasingInFileNames": true, "module": "commonjs", - "target": "ES2018", - "lib": ["ES2020"], + "target": "ES2016", "sourceMap": true, - "rootDir": "src", - "strict": true /* enable all strict type-checking options */ - /* Additional Checks */ - // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ - // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ - // "noUnusedParameters": true, /* Report errors on unused parameters. */ + "strict": true } } diff --git a/packages/vscode/tsup.config.ts b/packages/vscode/tsup.config.ts new file mode 100644 index 00000000..2b58b0b0 --- /dev/null +++ b/packages/vscode/tsup.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'tsup'; + +export default defineConfig({ + entry: ['src/extension.ts'], + external: [ + 'vscode' + + // /original-fs/ + ], + format: 'cjs', + minify: true, + shims: true, + sourcemap: true +}); diff --git a/packages/vscode/webpack.config.js b/packages/vscode/webpack.config.js deleted file mode 100644 index 07b5dd84..00000000 --- a/packages/vscode/webpack.config.js +++ /dev/null @@ -1,87 +0,0 @@ -//@ts-check - -'use strict'; -const webpack = require('webpack'); -const FilterWarningsPlugin = require('webpack-filter-warnings-plugin'); -const path = require('path'); -const CopyPlugin = require('copy-webpack-plugin'); - -//@ts-check -/** @typedef {import('webpack').Configuration} WebpackConfig **/ - -/** @type WebpackConfig */ -const extensionConfig = { - target: 'node', // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/ - mode: 'none', // this leaves the source code as close as possible to the original (when packaging we set this to 'production') - - entry: './src/extension.ts', // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/ - output: { - // the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/ - path: path.resolve(__dirname, 'dist'), - filename: 'extension.js', - libraryTarget: 'commonjs2' - }, - ignoreWarnings: [ - { module: /node_modules\/typeorm\/util\/ImportUtils\.js/ }, - { - module: /node_modules\/typeorm\/util\/DirectoryExportedClassesLoader\.js/ - }, - { module: /node_modules\/typeorm\/platform\/PlatformTools\.js/ }, - { - module: /node_modules\/typeorm\/connection\/ConnectionOptionsReader\.js/ - } - ], - externals: { - sqlite3: 'commonjs sqlite3', - vscode: 'commonjs vscode' // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/ - // modules added here also need to be added in the .vscodeignore file - }, - resolve: { - // support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader - extensions: ['.ts', '.js', '.wasm'] - }, - experiments: { - asyncWebAssembly: true, - topLevelAwait: true - }, - module: { - noParse: /sql.js/, - rules: [{ test: /\.ts$/, exclude: /(node_modules|bin)/, use: ['ts-loader'] }] - }, - devtool: 'nosources-source-map', - plugins: [ - new CopyPlugin({ - patterns: [{ from: '../../node_modules/sql.js/dist/sql-wasm.wasm' }] - }), - - // @ts-ignore - new FilterWarningsPlugin({ - exclude: [ - /mongodb/, - /mssql/, - /mysql/, - /mysql2/, - /oracledb/, - /pg/, - /pg-native/, - /pg-query-stream/, - /react-native-sqlite-storage/, - /redis/, - /sqlite3/, - // /sql.js/, - /typeorm-aurora-data-api-driver/, - /hdb-pool/, - /spanner/, - /hana-client/, - - /original-fs/ - ] - }), - - new webpack.ContextReplacementPlugin(/keyv/) - ], - infrastructureLogging: { - level: 'log' // enables logging required for problem matchers - } -}; -module.exports = [extensionConfig]; diff --git a/packages/web/src/api/modules/vsc.ts b/packages/web/src/api/modules/vsc.ts index bbae94c3..16ee544c 100644 --- a/packages/web/src/api/modules/vsc.ts +++ b/packages/web/src/api/modules/vsc.ts @@ -11,3 +11,11 @@ export function executeCommand(data: any) { data }); } + +export function saveRoute(data: any) { + return request({ + method: 'post', + url: 'vscode/saveRoute', + data + }); +} diff --git a/packages/web/src/main.ts b/packages/web/src/main.ts index 57cdea22..6e98d231 100644 --- a/packages/web/src/main.ts +++ b/packages/web/src/main.ts @@ -10,7 +10,9 @@ import 'uno.css'; import './assets/main.scss'; // vscode ui import './plugins/vsc-ui'; +import { PLATFORM } from './constants'; import { addHistory } from '@/api'; +import { saveRoute } from '@/api/modules/vsc'; import { saveChapterHistory } from '@/api/modules/chapter-history'; import { createPinia } from 'pinia'; @@ -41,4 +43,8 @@ router.beforeEach((to, _from, next) => { } }); +router.afterEach((to) => { + if (PLATFORM === 'vscode') saveRoute({ fullPath: to.fullPath }); +}); + app.mount('#app'); diff --git a/packages/web/src/pages/vscode/rules/index.vsc.vue b/packages/web/src/pages/vscode/rules/index.vsc.vue index 261b0b39..cdb2699d 100644 --- a/packages/web/src/pages/vscode/rules/index.vsc.vue +++ b/packages/web/src/pages/vscode/rules/index.vsc.vue @@ -31,11 +31,11 @@