Hướng dẫn sử dung mono repository với yarn workspaces

by Hieu Nguyen
1.4K views

Thông thường khi viết ứng dụng node, chúng ta thương sử dụng các thử viện có sẵn trên npmjs. Trong bài viết này tôi sẽ hướng dẫn các bạn tạo một dự án node có thể chia nhỏ thành các packages nhưng vẫn có thể viết trên cùng một repository. Yarn cung cấp cho bạn chức năng để tạo và link các packages với nhau bằng yarn workspaces. Chúng ta tìm hiểu yarn workspaces hoạt động ra sao nhé

Các bạn có thể tham khảo hướng dẫn cài đặt yarn ở đây

Tạo mới project

hieunv@HieuNV hieunv % mkdir mono
hieunv@HieuNV hieunv % cd mono
hieunv@HieuNV mono % yarn init
yarn init v1.22.0
question name (mono):
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
✨  Done in 5.46s.
hieunv@HieuNV mono %

Enable yarn workspaces

  • Thêm đoạn "workspaces": ["packages/*"] vào package.json

package.json

{
  "name": "mono",
  "workspaces": ["packages/*"],
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

Tiến hành tạo mono repository theo các bước sau:

  • Tạo 3 package trong thư mục packages như sau:
hieunv@HieuNV mono % find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
.
|____package.json
|____packages
| |____package-c
| | |____index.js
| | |____package.json
| |____package-b
| | |____index.js
| | |____package.json
| |____package-a
| | |____index.js
| | |____package.json
  • Tại thư mục package-apackage-b, thực hiện đăng ký package:

package-a

hieunv@HieuNV package-a % yarn link
yarn link v1.22.0
success Registered "package-a".
info You can now run `yarn link "package-a"` in the projects where you want to use this package and it will be used instead.
✨  Done in 0.04s.

package-b

hieunv@HieuNV package-b % yarn link
yarn link v1.22.0
success Registered "package-b".
info You can now run `yarn link "package-b"` in the projects where you want to use this package and it will be used instead.
✨  Done in 0.04s.
  • Thêm package-apackage-b như là dependency của package-c:
hieunv@HieuNV package-c % yarn link "package-a"
yarn link v1.22.0
success Using linked package for "package-a".
✨  Done in 0.04s.
hieunv@HieuNV package-c % yarn link "package-b"
yarn link v1.22.0
success Using linked package for "package-b".
✨  Done in 0.04s.
  • Trong package-a tạo các file sau:

package-a/package.json

{
  "type": "module",
  "name": "package-a",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

package-a/index.js

export function packageA() {
  console.log('Package A');
}
  • Trong của package-b tạo các file sau:

package-b/package.json

{
  "type": "module",
  "name": "package-b",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

package-b/index.js

export function packageB() {
  console.log('Package B');
}
  • Sử dụng các hàm được khai báo package-apackage-b

package-c/package.json

{
  "type": "module",
  "name": "package-c",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

package-c/index.js

import { packageA } from 'package-a';
import { packageB } from 'package-b';

packageA();
packageB();
  • Run package-c/index.js
hieunv@HieuNV package-c % node index.js
(node:10165) ExperimentalWarning: The ESM module loader is experimental.
Package A
Package B

Tài liệu tham khảo:

  • https://classic.yarnpkg.com/en/docs/workspaces/

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

You may also like