Debian Codegraph @

plugins/jieba/node/NPM_PACKAGE_DESIGN.md

raw symbols SCIP
1 # opencc-jieba Node.js Architecture & Engineering Design
2
3 > This document details the architectural improvements and engineering updates for the `opencc-jieba` Node.js package.
4
5 ## 1. Background and Objectives
6
7 Integrating the Jieba segmentation plugin into Node.js previously faced two major pain points:
8 1. **Environment Variable Dependency**: The C++ core library relied on environment variables (like `OPENCC_SEGMENTATION_PLUGIN_PATH` and `OPENCC_DATA_DIR`) to locate dynamic libraries and dictionary files. This approach is prone to global pollution and is unsuitable for an independent NPM module.
9 2. **Binary Artifact Management**: Committing large cross-platform build artifacts (`.dylib`, `.so`, `.dll`) and dictionary data directly into the Git repository causes uncontrollable repository bloat and severe merge conflicts during CI workflows and collaborative development. Publishing every platform binary in the main npm package also makes installs larger than necessary.
10
11 To resolve these issues, we refactored `opencc-jieba` into a **fully self-contained, deterministic, and artifact-free** modern NPM package.
12
13 ---
14
15 ## 2. Core Architectural Changes
16
17 ### 2.1 Deterministic Plugin Loading via Absolute Paths
18
19 **[C++ Core Updates: `src/PluginSegmentation.cpp`]**
20 To eliminate reliance on environment variables, we introduced a precise loading entry point in the C++ `PluginManager`: `LoadByPath(const std::string& path, ...)`.
21 Simultaneously, when parsing segmentation configurations, the core library now checks for a special parameter named `__plugin_library` within the config pairs:
22 ```cpp
23 // If __plugin_library is specified, bypass the traditional directory search strategy
24 std::shared_ptr<PluginLibrary> plugin =
25 pluginLibraryPath.empty()
26 ? GetPluginManager().LoadByType(type)
27 : GetPluginManager().LoadByPath(pluginLibraryPath, type);
28 ```
29
30 **[Node.js Integration]**
31 On the Node.js side, `plugins/jieba/node/index.js` now exports the exact absolute paths to the platform-specific dynamic library (`pluginLibrary`) and the data directory (`dataDir`). It first resolves the matching optional scoped package (`@opencc/opencc-jieba-<platform>-<arch>`), then falls back to the local `prebuilds/<platform>-<arch>/` directory for development builds.
32 When the upper-level Node.js wrapper (e.g., the `opencc` npm package) parses user-specified JSON configurations, it dynamically injects these **absolute paths** into the `__plugin_library` field. This guarantees 100% stable plugin discovery, completely unaffected by environment variables.
33
34 ### 2.2 Separation of Artifacts and Source Code
35
36 **[Cleaning up Git Tracking: `.gitignore`]**
37 By adding `prebuilds/` and `data/` to `plugins/jieba/node/.gitignore`, we completely stopped tracking external dictionary copies and compiled dynamic libraries in the code repository.
38
39 **[Automated Directory Tree Restoration: `build.js`]**
40 To ensure a seamless NPM publishing experience, we introduced `plugins/jieba/node/build.js`. Before packaging, this script automates the following pipeline:
41 1. **Triggering the Build System**: Executes `bazel build //plugins/jieba:opencc-jieba //plugins/jieba:jieba_merged_dict` to compile the native addon and convert dictionaries.
42 2. **Collecting Artifacts**:
43 - Extracts the generated dynamic libraries (`.dylib` / `.so` / `.dll`) into `prebuilds/<platform>-<arch>/`.
44 - Copies the upstream raw dictionaries and the compiled `.ocd2` binary dictionary into `data/jieba_dict/`.
45 - Copies segmentation-specific JSON configurations into `data/`.
46
47 **[Scoped Binary Package Preparation: `scripts/prepare-scoped-packages.js`]**
48 The main `opencc-jieba` package now ships JavaScript and data only. `scripts/prepare-scoped-packages.js` converts the local `prebuilds/<platform>-<arch>/` tree into publishable packages under `dist/scoped-packages/@opencc/opencc-jieba-<platform>-<arch>/`. These packages contain only `index.js`, `package.json`, and the native library for that platform.
49
50 **[Automated NPM Hooks: `package.json`]**
51 We configured `"prepack": "npm run build:all"` in the `scripts` section of `package.json`. This ensures that whether running `npm pack` locally or `npm publish` in CI, NPM will automatically invoke `build.js` to populate the latest artifact directories. Before publishing, maintainers can run `npm run prepare:scoped-packages` and publish each generated scoped package first; end users install only the platform package selected by npm optional dependency resolution.
52
53 ### 2.3 Highly Optimized Cross-Platform Compilation
54
55 Building upon this engineering foundation, we optimized the package size and distribution efficiency:
56 1. **Symbol Stripping**: Modified `plugins/jieba/BUILD.bazel` to explicitly add `-Wl,-S` (macOS) and `-Wl,-s` (Linux) linkopts. This ensures that C++ dynamic libraries in Release mode are stripped of redundant debugging symbols.
57 2. **Remote Cross-Compilation**: The `build.js` script now accepts CLI arguments to trigger remote compilation (e.g., `--config=linux-arm64-remote`). Coupled with the `--remote_download_toplevel` Bazel flag, developers can seamlessly trigger cross-platform builds via a remote execution cluster and accurately download the artifacts directly into the local packaging directory tree.
58
59 ---
60
61 ## 3. Conclusion
62
63 Through these refactoring efforts, the development and publishing workflow for `opencc-jieba` is now highly standardized. For contributors, the repository remains clean, and updating code or dictionaries requires only a single command to rebuild and repackage. For end users, the plugin provides a true "plug-and-play" experience, eliminating all elusive bugs caused by environment variable misconfigurations.