Skip to content

Commit 752f1d5

Browse files
prune yarn devDependencies at the end of build (heroku#891)
* prune yarn devDependencies at the end of build * Remove warning message for production flag * Update tests to reflect need of workspace import * install workspace plugin when not present * update fixtures * cache build with yarn 2 * change grep of yarn plugins from yarnrc.yml; delete warning test for production flag * set up buildpack to instead warn that plugin is missing and prompt user to install before build
1 parent 7cc47c5 commit 752f1d5

32 files changed

+18103
-6737
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Node.js Buildpack Changelog
22

33
## main
4+
- Prune devDependencies with Yarn 2 ([#891](https://github.com/heroku/heroku-buildpack-nodejs/pull/891))
45

56
## v183 (2020-02-03)
67
- Update node version to 14 ([#885](https://github.com/heroku/heroku-buildpack-nodejs/pull/885))

bin/compile

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ export YARN_CACHE_FOLDER NPM_CONFIG_CACHE
145145
export VENDOR_PATH
146146

147147
if [[ "$YARN_2" == "true" ]]; then
148-
fail_yarn_2_production "$BUILD_DIR"
149-
150148
if [[ "$NODE_MODULES_CACHE" == "true" ]] && ! node_modules_enabled "$BUILD_DIR"; then
151149
warn "
152150
The buildpack won't cache the dependencies in .yarn/cache. Since you're using Yarn 2 and Plug'n'Play, you will need to customize your cache or unset NODE_MODULES_CACHE." "https://devcenter.heroku.com/articles/migrating-to-yarn-2#disable-dependency-caching"
@@ -348,22 +346,20 @@ cache_build() {
348346
}
349347

350348
prune_devdependencies() {
351-
if $YARN; then
352-
yarn_prune_devdependencies "$BUILD_DIR"
349+
if $YARN || $YARN_2; then
350+
yarn_prune_devdependencies "$BUILD_DIR" "$YARN_CACHE_FOLDER"
353351
else
354352
npm_prune_devdependencies "$BUILD_DIR"
355353
fi
356354
}
357355

358-
# if we are using yarn
359-
# and in the new feature
360-
# and we're using the default cache directories
361-
# then save off the cache after we prune out devDepenencies
362-
if [[ "$YARN_2" == "true" ]]; then
356+
if $YARN_2; then
357+
meta_set "build-step" "save-cache"
363358
cache_build | output "$LOG_FILE"
364359
meta_set "build-step" "prune-dependencies"
365-
meta_set "build-step" "save-cache"
366-
elif [[ "$YARN" == "true" ]] && [[ "$USE_YARN_CACHE" == "true" ]] && [[ "$(get_cache_directories "$BUILD_DIR")" == "" ]]; then
360+
header "Pruning devDependencies" | output "$LOG_FILE"
361+
prune_devdependencies | output "$LOG_FILE"
362+
elif "$YARN" && "$USE_YARN_CACHE" && [[ "$(get_cache_directories "$BUILD_DIR")" == "" ]]; then
367363
meta_set "build-step" "prune-dependencies"
368364
header "Pruning devDependencies" | output "$LOG_FILE"
369365
prune_devdependencies | output "$LOG_FILE"

lib/dependencies.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ yarn_2_install() {
156156

157157
yarn_prune_devdependencies() {
158158
local build_dir=${1:-}
159+
local cache_dir=${2:-}
160+
local workspace_plugin_path
159161

160162
if [ "$NODE_ENV" == "test" ]; then
161163
echo "Skipping because NODE_ENV is 'test'"
@@ -169,6 +171,21 @@ yarn_prune_devdependencies() {
169171
echo "Skipping because YARN_PRODUCTION is '$YARN_PRODUCTION'"
170172
meta_set "skipped-prune" "true"
171173
return 0
174+
elif $YARN_2; then
175+
cd "$build_dir" || return
176+
177+
if has_yarn_workspace_plugin_installed "$build_dir"; then
178+
meta_set "workspace-plugin-present" "true"
179+
180+
# The cache is removed beforehand because the command is running an install on devDeps, and
181+
# it will not remove the existing dependencies beforehand.
182+
rm -rf "$cache_dir"
183+
monitor "yarn-prune" yarn workspaces focus --all --production
184+
meta_set "skipped-prune" "false"
185+
else
186+
meta_set "workspace-plugin-present" "false"
187+
echo "Skipping because the Yarn workspace plugin is not present. Add the plugin to your source code with 'yarn plugin import workspace-tools'."
188+
fi
172189
else
173190
cd "$build_dir" || return
174191
monitor "yarn-prune" yarn install --frozen-lockfile --ignore-engines --ignore-scripts --prefer-offline 2>&1

lib/failure.sh

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,6 @@ fail_missing_yarn_vendor() {
418418
fi
419419
}
420420

421-
fail_yarn_2_production() {
422-
local build_dir="$1"
423-
424-
if [[ -n "$YARN_PRODUCTION" ]]; then
425-
header "Build failed"
426-
warn "
427-
The --production flag is no longer used for installing production dependencies for entire projects, but is still used in workspaces. See https://devcenter.heroku.com/articles/migrating-to-yarn-2#update-heroku-environment-with-plug-n-play-remove-production-environment-variables for additional information.
428-
"
429-
fail
430-
fi
431-
}
432-
433421
log_other_failures() {
434422
local log_file="$1"
435423
if grep -qi "sh: 1: .*: not found" "$log_file"; then

lib/yarn-2.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@ detect_yarn_2() {
2222

2323
has_yarn_cache() {
2424
local build_dir="$1"
25-
2625
[[ -d "$build_dir/.yarn/cache" ]]
2726
}
2827

28+
has_yarn_workspace_plugin_installed() {
29+
local build_dir="$1"
30+
local plugin_paths=$(get_yarn_plugin_paths "$build_dir")
31+
[[ ! -n $plugin_paths ]] && echo "$plugin_paths" | grep plugin-workspace-tools
32+
}
33+
34+
get_yarn_plugin_paths() {
35+
local build_dir="$1"
36+
$YQ r "$build_dir/.yarnrc.yml" "plugins.[*].path" 2>&1
37+
}
38+
2939
get_yarn_path() {
3040
local build_dir="$1"
3141
$YQ r "$build_dir/.yarnrc.yml" yarnPath 2>&1

test/fixtures/yarn-2-with-node-modules/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
},
1010
"devDependencies": {
1111
"debug": "^4.1.1"
12-
},
13-
"scripts": {
14-
"build": "tsc --version"
1512
}
1613
}

0 commit comments

Comments
 (0)