runsisi's

technical notes

yarn 本地仓库

2020-03-22 runsisi#web

g-f-w 一言难尽。

配置 npm 国内镜像仓库

$ yarn config set registry https://registry.npm.taobao.org --global
yarn config v1.22.4
success Set "registry" to "https://registry.npm.taobao.org".
Done in 0.04s.
$ yarn config set disturl https://npm.taobao.org/mirrors/node --global
yarn config v1.22.4
success Set "disturl" to "https://npm.taobao.org/mirrors/node".
Done in 0.03s.
$ yarn config get registry
https://registry.npm.taobao.org
$ yarn config get disturl
https://npm.taobao.org/mirrors/node

配置 node-sass 等使用国内镜像

$ vi .npmrc
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
electron_mirror=https://npm.taobao.org/mirrors/electron/
sharp_dist_base_url=https://npm.taobao.org/mirrors/sharp-libvips/v8.9.1/

使用 yalc 发布本地版本

安装 yalc:

$ sudo yarn global add yalc

从 github 克隆相应的 npm 包到本地,并使用 yalc 进行本地发布:

$ ls
cwebp-bin  mozjpeg-bin  pngquant-bin
$ cd cwebp-bin/
$ yalc publish
cwebp-bin@5.1.0-dce829b8 published in store.
$ cd ../mozjpeg-bin/
$ yalc publish
mozjpeg@6.0.1-10b1648e published in store.
$ cd ../pngquant-bin/
$ yalc publish
pngquant-bin@5.0.2-f80f7246 published in store.

yalc 实际上是将 npm 包拷贝到了 ~/.yalc 目录:

$ yalc dir
/home/runsisi/.yalc
$ tree -L 4 $(yalc dir)
/home/runsisi/.yalc
└── packages
    ├── cwebp-bin
    │   └── 5.1.0
    │       ├── cli.js
    │       ├── index.js
    │       ├── lib
    │       ├── license
    │       ├── package.json
    │       ├── readme.md
    │       └── yalc.sig
    ├── mozjpeg
    │   └── 6.0.1
    │       ├── cli.js
    │       ├── index.js
    │       ├── lib
    │       ├── license
    │       ├── package.json
    │       ├── readme.md
    │       └── yalc.sig
    └── pngquant-bin
        └── 5.0.2
            ├── cli.js
            ├── index.js
            ├── lib
            ├── license
            ├── package.json
            ├── readme.md
            ├── vendor
            └── yalc.sig

11 directories, 18 files

注意拷贝相应的可执行文件到 vendor 目录,否则仍然会连接 github 进行下载(阅读 lib 目录下的源码可知):

$ cd ..
$ mkdir ~/.yalc/packages/cwebp-bin/5.1.0/vendor
$ cp cwebp-bin/vendor/linux/x64/cwebp ~/.yalc/packages/cwebp-bin/5.1.0/vendor/
$ mkdir ~/.yalc/packages/mozjpeg/6.0.1/vendor
$ cp mozjpeg-bin/vendor/linux/cjpeg ~/.yalc/packages/mozjpeg/6.0.1/vendor/
$ cp pngquant-bin/vendor/linux/x64/pngquant ~/.yalc/packages/pngquant-bin/5.0.2/vendor/

在实际的工程中添加 yalc 发布的本地 npm 包:

$ cd runsisi.github.io/
$ yalc add cwebp-bin mozjpeg pngquant-bin
Package mozjpeg@6.0.1-10b1648e added ==> /home/runsisi/working/src/runsisi.github.io/node_modules/mozjpeg.
Package pngquant-bin@5.0.2-f80f7246 added ==> /home/runsisi/working/src/runsisi.github.io/node_modules/pngquant-bin.
Package cwebp-bin@5.1.0-dce829b8 added ==> /home/runsisi/working/src/runsisi.github.io/node_modules/cwebp-bin.
Don't forget you may need to run yarn after adding packages with yalc to install/update dependencies/bin scripts.
$ yalc installations show
Installations of package cwebp-bin:
  /home/runsisi/working/src/runsisi.github.io
Installations of package mozjpeg:
  /home/runsisi/working/src/runsisi.github.io
Installations of package pngquant-bin:
  /home/runsisi/working/src/runsisi.github.io
$ yalc check
Yalc dependencies found: [ 'cwebp-bin', 'mozjpeg', 'pngquant-bin' ]

查看 yalc 为工程添加的内容:

$ git st
On branch source
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   package.json

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .yalc/
        yalc.lock

no changes added to commit (use "git add" and/or "git commit -a")

$ tree -L 3 .yalc/
.yalc/
├── cwebp-bin
│   ├── cli.js
│   ├── index.js
│   ├── lib
│   │   ├── index.js
│   │   └── install.js
│   ├── license
│   ├── package.json
│   ├── readme.md
│   ├── vendor
│   │   └── cwebp
│   └── yalc.sig
├── mozjpeg
│   ├── cli.js
│   ├── index.js
│   ├── lib
│   │   ├── index.js
│   │   └── install.js
│   ├── license
│   ├── package.json
│   ├── readme.md
│   ├── vendor
│   │   └── cjpeg
│   └── yalc.sig
└── pngquant-bin
    ├── cli.js
    ├── index.js
    ├── lib
    │   ├── index.js
    │   └── install.js
    ├── license
    ├── package.json
    ├── readme.md
    ├── vendor
    │   ├── pngquant
    │   └── source
    └── yalc.sig

10 directories, 27 files

注意添加 yarn 的 resolutions 处理,让依赖也使用 yalc 本地发布的版本:

$ vi package.json
...
"resolutions": {
  "cwebp-bin": "file:.yalc/cwebp-bin",
  "mozjpeg": "file:.yalc/mozjpeg",
  "pngquant-bin": "file:.yalc/pngquant-bin"
},
...

$ yarn install
yarn install v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
...

需要移除 yalc 添加的 npm 包的话可以使用相应的 remove 命令:

$ yalc remove cwebp-bin mozjpeg pngquant-bin

或者手工移除相关的信息即可。

参考资料

淘宝 NPM 镜像

https://npm.taobao.org/mirrors

A lightweight private npm proxy registry

https://github.com/verdaccio/verdaccio

更改 yarn 仓库源

https://zju.date/yarn-registry/

support npm_config_sharp_dist_base_url

https://github.com/lovell/sharp/pull/1422

sharp Installation

https://sharp.pixelplumbing.com/install

Selective version resolution feature

https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-selective-versions-resolutions.md

“yarn knit”: a better “yarn link”

https://github.com/yarnpkg/yarn/issues/1213

Work with yarn/npm packages locally like a boss.

https://github.com/whitecolor/yalc

The solution for a working npm/yarn link

https://medium.com/@mtfranchetto/the-solution-for-a-working-npm-yarn-link-ddcb4f3c785e