Bootstrap

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 > [email protected]: Glob versions prior to v9 are no longer supported
warning @jest/globals > @jest/expect > jest-snapshot > @jest/transform > babel-plugin-istanbul > test-exclude > glob > [email protected]: 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 > [email protected]: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > jest-runtime > [email protected]: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > @jest/reporters > [email protected]: Glob versions prior to v9 are no longer supported
warning nyc > [email protected]: Glob versions prior to v9 are no longer supported
warning nyc > [email protected]: Rimraf versions prior to v4 are no longer supported
warning nyc > istanbul-lib-processinfo > [email protected]: Rimraf versions prior to v4 are no longer supported
warning nyc > spawn-wrap > [email protected]: Rimraf versions prior to v4 are no longer supported
warning nyc > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning shx > shelljs > [email protected]: 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
[email protected] | 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: false

       95 |       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.2

D:\Source\js-mdict-master> npm run build

> [email protected] build
> npm run build-cjs && npm run build-esm


> [email protected] build-cjs
> tsc --module commonjs --outDir dist/cjs/ && echo '{"type": "commonjs"}' > dist/cjs/package.json


> [email protected] 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解析器

;