← 返回主页

🔧 构建集成示例

展示 TextRank4ZH-TS 在不同构建系统中的使用方式

📦 构建产物说明

TextRank4ZH-TS 使用 unbuild 构建工具,生成多种格式以适应不同的使用场景和部署环境。

📄 index.mjs

ES Module 格式,适用于现代构建工具

~100KB

📄 index.cjs

CommonJS 格式,适用于 Node.js 环境

~105KB

📄 index.iife.js

IIFE 格式,可直接在浏览器中使用

~110KB

📄 index.d.ts

TypeScript 类型定义文件

~15KB

⚡ index.worker.js

DedicatedWorker 独立文件

~60KB

🔗 index.sharedworker.js

SharedWorker 独立文件

~65KB
🌐
IIFE 全局引入
最简单的浏览器使用方式,直接通过 script 标签引入,无需构建工具。

HTML 引入:

<script src="./dist/index.iife.js"></script> <script> const { TextRankKeyword } = window.TextRank4ZH; const tr4w = new TextRankKeyword(); // 使用库... </script>
📦
ES Module 导入
现代浏览器和构建工具的标准方式,支持 Tree Shaking 和静态分析。

模块导入:

import { TextRankKeyword, TextRankSentence } from 'textrank4zh-ts'; // 或从 CDN 导入 import { TextRankKeyword } from 'https://unpkg.com/textrank4zh-ts@latest/dist/index.mjs'; const tr4w = new TextRankKeyword(); const result = tr4w.analyze(text);

构建工具配置 (Vite/Webpack):

// package.json { "dependencies": { "textrank4zh-ts": "^0.1.0" } } // main.js import { TextRankKeyword } from 'textrank4zh-ts';
🟢
Node.js CommonJS
Node.js 环境的标准使用方式,支持 require 语法和 CommonJS 模块系统。

CommonJS 语法:

const { TextRankKeyword, TextRankSentence } = require('textrank4zh-ts'); const tr4w = new TextRankKeyword(); const result = tr4w.analyze('中文测试文本'); if (result.ok) { const keywords = tr4w.getKeywords(10); console.log('关键词:', keywords); }

package.json 配置:

{ "name": "my-project", "type": "commonjs", "dependencies": { "textrank4zh-ts": "^0.1.0" } }
🔗
CDN 直接使用
通过 CDN 直接使用,无需本地安装,适合快速原型开发和简单项目。

unpkg CDN:

<script type="module"> import { TextRankKeyword } from 'https://unpkg.com/textrank4zh-ts@latest/dist/index.mjs'; const tr4w = new TextRankKeyword(); // 使用库... </script>

jsDelivr CDN:

<script type="module"> import { TextRankKeyword } from 'https://cdn.jsdelivr.net/npm/textrank4zh-ts@latest/dist/index.mjs'; </script> <!-- IIFE 版本 --> <script src="https://cdn.jsdelivr.net/npm/textrank4zh-ts@latest/dist/index.iife.js"></script>

🚀 快速集成指南

根据您的项目环境选择合适的集成方式:

🌐 浏览器项目

使用 ES Module 或 IIFE 格式,支持现代浏览器 (ES2020+)

🟢 Node.js 项目

使用 CommonJS 或 ES Module 格式,支持 Node.js 16+

🔧 构建工具项目

支持 Webpack、Vite、Rollup 等主流构建工具

⚡ Worker 项目

使用独立的 Worker 文件,支持多线程处理