Bỏ qua, đến nội dung

@digiforce-nc/build

Package@digiforce-nc/build
Vai tròBuild tool dựa trên Rollup / RSBuild, orchestrate build toàn bộ monorepo Digiforce

Tổng quan

@digiforce-nc/build chịu trách nhiệm biên dịch tất cả package trong monorepo theo thứ tự topological - đảm bảo package phụ thuộc luôn được build trước package sử dụng nó.

CLI command

bash
# Build toàn bộ monorepo
bun digiforce-build

# Build một package cụ thể
bun digiforce-build --pkg @digiforce-nc/database

Lưu ý

Dùng bun digiforce-build thay vì bun digiforce-build - vì bun build là lệnh bundler riêng của Bun, cần bun run để chạy script từ package.json.

Lệnh digiforce-build là entry point chính, tự động phát hiện package và build theo đúng thứ tự phụ thuộc.

Build targets

TargetHàmMô tả
CJSbuildCjs()CommonJS output (dist/cjs/) - dùng cho Node.js runtime
ESMbuildEsm()ES Module output (dist/esm/) - dùng cho bundler và Node.js ESM
ClientbuildClient()Bundle client code (React components, UI) qua RSBuild
DeclarationbuildDeclaration()Generate .d.ts type declarations
PluginbuildPlugin()Build plugin hoàn chỉnh (server + client + types)

API chính

build(pkgs)

Build danh sách package cụ thể. Nhận mảng package descriptor và thực thi các build target tương ứng.

ts
import { build } from '@digiforce-nc/build';

await build([
  { name: '@digiforce-nc/database', dir: 'packages/core/database' },
  { name: '@digiforce-nc/server', dir: 'packages/core/server' },
]);

buildPackages()

Build tất cả package trong monorepo. Tự động gọi getPackages()groupPackagesByTopoLevel()build() theo từng level.

defineConfig / getUserConfig

Cho phép package tùy chỉnh cấu hình build riêng.

ts
// packages/my-plugin/build.config.ts
import { defineConfig } from '@digiforce-nc/build';

export default defineConfig({
  targets: ['cjs', 'esm', 'client'],
  externals: ['lodash'],
  alias: {
    '@shared': './src/shared',
  },
});

getUserConfig() đọc file build.config.ts trong thư mục package và merge với config mặc định.

Obfuscation

Hỗ trợ obfuscate code cho production build:

HàmMô tả
obfuscateFile(filePath)Obfuscate một file JavaScript
obfuscateFiles(dir)Obfuscate toàn bộ file JS trong thư mục

Obfuscation được áp dụng sau bước build, thường dùng cho plugin thương mại cần bảo vệ mã nguồn.

tarPlugin

tarPlugin() đóng gói plugin thành file .tar.gz để phân phối:

bash
# Kết quả: my-plugin-1.0.0.tgz

File tar bao gồm dist/, package.json, và các asset cần thiết - sẵn sàng install vào ứng dụng Digiforce.

Package discovery

HàmMô tả
getPackages()Quét monorepo, trả về danh sách tất cả package cần build
groupPackagesByTopoLevel()Nhóm package theo level phụ thuộc (level 0 = không phụ thuộc ai, level 1 = chỉ phụ thuộc level 0, …)

Cơ chế topo-level đảm bảo build song song tối đa trong cùng level mà không vi phạm thứ tự phụ thuộc.

Ví dụ: Custom build config

ts
// packages/plugins/my-plugin/build.config.ts
import { defineConfig } from '@digiforce-nc/build';

export default defineConfig({
  targets: ['cjs', 'esm', 'client'],
  client: {
    entry: './src/client/index.tsx',
  },
  externals: ['react', 'react-dom', 'antd'],
  afterBuild: async () => {
    console.log('Build xong!');
  },
});

TIP

Nếu không có build.config.ts, package sẽ dùng cấu hình mặc định - build cả CJS, ESM và declaration.