Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/rollup #384

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

----

## 4.0.0-alpha.0

- optionalPeerDependencies nodejieba, @node-rs/jieba, and segmentit.
- 打包为 esm 和 umd 格式。

## 3.1.0

- release 3.1.0, with output amd format.

## 3.0.0-alpha.7 (2023-11-20)

- feat: dynamic import optional dependencies.
Expand Down
34 changes: 27 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "pinyin",
"version": "3.1.0",
"version": "4.0.0-alpha.0",
"description": "汉语拼音转换工具。",
"main": "./lib/pinyin.js",
"module": "./esm/pinyin.js",
"main": "./dist/pinyin.js",
"module": "./dist/pinyin.mjs",
"browser": {
"./lib/pinyin.js": "./lib/pinyin-web.js",
"./esm/pinyin.js": "./esm/pinyin-web.js"
"./dist/pinyin.js": "./lib/pinyin-web.js",
"./dist/pinyin.mjs": "./esm/pinyin-web.js"
},
"bin": {
"pinyin": "./bin/pinyin"
Expand All @@ -23,7 +23,8 @@
"scripts": {
"start": "dumi dev",
"prepublishOnly": "npm run build",
"build": "tsc --downlevelIteration -p tsconfig-es5.json & tsc --downlevelIteration -p tsconfig.json & npm run build:amd",
"build": "rollup -c && rollup -c rollup.esm.config.js",
"build:tsc": "tsc --downlevelIteration -p tsconfig-es5.json & tsc --downlevelIteration -p tsconfig.json & npm run build:amd",
"build:amd": "tsc --downlevelIteration -p tsconfig-amd.json",
"doc:build": "rm -rf ./src/.umi && dumi build",
"doc:deploy": "npm run doc:build && cp CNAME docs-dist/dist && gh-pages -d docs-dist/dist",
Expand All @@ -34,14 +35,28 @@
"dependencies": {
"commander": "~1.1.1"
},
"optionalDependencies": {
"peerDependencies": {
"@node-rs/jieba": "^1.6.0",
"nodejieba": "2.5.2",
"segmentit": "^2.0.3"
},
"peerDependenciesMeta": {
"@node-rs/jieba": {
"optional": true
},
"nodejieba": {
"optional": true
},
"segmentit": {
"optional": true
}
},
"devDependencies": {
"@babel/preset-env": "^7.15.6",
"@babel/preset-typescript": "^7.15.0",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
Expand All @@ -62,6 +77,11 @@
"prettier": "^2.6.0",
"react-json-view": "^1.21.3",
"request": "~2.68.0",
"rollup": "2.60.0",
"rollup-plugin-alias": "^2.2.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.34.1",
"ts-node": "^10.5.0",
"typescript": "^4.4.4"
},
Expand Down
37 changes: 37 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from "path";
import json from "@rollup/plugin-json";
import { terser } from "rollup-plugin-terser";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs"; // commonjs模块转换插件
import cleanup from "rollup-plugin-cleanup";
import ts from "rollup-plugin-typescript2";
import alias from "rollup-plugin-alias";

const plugins = [
cleanup(),
json(),
nodeResolve(),
ts({
tsconfig: path.resolve(__dirname, "./tsconfig.json"), // 导入本地ts配置
clean: true,
useTsconfigDeclarationDir: true,
}),
commonjs(),
alias({
entries: [{ find: "@", replacement: "./src" }],
}),
terser(),
];

module.exports = {
input: path.resolve("./src/pinyin.ts"),
output: [
{
exports: "auto",
file: path.resolve(__dirname, "./dist/pinyin.js"),
format: "umd",
name: "pinyin",
},
],
plugins,
};
33 changes: 33 additions & 0 deletions rollup.esm.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from "path";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs"; // commonjs模块转换插件
import ts from "rollup-plugin-typescript2";
import alias from "rollup-plugin-alias";

const plugins = [
json(),
nodeResolve(),
ts({
tsconfig: path.resolve(__dirname, "./tsconfig.json"), // 导入本地ts配置
clean: true,
useTsconfigDeclarationDir: true,
}),
commonjs(),
alias({
entries: [{ find: "@", replacement: "./src" }],
}),
];

module.exports = {
input: path.resolve("./src/pinyin.ts"),
output: [
{
exports: "auto",
file: path.resolve(__dirname, "./dist/pinyin.mjs"),
format: "es",
sourcemap: false,
},
],
plugins,
};
12 changes: 12 additions & 0 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IPinyinSegment } from "./declare";

let nodeRsJiebaLoaded = false; // @node-rs/jieba 加载词典。
let segmentit: any; // segmentit 加载词典。

Check warning on line 4 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 4 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
let hansIntlSegmenter: any; // Intl.Segmenter

Check warning on line 5 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 5 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

/**
* TODO: 分词并带词性信息,需要调整 segment_pinyin 方法。
Expand All @@ -14,6 +14,10 @@
if (segment === "@node-rs/jieba") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { load, cut /*, tag */ } = require("@node-rs/jieba");
if (!load || !cut) {
console.error("pinyin v4: \"@node-rs/jieba\" is peerDependencies");
return [hans];
}
if (!nodeRsJiebaLoaded) {
nodeRsJiebaLoaded = true;
load();
Expand All @@ -26,6 +30,10 @@
if (segment === "segmentit") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Segment, useDefault } = require("segmentit");
if (!Segment) {
console.error("pinyin v4: \"segmentit\" is peerDependencies");
return [hans];
}
if (!segmentit) {
segmentit = useDefault(new Segment());
}
Expand All @@ -48,6 +56,10 @@

// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodejieba = require("nodejieba");
if (!nodejieba) {
console.error("pinyin v4: \"nodejieba\" is peerDependencies");
return [hans];
}
// 默认使用 nodejieba (C++)
// return nodejieba.tag(hans);
// nodejieba 定义的类型返回值错误,先忽略。
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"resolveJsonModule": true,
"declaration": true,
"outDir": "esm",
"module": "ES2015",
"target": "esnext",
"module": "esnext",
"target": "es6",
"lib": ["esnext"],
"sourceMap": true,
"baseUrl": "./src/",
Expand Down