欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > nodejs:js-mdict 的下载、安装、测试、build

nodejs:js-mdict 的下载、安装、测试、build

2025/2/5 12:44:35 来源:https://blog.csdn.net/belldeep/article/details/145398342  浏览:    关键词:nodejs:js-mdict 的下载、安装、测试、build

js-mdict 项目的目录结构:js-mdict 项目教程

js-mdict 下载地址:  js-mdict-master.zip 先解压到 D:\Source\

js-mdict 6.0.2 用了 ts (TypeScript) 和 Jest,增加了应用开发的难度,因为先要了解 ts 和 Jest。

 参阅:测试与开发:Jest测试框架介绍

Jest 是最流行的 JavaScript 测试框架之一。测试人员广泛使用 Jest 对 JavaScript 函数进行单元测试。Jest 确保 JavaScript 应用程序具有生成正确输出的工作代码。它也非常直观,因为 Jest 语法非常可读。在使用 Jest 测试框架时,我们使用其功能丰富的 API,该 API 快速且用户友好。 由于 Jest 是开源的,因此对测试框架有很好的社区支持。

Win 10 运行 cmd
nvm list available
nodejs 版本选择,请 visit https://nodejs.org/en/download/releases
v22.13.1 , v20.18.2 , v18.20.6 任选一个

D:\nvm>nvm install 18.20.6
Downloading node.js version 18.20.6 (64-bit)...
Extracting node and npm...
Complete
npm v10.8.2 installed successfully.

nvm list
nvm use 18.20.6
node -v
npm -v
where yarn
cd \Source\js-mdict-master
dir

D:\Source\js-mdict-master> yarn install
yarn install v1.22.19
info No lockfile found.
[1/5] Validating package.json...
[2/5] Resolving packages...
warning @jest/globals > @jest/expect > jest-snapshot > @jest/transform > babel-plugin-istanbul > test-exclude > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning @jest/globals > @jest/expect > jest-snapshot > @jest/transform > babel-plugin-istanbul > test-exclude > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning jest > @jest/core > jest-config > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > jest-runtime > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > @jest/reporters > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning nyc > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning nyc > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning nyc > istanbul-lib-processinfo > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning nyc > spawn-wrap > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning nyc > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning shx > shelljs > glob@7.2.3: Glob versions prior to v9 are no longer supported
[3/5] Fetching packages...
[4/5] Linking dependencies...
[5/5] Building fresh packages...
success Saved lockfile.
Done in 64.70s.

yarn list
输出清单太长
\Source\js-mdict-master\ 生成目录 node_modules 大约70MB

npm show jest
jest@29.7.0 | MIT | deps: 4 | versions: 354
Delightful JavaScript Testing.
https://jestjs.io/

D:\Source\js-mdict-master> where jest
jest -verbose
D:\Source\js-mdict-master> npm run test

Test Suites: 27 failed, 3 passed, 30 total
Tests:       28 passed, 28 total
Snapshots:   0 total
Time:        32.163 s
Ran all test suites.
失败率大约50%,是因为./test/data/ 缺少用来测试的字典文件。


修改 \Source\js-mdict-master\test\max-000-baseline.test.ts

describe('Mdict', () => {
  describe('#lookup', () => {
    const mdict = new MDX('./test/data/oald7.mdx', {
修改为:const mdict = new MDX('/js/testdict/oale8.mdx', {

npm test baseline

      87 |     it("should be 'bad'", () => {88 |       const def = mdict.lookup('bad');at Object.<anonymous> (test/max-000-baseline.test.ts:85:60)● Mdict › #lookup › should be 'bad'expect(received).toBeTruthy()Received: false95 |       expect(def.definition.length > 0).toBeTruthy();96 |       const expect_str = '<head><link rel="stylesheet" type="text/css" href="O7.css"/>';>  97 |       expect(def.definition.startsWith(expect_str.trim())).toBeTruthy();|                                                            ^98 |     });99 |   });100 | });at Object.<anonymous> (test/max-000-baseline.test.ts:97:60)Test Suites: 1 failed, 1 total
Tests:       7 failed, 7 total
Snapshots:   0 total
Time:        3.105 s, estimated 14 s
Ran all test suites matching /baseline/i.

还是 test 失败。
dir \js\testdict\oale8.mdx
文件路径没有问题。


npm how to run typescript ?
npx -h
npm help exec

npx tsx -h
Node.js runtime enhanced with esbuild for loading TypeScript & ESM

如何使用 npm 运行 TypeScript 文件

为了能够使用 npm 来运行 TypeScript 文件,项目需配置好必要的开发环境。这涉及到创建并编辑几个重要的文件。

创建 package.json 和 tsconfig.json 文件

首先,确保项目的根目录下存在 package.json 文件。此文件定义了项目的元数据以及脚本命令

接着,设置 tsconfig.json 文件来指定编译选项。该文件告诉 TypeScript 编译器如何处理源码中的各种特性:

{"compileOnSave": true,"compilerOptions": {"esModuleInterop": true,"target": "ES6","module": "CommonJS","strict": true,"noFallthroughCasesInSwitch": true,"noUnusedLocals": true,"skipLibCheck": true,"forceConsistentCasingInFileNames": true,"sourceMap": true,"outDir": "dist","lib": ["DOM", "ES2022"],"declaration": true,"paths": {"../src/*": ["./*"]}},"exclude": ["node_modules"],"include": ["src/**/*.ts", "tests/**/*.ts", "examples/**/*.ts"],
}

上述配置指定了目标 ECMAScript 版本、模块解析方式以及其他一些有用的标志位;同时设置了输出路径为 dist 并包含了所有位于 src/ 下的 .ts 文件作为输入。
打开 package.json 文件并向其中添加一个或多个方便调用的 script 脚本条目以便于后续快速启动编译流程:

"scripts": {"build": "tsc"
},

现在可以在终端里仅需输入简单的指令就可以触发整个工程下的所有 .ts 文件被转换成对应的 js 文件: npm run build 

D:\Source\js-mdict-master> npm -v
10.8.2D:\Source\js-mdict-master> npm run build> js-mdict@6.0.2 build
> npm run build-cjs && npm run build-esm> js-mdict@6.0.2 build-cjs
> tsc --module commonjs --outDir dist/cjs/ && echo '{"type": "commonjs"}' > dist/cjs/package.json> js-mdict@6.0.2 build-esm
> tsc  --module nodenext --moduleResolution nodenext --outDir dist/esm/ && echo '{"type": "module"}' > dist/esm/package.json

cd D:\Source\js-mdict-master
npx tsx ./example/oald7-example.ts
npx tsx ./example/oale8-mdd-example.ts

推荐开源项目:mdict-js - 纯JavaScript实现的MDict解析器

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com