Skip to content

Commit 3af60cf

Browse files
authored
Use standalone .js bundle in dist tarball rather than individual JS files (#3030)
Instead of including all the raw JS files in the dist tarball, just use the single Yarn JS file that's built as part of the build, along with a few other files that are required. This significantly reduces the number of files in the tarball: ``` C:\src\yarn\dist (bundle-as-dist) ([email protected]) λ find . . ./bin ./bin/node-gyp-bin ./bin/node-gyp-bin/node-gyp ./bin/node-gyp-bin/node-gyp.cmd ./bin/yarn ./bin/yarn.cmd ./bin/yarn.js ./bin/yarnpkg ./bin/yarnpkg.cmd ./lib ./lib/v8-compile-cache.js ./lib/yarn-cli.js ./LICENSE ./package.json ``` There are three .js files in the archive: - `lib/v8-compile-cache.js`: Speeds up instantiation time by using the V8 code cache (https://www.npmjs.com/package/v8-compile-cache). This needs to be separate as it has to load **before** the bulk of the application code is loaded, so it can **not** be bundled - `lib/yarn-cli.js`: Contains all the bundled Yarn code - `bin/yarn.js`: Entry point to the app, just like today. Loads `v8-compile-cache` then loads `yarn-cli` This change means that **only** the JavaScript files that are actually used are included, resulting in a nice file size reduction for the installation packages: ![](http://ss.dan.cx/2017/04/Yarn_bundle_dist_metrics_-_Google_Sheets_-_Google__01-13.51.49.png) Differences are due to differing compression algorithms: Debian packages use xz or LZMA, RedHat uses gzip, Windows installer uses Cabinet They're also slightly faster to extract: ![image 3](https://cloud.githubusercontent.com/assets/91933/24582332/483b41f4-16e2-11e7-9509-8024b1e78a39.png) Testing was performed on my desktop computer (Intel Core i5 6500, Samsung 850 Evo 1TB SSD, Windows 10), with testing for Linux stuff (like installing the Debian package) tested in a Docker container. Raw data: https://docs.google.com/spreadsheets/d/1d8jdf3DU_GUFdotlPl08PkYa8SkzStK2tgnQ54ivsm0/edit?usp=sharing Performance is very slightly faster when using `v8-compile-cache` along with the bundled file, but it's not extremely significant (`yarn --version` went from 0.19s to 0.14s on my BuyVM server). The difference might be bigger on servers with slower disks (HDD) or with more overloaded servers. I also deleted the `build-dist.ps1` file because we _should_ be able to assume that Bash is available on Windows, particularly if Git is installed (as it comes with Git Bash). I need to verify that this works on AppVeyor.
1 parent b2882e7 commit 3af60cf

13 files changed

+84
-80
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/.nyc_output
66
/coverage
77
/dist
8+
/dist-debug
89
/artifacts
910
/updates
1011
/.roadrunner.json

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/scripts
2525
/coverage
2626
/dist
27+
/dist-debug
2728
/__tests__
2829
/.roadrunner.json
2930
.vs

appveyor.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ install:
1414

1515
build_script:
1616
- ps: if ($env:APPVEYOR_REPO_BRANCH -eq 'master') { node ./scripts/set-dev-version.js }
17-
- npm run build
18-
- ps: ./scripts/build-dist.ps1
19-
- npm run build-win-installer
17+
- yarn run build-dist
18+
- yarn run build-win-installer
2019

2120
test_script:
2221
- node --version

bin/yarn-bundle-entry.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
/* eslint-disable flowtype/require-valid-file-annotation */
4+
'use strict';
5+
6+
require('../lib/v8-compile-cache');
7+
module.exports = require('../lib/yarn-cli');

circle.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ test:
3131
- yarn lint
3232
- yarn test-ci -- -- --maxWorkers 3
3333
- yarn check-lockfile
34-
- yarn build-dist
35-
- node ./scripts/build-webpack.js
36-
- ls -t1 artifacts | head -n2 | while read entry; do node "./artifacts/$entry" --version; done
37-
- ./scripts/build-deb.sh
34+
- yarn run build-dist
35+
- yarn run build-deb
36+
# Test that the standalone .js build works as expected
37+
- ./artifacts/yarn-`./dist/bin/yarn --version`.js --version
38+
- ./artifacts/yarn-legacy-`./dist/bin/yarn --version`.js --version
39+
3840
# Test that installing as root works and that it also works
3941
# behind a user namespace which Circle CI tests are run under
4042
- sudo env "PATH=$PATH" bin/yarn install --force

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@
9292
"test-only": "jest --coverage --verbose",
9393
"lint": "eslint . && flow check",
9494
"release-branch": "./scripts/release-branch.sh",
95-
"build-dist": "./scripts/build-dist.sh",
95+
"build-bundle": "node ./scripts/build-webpack.js",
96+
"build-deb": "./scripts/build-deb.sh",
97+
"build-dist": "bash ./scripts/build-dist.sh",
9698
"build-chocolatey": "powershell ./scripts/build-chocolatey.ps1",
9799
"build-win-installer": "scripts\\build-windows-installer.bat"
98100
},

scripts/build-deb.sh

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,18 @@ mkdir -p $OUTPUT_DIR
2727
# Remove old packages
2828
rm -f $OUTPUT_DIR/*.deb $OUTPUT_DIR/*.rpm
2929

30-
# Extract to a temporary directory
30+
# Create temporary directory to start building up the package
3131
rm -rf $PACKAGE_TMPDIR
3232
mkdir -p $PACKAGE_TMPDIR/
3333
umask 0022 # Ensure permissions are correct (0755 for dirs, 0644 for files)
34-
tar zxf $TARBALL_NAME -C $PACKAGE_TMPDIR/
3534
PACKAGE_TMPDIR_ABSOLUTE=$(readlink -f $PACKAGE_TMPDIR)
3635

3736
# Create Linux package structure
3837
mkdir -p $PACKAGE_TMPDIR/usr/share/yarn/
3938
mkdir -p $PACKAGE_TMPDIR/usr/share/doc/yarn/
40-
mv $PACKAGE_TMPDIR/dist/bin $PACKAGE_TMPDIR/usr/share/yarn/
41-
mv $PACKAGE_TMPDIR/dist/lib $PACKAGE_TMPDIR/usr/share/yarn/
42-
mv $PACKAGE_TMPDIR/dist/lib-legacy $PACKAGE_TMPDIR/usr/share/yarn/
43-
mv $PACKAGE_TMPDIR/dist/node_modules $PACKAGE_TMPDIR/usr/share/yarn/
44-
mv $PACKAGE_TMPDIR/dist/package.json $PACKAGE_TMPDIR/usr/share/yarn/
39+
tar zxf $TARBALL_NAME -C $PACKAGE_TMPDIR/usr/share/yarn/ --strip 1
4540
cp resources/debian/copyright $PACKAGE_TMPDIR/usr/share/doc/yarn/copyright
4641

47-
# These are unneeded and throw lintian lint errors
48-
rm -f $PACKAGE_TMPDIR/usr/share/yarn/node_modules/node-uuid/benchmark/bench.gnu
49-
find $PACKAGE_TMPDIR/usr/share/yarn \( -name '*.md' -o -name '*.md~' -o -name '*.gitmodules' \) -delete
50-
51-
# Assume everything else is junk we don't need
52-
rm -rf $PACKAGE_TMPDIR/dist
53-
5442
# The Yarn executable expects to be in the same directory as the libraries, so
5543
# we can't just copy it directly to /usr/bin. Symlink them instead.
5644
mkdir -p $PACKAGE_TMPDIR/usr/bin/

scripts/build-dist-debug.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
set -ex
3+
4+
# This is similar to build-dist.sh, except it includes the original .js files
5+
# rather than bundling them into a single .js file. This distribution can
6+
# potentially be useful for debugging purposes, but it's more bloated than the
7+
# regular distribution.
8+
9+
npm run build
10+
npm pack
11+
rm -rf dist-debug
12+
mkdir dist-debug
13+
mkdir -p artifacts
14+
mv yarn-*.tgz dist-debug/pack.tgz
15+
16+
cd dist-debug
17+
umask 0022 # Ensure permissions are correct (0755 for dirs, 0644 for files)
18+
tar -xzf pack.tgz --strip 1
19+
rm -rf pack.tgz
20+
# Change this to "yarn install --production" once #1115 is fixed
21+
npm install --production
22+
../scripts/set-installation-method.js $(readlink -f package.json) tar
23+
cd ..
24+
25+
tar -cvzf artifacts/yarn-v`dist-debug/bin/yarn --version`.tar.gz dist-debug/*
26+
shasum -a 256 artifacts/yarn-*.tar.gz

scripts/build-dist.ps1

Lines changed: 0 additions & 18 deletions
This file was deleted.

scripts/build-dist.sh

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
#!/bin/sh
2-
1+
#!/bin/bash
32
set -ex
3+
# Builds the release tarball for Yarn.
4+
5+
umask 0022 # Ensure permissions are correct (0755 for dirs, 0644 for files)
46

5-
npm run build
6-
npm pack
7+
# Workaround for https://github.com/yarnpkg/yarn/issues/2591
8+
case "$(uname -s)" in
9+
*CYGWIN*|MSYS*|MINGW*)
10+
dist_yarn=dist/bin/yarn.cmd
11+
system_yarn=yarn.cmd
12+
;;
13+
*)
14+
dist_yarn=dist/bin/yarn
15+
system_yarn=yarn
16+
;;
17+
esac
18+
19+
rm -rf artifacts dist
720
rm -rf dist
8-
mkdir dist
9-
mkdir -p artifacts
10-
mv yarn-*.tgz dist/pack.tgz
21+
mkdir artifacts
22+
mkdir dist{,/bin,/lib}
1123

12-
cd dist
13-
umask 0022 # Ensure permissions are correct (0755 for dirs, 0644 for files)
14-
tar -xzf pack.tgz --strip 1
15-
rm -rf pack.tgz
16-
# Change this to "yarn install --production" once #1115 is fixed
17-
npm install --production
18-
../scripts/clean-node-modules.sh
19-
../scripts/set-installation-method.js $(readlink -f package.json) tar
20-
cd ..
24+
# Workaround for https://github.com/yarnpkg/yarn/issues/2591
25+
eval $system_yarn run build
26+
eval $system_yarn run build-bundle
27+
chmod +x artifacts/*.js
28+
29+
cp package.json dist/
30+
cp LICENSE dist/
31+
cp artifacts/yarn-legacy-*.js dist/lib/yarn-cli.js
32+
cp bin/yarn-bundle-entry.js dist/bin/yarn.js
33+
cp bin/{yarn,yarnpkg,*.cmd} dist/bin/
34+
cp -r bin/node-gyp-bin dist/bin/
35+
# We cannot bundle v8-compile-cache as it must be loaded separately to be effective.
36+
cp node_modules/v8-compile-cache/v8-compile-cache.js dist/lib/v8-compile-cache.js
2137

22-
tar -cvzf artifacts/yarn-v`dist/bin/yarn --version`.tar.gz dist/*
23-
shasum -a 256 artifacts/yarn-*.tar.gz
38+
version=`exec $dist_yarn --version`
39+
./scripts/set-installation-method.js $(readlink -f dist/package.json) tar
40+
tar -cvzf artifacts/yarn-v$version.tar.gz dist/*

scripts/build-webpack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const compiler = webpack({
3838
});
3939

4040
compiler.run((err, stats) => {
41-
const {fileDependencies} = stats.compilation;
41+
const fileDependencies = stats.compilation.fileDependencies;
4242
const filenames = fileDependencies.map(x => x.replace(basedir, ''));
4343
console.log(util.inspect(filenames, {maxArrayLength: null}));
4444
});

scripts/clean-node-modules.ps1

Lines changed: 0 additions & 11 deletions
This file was deleted.

scripts/clean-node-modules.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)