LoginSignup
395
355

More than 5 years have passed since last update.

Yarnファーストインプレッション

Last updated at Posted at 2016-10-12

Yarn とは

名前から yet another ... な雰囲気を漂わせてますが、 npm互換 です。(追記: 正確にはnpmの生成するpackage.jsonと互換とのことだった)。各所から node連中はまたツール増やしやがって!という雰囲気を感じるので、ここは明確にした方がいい。(techcrunchの記事とかそういう印象を与える書き方になってる)

  • npm install 時のディレクトリ配置への介入
  • npm install 時のより賢いローカルキャッシュ
  • yarn.lock ファイルでバージョン固定

yarn 環境下で yarn add, yarn install などを行った場合、 yarn.lock と package.json に同時に書き込み、 その環境で生成されたファイルは yarn なしでも動きます。つまり、yarn はより厳密に npm のバージョンを固定したい人向け。

基本的な使い方

https://yarnpkg.com/en/docs/usage をみて

バージョンコンフリクト時の挙動

yarn.lock を手元で意図的にコンフリクトさせた場合、package.jsonの側に自動的に修正されました。ここはオプションで止めたり警告出したり出来たほうがよいと思う。

オプション

yarn -h みたら一発

  Usage: yarn install [flags]

  Options:

    -h, --help                  output usage information
    -V, --version               output the version number
    --offline                   
    --prefer-offline            
    --strict-semver             
    --json                      
    --global-folder [path]      
    --modules-folder [path]     rather than installing modules into the node_modules folder relative to the cwd, output them here
    --packages-root [path]      rather than storing modules into a global packages root, store them here
    --mutex [type][:specifier]  use a mutex to ensure only one yarn instance is executing
    --har                       save HAR output of network traffic
    --ignore-engines            ignore engines check
    --ignore-scripts            
    --ignore-optional           
    --force                     
    --flat                      only allow one version of a package
    --prod, --production        
    --no-lockfile               don't read or generate a lockfile 

--flat で複数 jQuery のimport を弾いたりするのに良さそう。

--ignore-optional はまさに自分が欲しかったものという感じ。Qiitaでもつい最近まで npm-shrinkwrap を使っていたのですが、docker環境に移行した際、環境ごとに変わる optional dependencies をshrinkwrap に含んで異なるOSでnpm install に失敗し爆死するということを npm がしでかして、まったく使えなくなり、破棄しました。yarn ならうまくいきそう。

--prefer-offline が速い!

npm install はサーバーへの整合性確認とかで毎回サーバーにリクエストしてたけど、プロジェクトの起動の前に yarn install --prefer-offline とか書いとくと便利そうだった。

で、使うの

現時点でとくに問題点は感じず、わかりやすいメリットもあるが、もうちょい様子見してから考える

395
355
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
395
355