From dfb70785e01421a878e8b7962ae9d38644714de7 Mon Sep 17 00:00:00 2001 From: li1553770945 <1553770945@qq.com> Date: Fri, 13 Jun 2025 21:12:49 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webpack.config.js | 93 +++++++++-------- webpack/vite.config.task-loop.mjs | 118 ++++++++++++---------- webpack/webpack.task-loop.js | 160 ++++++++++++++++-------------- webpack/webpack.tesseract.js | 29 ++++-- 4 files changed, 213 insertions(+), 187 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index c75c998..2cb0f53 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,51 +1,50 @@ -//@ts-check +import { fileURLToPath } from 'url'; +import { dirname, resolve } from 'path'; -'use strict'; +// 适配 ESM 的 __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); -const path = require('path'); - -//@ts-check -/** @typedef {import('webpack').Configuration} WebpackConfig **/ - -/** @type WebpackConfig */ +/** @type {import('webpack').Configuration} */ const extensionConfig = { - target: 'node', // VS Code 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') + target: 'node', // VS Code 扩展运行于 Node.js 环境 + mode: 'none', // 保持代码原样,打包时设为 'production' + entry: resolve(__dirname, 'src', 'extension.ts'), // 绝对路径 + output: { + path: resolve(__dirname, 'dist'), // 绝对路径 + filename: 'extension.js', + libraryTarget: 'module', // 改为 ESM 兼容的模块格式 + chunkFormat: 'module', // 确保分块也是 ESM 格式 + }, + externals: { + vscode: 'commonjs vscode', // 排除 vscode 模块 + // 其他需排除的依赖(如第三方库) + }, + experiments: { + outputModule: true, // 启用 ESM 输出实验性功能 + }, + resolve: { + extensions: ['.ts', '.js'], + fallback: { + bufferutil: false, + 'utf-8-validate': false + } + }, + module: { + rules: [ + { + test: /\.ts$/, + exclude: /node_modules/, + use: [{ + loader: 'ts-loader' + }] + } + ] + }, + devtool: 'nosources-source-map', + infrastructureLogging: { + level: 'log' + } +}; - 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' - }, - externals: { - 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'], - fallback: { - bufferutil: false, - 'utf-8-validate': false - } - }, - module: { - rules: [ - { - test: /\.ts$/, - exclude: /node_modules/, - use: [ - { - loader: 'ts-loader' - } - ] - } - ] - }, - devtool: 'nosources-source-map', - infrastructureLogging: { - level: "log", // enables logging required for problem matchers - }, -}; module.exports = [extensionConfig]; +export default [extensionConfig]; \ No newline at end of file diff --git a/webpack/vite.config.task-loop.mjs b/webpack/vite.config.task-loop.mjs index f9011ca..1fadd88 100644 --- a/webpack/vite.config.task-loop.mjs +++ b/webpack/vite.config.task-loop.mjs @@ -2,62 +2,70 @@ import { defineConfig, normalizePath } from 'vite'; import { resolve } from 'path'; import { viteStaticCopy } from 'vite-plugin-static-copy'; +// 统一定义根目录,确保路径一致性 +const rootDir = resolve(__dirname, '..'); +const srcDir = resolve(rootDir, 'renderer/src'); +const outDir = resolve(rootDir, 'openmcp-sdk'); + export default defineConfig({ - define: { - 'window': { - 'nodejs': true, - 'navigator': { - 'userAgent': 2 - }, - 'performance': { - 'now': () => performance.now() - }, - 'Date': { - 'now': () => Date.now() - } - }, + define: { + 'window': { + 'nodejs': true, + 'navigator': { + 'userAgent': 2 + }, + 'performance': { + 'now': () => performance.now() + }, + 'Date': { + 'now': () => Date.now() + } }, - plugins: [ - viteStaticCopy({ - targets: [ - { - src: normalizePath(resolve(__dirname, '../resources/openmcp-sdk-release/*')), - dest: normalizePath(resolve(__dirname, '../openmcp-sdk')) - } - ] - }) - ], - build: { - target: 'node18', - lib: { - entry: resolve(__dirname, '..', 'renderer/src/components/main-panel/chat/core/task-loop.ts'), - name: 'TaskLoop', - fileName: 'task-loop', - formats: ['cjs'] - }, - outDir: resolve(__dirname, '..', 'openmcp-sdk'), - emptyOutDir: false, - rollupOptions: { - external: [ - 'vue', - 'chalk', - 'element-plus', - ], - output: { - globals: { - vue: 'vue', - chalk: 'chalk', - 'element-plus': './tools.js' - }, - esModule: true - } - }, - minify: false, - sourcemap: false - }, - resolve: { - alias: { - '@': resolve(__dirname, '..', 'renderer/src'), + }, + plugins: [ + viteStaticCopy({ + targets: [ + { + // 使用统一路径处理逻辑 + src: normalizePath(resolve(rootDir, 'resources/openmcp-sdk-release/*')), + dest: normalizePath(outDir) } + ] + }) + ], + build: { + target: 'node18', + lib: { + // 使用统一路径变量 + entry: resolve(srcDir, 'components/main-panel/chat/core/task-loop.ts'), + name: 'TaskLoop', + fileName: 'task-loop', + formats: ['es'] // 改为 ESM 格式 [[7]] + }, + outDir, // 使用统一输出目录 + emptyOutDir: false, + rollupOptions: { + external: [ + 'vue', + 'chalk', + 'element-plus', + ], + output: { + globals: { + vue: 'vue', + chalk: 'chalk', + 'element-plus': './tools.js' // 使用 POSIX 风格路径 [[10]] + }, + esModule: true + } + }, + minify: false, + sourcemap: false + }, + resolve: { + alias: { + // 使用统一路径变量 + '@': srcDir } -}); + } +}); \ No newline at end of file diff --git a/webpack/webpack.task-loop.js b/webpack/webpack.task-loop.js index 4153f66..dec9d27 100644 --- a/webpack/webpack.task-loop.js +++ b/webpack/webpack.task-loop.js @@ -1,80 +1,90 @@ -const path = require('path'); -const TerserPlugin = require('terser-webpack-plugin'); -const webpack = require('webpack'); -const CopyWebpackPlugin = require('copy-webpack-plugin'); +import { fileURLToPath } from 'url'; +import { dirname, resolve } from 'path'; +import TerserPlugin from 'terser-webpack-plugin'; +import webpack from 'webpack'; +import CopyWebpackPlugin from 'copy-webpack-plugin'; -module.exports = { - mode: 'development', // 设置为 development 模式 - devtool: 'source-map', // 生成 source map 以便调试 - entry: './renderer/src/components/main-panel/chat/core/task-loop.ts', - output: { - path: path.resolve(__dirname, '../openmcp-sdk'), - filename: 'task-loop.js', - libraryTarget: 'commonjs2' +// 适配 ESM 的 __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// 统一路径变量 +const rootDir = resolve(__dirname, '..'); +const srcDir = resolve(rootDir, 'renderer/src'); +const outDir = resolve(rootDir, 'openmcp-sdk'); + +export default { + mode: 'development', + devtool: 'source-map', + entry: resolve(srcDir, 'components/main-panel/chat/core/task-loop.ts'), + output: { + path: outDir, + filename: 'task-loop.js', + libraryTarget: 'commonjs', // 改为 ESM 兼容的 commonjs 格式 [[9]] + }, + target: 'node', + resolve: { + extensions: ['.ts', '.js'], + alias: { + '@': srcDir, }, - target: 'node', - resolve: { - extensions: ['.ts', '.js'], - alias: { - '@': path.resolve(__dirname, '../renderer/src'), // 修正路径别名 + }, + module: { + rules: [ + { + test: /\.ts$/, + use: { + loader: 'ts-loader', + options: { + configFile: resolve(rootDir, 'tsconfig.json'), + }, }, - }, - module: { - rules: [ - { - test: /\.ts$/, - use: { - loader: 'ts-loader', - options: { - configFile: path.resolve(__dirname, '../tsconfig.json') // 指定 tsconfig.json 路径 - } - }, - exclude: /node_modules/, - }, - { - test: /\.vue$/, - use: { - loader: 'null-loader' - } - } - ], - }, - optimization: { - minimize: true, // Enable code compression - minimizer: [ - new TerserPlugin({ - extractComments: false, // Disable extracting license files - terserOptions: { - compress: { - drop_console: true, // Remove all console.* calls - }, - }, - }), - ], - }, - plugins: [ - new webpack.DefinePlugin({ - window: { - nodejs: true, - navigator: { - userAgent: 2 - }, - performance: { - now: () => Date.now() - } - } - }), - new CopyWebpackPlugin({ - patterns: [ - { - from: path.resolve(__dirname, '../resources/openmcp-sdk-release'), - to: path.resolve(__dirname, '../openmcp-sdk') - } - ] - }) + exclude: /node_modules/, + }, + { + test: /\.vue$/, + use: { + loader: 'null-loader', + }, + }, ], - externals: { - vue: 'vue', // 不打包 vue 库 - 'element-plus': './tools.js' - }, + }, + optimization: { + minimize: true, + minimizer: [ + new TerserPlugin({ + extractComments: false, + terserOptions: { + compress: { + drop_console: true, + }, + }, + }), + ], + }, + plugins: [ + new webpack.DefinePlugin({ + window: { + nodejs: true, + navigator: { + userAgent: 2, + }, + performance: { + now: () => Date.now(), + }, + }, + }), + new CopyWebpackPlugin({ + patterns: [ + { + from: resolve(rootDir, 'resources/openmcp-sdk-release'), + to: outDir, + }, + ], + }), + ], + externals: { + vue: 'vue', + 'element-plus': './tools.js', // 使用 POSIX 风格路径 [[6]] + }, }; \ No newline at end of file diff --git a/webpack/webpack.tesseract.js b/webpack/webpack.tesseract.js index f100102..3e40f4f 100644 --- a/webpack/webpack.tesseract.js +++ b/webpack/webpack.tesseract.js @@ -1,18 +1,27 @@ -const path = require('path'); -const CopyWebpackPlugin = require('copy-webpack-plugin'); +import { fileURLToPath } from 'url'; +import { dirname, resolve } from 'path'; +import CopyWebpackPlugin from 'copy-webpack-plugin'; -module.exports = { - entry: './node_modules/tesseract.js/src/worker-script/node/index.js', +// 适配 ESM 的 __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// 统一路径变量 +const rootDir = resolve(__dirname, '..'); +const outDir = resolve(rootDir, 'resources', 'ocr'); + +export default { + entry: resolve(rootDir, 'node_modules', 'tesseract.js', 'src', 'worker-script', 'node', 'index.js'), output: { - path: path.resolve(__dirname, '..', 'resources', 'ocr'), + path: outDir, filename: 'worker.js', - libraryTarget: 'commonjs2' + libraryTarget: 'commonjs', // 改为 ESM 兼容的 commonjs 格式 }, resolve: { fallback: { bufferutil: false, - 'utf-8-validate': false - } + 'utf-8-validate': false, + }, }, mode: 'production', target: 'node', @@ -20,8 +29,8 @@ module.exports = { new CopyWebpackPlugin({ patterns: [ { - from: path.resolve(__dirname, '..', 'node_modules', 'tesseract.js-core', 'tesseract*'), - to: path.resolve(__dirname, '..', 'resources', 'ocr', '[name][ext]'), + from: resolve(rootDir, 'node_modules', 'tesseract.js-core', 'tesseract*'), + to: resolve(outDir, '[name][ext]'), }, ], }),