Skip to content

Commit

Permalink
feat: add special handling to js_run_devserver for 1p deps to improve…
Browse files Browse the repository at this point in the history
… watch mode performance (#1411)
  • Loading branch information
gregmagolan authored Jan 19, 2024
1 parent 94a7afe commit 10121b8
Show file tree
Hide file tree
Showing 24 changed files with 523 additions and 53 deletions.
3 changes: 2 additions & 1 deletion e2e/webpack_devserver/.bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
mylib/node_modules
2 changes: 2 additions & 0 deletions e2e/webpack_devserver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ js_run_devserver(
"webpack.config.js",
":node_modules",
],
log_level = "debug",
tool = "webpack_binary",
)

Expand All @@ -45,5 +46,6 @@ js_run_devserver(
"webpack.config.cjs",
":node_modules",
],
log_level = "debug",
tool = "webpack_binary",
)
10 changes: 10 additions & 0 deletions e2e/webpack_devserver/mylib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")

npm_package(
name = "mylib",
srcs = [
"index.js",
"package.json",
],
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions e2e/webpack_devserver/mylib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const packageJson = require('./package.json')
const chalk = require('chalk')
module.exports = {
name: () => chalk.blue(packageJson.name),
}
6 changes: 6 additions & 0 deletions e2e/webpack_devserver/mylib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@mycorp/mylib",
"dependencies": {
"chalk": "^4"
}
}
3 changes: 2 additions & 1 deletion e2e/webpack_devserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@bazel/ibazel": "0.16.2"
},
"dependencies": {
"lodash": "4.17.21"
"lodash": "4.17.21",
"@mycorp/mylib": "workspace:*"
}
}
43 changes: 42 additions & 1 deletion e2e/webpack_devserver/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/webpack_devserver/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- '.'
- 'mylib'
58 changes: 55 additions & 3 deletions e2e/webpack_devserver/serve_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ _sedi() {
sed "${sedi[@]}" "$@"
}

echo ""
echo ""
echo "TEST - $0: $1"

./node_modules/.bin/ibazel run "$1" "$BZLMOD_FLAG" >/dev/null 2>&1 &
ibazel_logs=$(mktemp)
echo "Capturing ibazel logs to $ibazel_logs"
./node_modules/.bin/ibazel run "$1" "$BZLMOD_FLAG" >"$ibazel_logs" 2>&1 &
ibazel_pid="$!"

function _exit {
echo "Cleanup..."
kill "$ibazel_pid"
git checkout src/index.html
git checkout src/index.html >/dev/null 2>&1
git checkout mylib/index.js >/dev/null 2>&1
rm -f "$ibazel_logs"
}
trap _exit EXIT

Expand All @@ -43,6 +48,11 @@ if ! curl http://localhost:8080/index.html --fail 2>/dev/null | grep "Getting St
exit 1
fi

if ! curl http://localhost:8080/main.js --fail 2>/dev/null | grep "chalk.blue(packageJson.name)"; then
echo "ERROR: Expected http://localhost:8080/main.js to contain 'chalk.blue(packageJson.name)'"
exit 1
fi

_sedi 's#Getting Started#Goodbye#' src/index.html

echo "Waiting 5 seconds for ibazel rebuild after change to src/index.html..."
Expand All @@ -53,4 +63,46 @@ if ! curl http://localhost:8080/index.html --fail 2>/dev/null | grep "Goodbye";
exit 1
fi

_sedi 's#blue#red#' mylib/index.js

echo "Waiting 5 seconds for ibazel rebuild after change to mylib/index.js..."
sleep 5

if ! curl http://localhost:8080/main.js --fail 2>/dev/null | grep "chalk.red(packageJson.name)"; then
echo "ERROR: Expected http://localhost:8080/main.js to contain 'chalk.red(packageJson.name)'"
exit 1
fi

echo "Checking log file $ibazel_logs"

count=$(grep -c "Syncing file node_modules/.aspect_rules_js/@mycorp+mylib@0.0.0/node_modules/@mycorp/mylib/index.js" "$ibazel_logs" || true)
if [[ "$count" -ne 2 ]]; then
echo "ERROR: expected to have synced @mycorp/mylib/index.js 2 times but found ${count}"
exit 1
fi

count=$(grep -c "Skipping file node_modules/.aspect_rules_js/@mycorp+mylib@0.0.0/node_modules/@mycorp/mylib/index.js since its timestamp has not changed" "$ibazel_logs" || true)
if [[ "$count" -ne 1 ]]; then
echo "ERROR: expected to have skipped @mycorp/mylib/index.js due to timestamp 1 time but found ${count}"
exit 1
fi

count=$(grep -c "Syncing file node_modules/.aspect_rules_js/@mycorp+mylib@0.0.0/node_modules/@mycorp/mylib/package.json" "$ibazel_logs" || true)
if [[ "$count" -ne 1 ]]; then
echo "ERROR: expected to have synced @mycorp/mylib/package.json 1 time but found ${count}"
exit 1
fi

count=$(grep -c "Skipping file node_modules/.aspect_rules_js/@mycorp+mylib@0.0.0/node_modules/@mycorp/mylib/package.json since its timestamp has not changed" "$ibazel_logs" || true)
if [[ "$count" -ne 1 ]]; then
echo "ERROR: expected to have skipped @mycorp/mylib/package.json due to timestamp 1 time but found ${count}"
exit 1
fi

count=$(grep -c "Skipping file node_modules/.aspect_rules_js/@mycorp+mylib@0.0.0/node_modules/@mycorp/mylib/package.json since contents have not changed" "$ibazel_logs" || true)
if [[ "$count" -ne 1 ]]; then
echo "ERROR: expected to have skipped @mycorp/mylib/package.json due to contents 1 time but found ${count}"
exit 1
fi

echo "All tests passed"
3 changes: 2 additions & 1 deletion e2e/webpack_devserver/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import _ from 'lodash'
import { name } from '@mycorp/mylib'

function component() {
const element = document.createElement('div')

// Lodash, currently included via a script, is required for this line to work
element.innerHTML = _.join(['Hello', 'webpack'], ' ')
element.innerHTML = _.join(['Hello', 'webpack', name()], ' ')

return element
}
Expand Down
3 changes: 2 additions & 1 deletion e2e/webpack_devserver_esm/.bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
mylib/node_modules
1 change: 1 addition & 0 deletions e2e/webpack_devserver_esm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ js_run_devserver(
"webpack.config.mjs",
":node_modules",
],
log_level = "debug",
tool = "webpack_binary",
)
10 changes: 10 additions & 0 deletions e2e/webpack_devserver_esm/mylib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")

npm_package(
name = "mylib",
srcs = [
"index.js",
"package.json",
],
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions e2e/webpack_devserver_esm/mylib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import packageJson from './package.json'
import chalk from 'chalk'
export function name() {
return chalk.blue(packageJson.name)
}
6 changes: 6 additions & 0 deletions e2e/webpack_devserver_esm/mylib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@mycorp/mylib",
"dependencies": {
"chalk": "^4"
}
}
3 changes: 2 additions & 1 deletion e2e/webpack_devserver_esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@bazel/ibazel": "0.16.2"
},
"dependencies": {
"lodash": "4.17.21"
"lodash": "4.17.21",
"@mycorp/mylib": "workspace:*"
}
}
43 changes: 42 additions & 1 deletion e2e/webpack_devserver_esm/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/webpack_devserver_esm/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- '.'
- 'mylib'
Loading

0 comments on commit 10121b8

Please sign in to comment.