Skip to content

Commit

Permalink
feat: 动态import可选依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Nov 20, 2023
1 parent f3d3b22 commit dc4b768
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
presets: [
[ '@babel/preset-env', {targets: {node: 'current'}} ],
[ '@babel/preset-typescript', {allExtensions: true}],
[ "@babel/preset-env", {targets: {node: "current"}} ],
[ "@babel/preset-typescript", {allExtensions: true}],
],
'compact': true,
"compact": true,
};
12 changes: 7 additions & 5 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nodejieba from "nodejieba";
import { load, cut /*, tag */ } from "@node-rs/jieba";
// import nodejieba from "nodejieba";
// import { load, cut /*, tag */ } from "@node-rs/jieba";
// @ts-ignore

Check warning on line 3 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Do not use "@ts-ignore" because it alters compilation errors

Check warning on line 3 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Do not use "@ts-ignore" because it alters compilation errors
import { Segment, useDefault } from "segmentit";
// import { Segment, useDefault } from "segmentit";
import type { IPinyinSegment } from "./declare";

let nodeRsJiebaLoaded = false; // @node-rs/jieba 加载词典。
Expand All @@ -15,6 +15,7 @@ let hansIntlSegmenter: any; // Intl.Segmenter
export function segment(hans: string, segment?: IPinyinSegment): string[] {
// @node-rs/jieba (Rust)
if (segment === "@node-rs/jieba") {
const { load, cut /*, tag */ } = require("@node-rs/jieba");

Check failure on line 18 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Require statement not part of import statement

Check failure on line 18 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Require statement not part of import statement
if (!nodeRsJiebaLoaded) {
nodeRsJiebaLoaded = true;
load();
Expand All @@ -25,6 +26,7 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {

// segmentit (Node.js)
if (segment === "segmentit") {
const { Segment, useDefault } = require("segmentit");

Check failure on line 29 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Require statement not part of import statement

Check failure on line 29 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Require statement not part of import statement
if (!segmentit) {
segmentit = useDefault(new Segment());
}
Expand All @@ -45,9 +47,9 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {
}
}

const nodejieba = require("nodejieba");

Check failure on line 50 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Require statement not part of import statement

Check failure on line 50 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Require statement not part of import statement
// 默认使用 nodejieba (C++)
// return nodejieba.tag(hans);
// nodejieba 定义的类型返回值错误,先忽略。
// @ts-ignore
return nodejieba.cutSmall(hans, 4);
return nodejieba.cutSmall(hans, 4) as unknown as string[];
}

0 comments on commit dc4b768

Please sign in to comment.