cli: fix backend dev watch mode

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-03-17 21:13:07 +01:00
parent 5d69520238
commit 0d72065dfd
6 changed files with 74 additions and 278 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': minor
---
The backend devlopment server transpilation has been replaced with a simplified solution based on SWC, which is already the transpiler used for tests. This fixed an issue where never versions of the `tsx` dependency had a new contract for signalling dependencies, breaking watch mode. This change fixed file watches as well as enables sourcemaps.
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { transformSync } = require('@swc/core');
const { addHook } = require('pirates');
addHook(
(code, filename) => {
const transformed = transformSync(code, {
filename,
sourceMaps: 'inline',
module: { type: 'commonjs' },
});
process.send?.({ type: 'watch', path: filename });
return transformed.code;
},
{ extensions: ['.ts', '.cts'], ignoreNodeModules: true },
);
addHook(
(code, filename) => {
process.send?.({ type: 'watch', path: filename });
return code;
},
{ extensions: ['.js', '.cjs'], ignoreNodeModules: true },
);
+2 -1
View File
@@ -1,6 +1,6 @@
# Knip report
## Unused dependencies (27)
## Unused dependencies (28)
| Name | Location | Severity |
| :------------------------------- | :----------- | :------- |
@@ -25,6 +25,7 @@
| @swc/core | package.json | error |
| @swc/jest | package.json | error |
| esbuild | package.json | error |
| pirates | package.json | error |
| postcss | package.json | error |
| process | package.json | error |
| sucrase | package.json | error |
+1 -1
View File
@@ -114,6 +114,7 @@
"ora": "^5.3.0",
"p-limit": "^3.1.0",
"p-queue": "^6.6.2",
"pirates": "^4.0.6",
"postcss": "^8.1.0",
"process": "^0.11.10",
"react-dev-utils": "^12.0.0-next.60",
@@ -131,7 +132,6 @@
"swc-loader": "^0.2.3",
"tar": "^6.1.12",
"terser-webpack-plugin": "^5.1.3",
"tsx": "^4.0.0",
"util": "^0.12.3",
"webpack": "^5.70.0",
"webpack-dev-server": "^4.7.3",
@@ -22,22 +22,16 @@ import { ctrlc } from 'ctrlc-windows';
import { IpcServer } from './IpcServer';
import { ServerDataStore } from './ServerDataStore';
import debounce from 'lodash/debounce';
import { fileURLToPath, pathToFileURL } from 'url';
import { fileURLToPath } from 'url';
import { isAbsolute as isAbsolutePath } from 'path';
import { paths } from '../paths';
import spawn from 'cross-spawn';
const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(Number);
const supportsModuleLoaderRegister =
nodeMajor > 20 ||
(nodeMajor === 20 && nodeMinor >= 6) ||
(nodeMajor === 18 && nodeMinor >= 19);
const loaderArgs = [
'--enable-source-maps',
'--require',
require.resolve('tsx/preflight'),
supportsModuleLoaderRegister ? '--import' : '--loader',
pathToFileURL(require.resolve('tsx')).toString(), // Windows prefers a URL here
require.resolve('@backstage/cli/config/nodeTransform.cjs'),
// TODO: Support modules, although there's currently no way to load them since import() is transpiled tp require()
];
export async function startBackendExperimental(options: BackendServeOptions) {
@@ -51,11 +45,21 @@ export async function startBackendExperimental(options: BackendServeOptions) {
ServerDataStore.bind(server);
let exiting = false;
let firstStart = true;
let child: ChildProcess | undefined;
let watcher: FSWatcher | undefined = undefined;
let shutdownPromise: Promise<void> | undefined = undefined;
const watchedPaths = new Set<string>();
const restart = debounce(async () => {
if (firstStart) {
firstStart = false;
} else {
console.log();
console.log('Change detected, restarting the development server...');
console.log();
}
// If a re-trigger happens during an existing shutdown, we just ignore it
if (shutdownPromise) {
return;
@@ -115,14 +119,18 @@ export async function startBackendExperimental(options: BackendServeOptions) {
// This captures messages sent by @esbuild-kit/cjs-loader
child.on('message', (data: { type?: string } | null) => {
if (typeof data === 'object' && data?.type === 'dependency') {
if (!watcher) {
return;
}
if (typeof data === 'object' && data?.type === 'watch') {
let path = (data as { path: string }).path;
if (path.startsWith('file:')) {
path = fileURLToPath(path);
}
if (isAbsolutePath(path)) {
watcher?.add(path);
if (isAbsolutePath(path) && !watchedPaths.has(path)) {
watchedPaths.add(path);
watcher.add(path);
}
}
});
@@ -130,7 +138,7 @@ export async function startBackendExperimental(options: BackendServeOptions) {
restart();
watcher = watch([], {
watcher = watch(['./package.json'], {
cwd: process.cwd(),
ignoreInitial: true,
ignorePermissionErrors: true,
+5 -262
View File
@@ -3692,6 +3692,7 @@ __metadata:
ora: ^5.3.0
p-limit: ^3.1.0
p-queue: ^6.6.2
pirates: ^4.0.6
postcss: ^8.1.0
process: ^0.11.10
react-dev-utils: ^12.0.0-next.60
@@ -3710,7 +3711,6 @@ __metadata:
tar: ^6.1.12
terser-webpack-plugin: ^5.1.3
ts-node: ^10.0.0
tsx: ^4.0.0
util: ^0.12.3
webpack: ^5.70.0
webpack-dev-server: ^4.7.3
@@ -10957,13 +10957,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/aix-ppc64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/aix-ppc64@npm:0.19.12"
conditions: os=aix & cpu=ppc64
languageName: node
linkType: hard
"@esbuild/aix-ppc64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/aix-ppc64@npm:0.20.2"
@@ -10978,13 +10971,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/android-arm64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/android-arm64@npm:0.19.12"
conditions: os=android & cpu=arm64
languageName: node
linkType: hard
"@esbuild/android-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/android-arm64@npm:0.20.2"
@@ -10999,13 +10985,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/android-arm@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/android-arm@npm:0.19.12"
conditions: os=android & cpu=arm
languageName: node
linkType: hard
"@esbuild/android-arm@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/android-arm@npm:0.20.2"
@@ -11020,13 +10999,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/android-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/android-x64@npm:0.19.12"
conditions: os=android & cpu=x64
languageName: node
linkType: hard
"@esbuild/android-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/android-x64@npm:0.20.2"
@@ -11041,13 +11013,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/darwin-arm64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/darwin-arm64@npm:0.19.12"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard
"@esbuild/darwin-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/darwin-arm64@npm:0.20.2"
@@ -11062,13 +11027,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/darwin-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/darwin-x64@npm:0.19.12"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard
"@esbuild/darwin-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/darwin-x64@npm:0.20.2"
@@ -11083,13 +11041,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/freebsd-arm64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/freebsd-arm64@npm:0.19.12"
conditions: os=freebsd & cpu=arm64
languageName: node
linkType: hard
"@esbuild/freebsd-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/freebsd-arm64@npm:0.20.2"
@@ -11104,13 +11055,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/freebsd-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/freebsd-x64@npm:0.19.12"
conditions: os=freebsd & cpu=x64
languageName: node
linkType: hard
"@esbuild/freebsd-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/freebsd-x64@npm:0.20.2"
@@ -11125,13 +11069,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-arm64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-arm64@npm:0.19.12"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard
"@esbuild/linux-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-arm64@npm:0.20.2"
@@ -11146,13 +11083,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-arm@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-arm@npm:0.19.12"
conditions: os=linux & cpu=arm
languageName: node
linkType: hard
"@esbuild/linux-arm@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-arm@npm:0.20.2"
@@ -11167,13 +11097,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-ia32@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-ia32@npm:0.19.12"
conditions: os=linux & cpu=ia32
languageName: node
linkType: hard
"@esbuild/linux-ia32@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-ia32@npm:0.20.2"
@@ -11188,13 +11111,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-loong64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-loong64@npm:0.19.12"
conditions: os=linux & cpu=loong64
languageName: node
linkType: hard
"@esbuild/linux-loong64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-loong64@npm:0.20.2"
@@ -11209,13 +11125,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-mips64el@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-mips64el@npm:0.19.12"
conditions: os=linux & cpu=mips64el
languageName: node
linkType: hard
"@esbuild/linux-mips64el@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-mips64el@npm:0.20.2"
@@ -11230,13 +11139,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-ppc64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-ppc64@npm:0.19.12"
conditions: os=linux & cpu=ppc64
languageName: node
linkType: hard
"@esbuild/linux-ppc64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-ppc64@npm:0.20.2"
@@ -11251,13 +11153,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-riscv64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-riscv64@npm:0.19.12"
conditions: os=linux & cpu=riscv64
languageName: node
linkType: hard
"@esbuild/linux-riscv64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-riscv64@npm:0.20.2"
@@ -11272,13 +11167,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-s390x@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-s390x@npm:0.19.12"
conditions: os=linux & cpu=s390x
languageName: node
linkType: hard
"@esbuild/linux-s390x@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-s390x@npm:0.20.2"
@@ -11293,13 +11181,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/linux-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/linux-x64@npm:0.19.12"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard
"@esbuild/linux-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/linux-x64@npm:0.20.2"
@@ -11314,13 +11195,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/netbsd-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/netbsd-x64@npm:0.19.12"
conditions: os=netbsd & cpu=x64
languageName: node
linkType: hard
"@esbuild/netbsd-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/netbsd-x64@npm:0.20.2"
@@ -11335,13 +11209,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/openbsd-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/openbsd-x64@npm:0.19.12"
conditions: os=openbsd & cpu=x64
languageName: node
linkType: hard
"@esbuild/openbsd-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/openbsd-x64@npm:0.20.2"
@@ -11356,13 +11223,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/sunos-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/sunos-x64@npm:0.19.12"
conditions: os=sunos & cpu=x64
languageName: node
linkType: hard
"@esbuild/sunos-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/sunos-x64@npm:0.20.2"
@@ -11377,13 +11237,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/win32-arm64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/win32-arm64@npm:0.19.12"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard
"@esbuild/win32-arm64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/win32-arm64@npm:0.20.2"
@@ -11398,13 +11251,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/win32-ia32@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/win32-ia32@npm:0.19.12"
conditions: os=win32 & cpu=ia32
languageName: node
linkType: hard
"@esbuild/win32-ia32@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/win32-ia32@npm:0.20.2"
@@ -11419,13 +11265,6 @@ __metadata:
languageName: node
linkType: hard
"@esbuild/win32-x64@npm:0.19.12":
version: 0.19.12
resolution: "@esbuild/win32-x64@npm:0.19.12"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard
"@esbuild/win32-x64@npm:0.20.2":
version: 0.20.2
resolution: "@esbuild/win32-x64@npm:0.20.2"
@@ -26909,86 +26748,6 @@ __metadata:
languageName: node
linkType: hard
"esbuild@npm:~0.19.10":
version: 0.19.12
resolution: "esbuild@npm:0.19.12"
dependencies:
"@esbuild/aix-ppc64": 0.19.12
"@esbuild/android-arm": 0.19.12
"@esbuild/android-arm64": 0.19.12
"@esbuild/android-x64": 0.19.12
"@esbuild/darwin-arm64": 0.19.12
"@esbuild/darwin-x64": 0.19.12
"@esbuild/freebsd-arm64": 0.19.12
"@esbuild/freebsd-x64": 0.19.12
"@esbuild/linux-arm": 0.19.12
"@esbuild/linux-arm64": 0.19.12
"@esbuild/linux-ia32": 0.19.12
"@esbuild/linux-loong64": 0.19.12
"@esbuild/linux-mips64el": 0.19.12
"@esbuild/linux-ppc64": 0.19.12
"@esbuild/linux-riscv64": 0.19.12
"@esbuild/linux-s390x": 0.19.12
"@esbuild/linux-x64": 0.19.12
"@esbuild/netbsd-x64": 0.19.12
"@esbuild/openbsd-x64": 0.19.12
"@esbuild/sunos-x64": 0.19.12
"@esbuild/win32-arm64": 0.19.12
"@esbuild/win32-ia32": 0.19.12
"@esbuild/win32-x64": 0.19.12
dependenciesMeta:
"@esbuild/aix-ppc64":
optional: true
"@esbuild/android-arm":
optional: true
"@esbuild/android-arm64":
optional: true
"@esbuild/android-x64":
optional: true
"@esbuild/darwin-arm64":
optional: true
"@esbuild/darwin-x64":
optional: true
"@esbuild/freebsd-arm64":
optional: true
"@esbuild/freebsd-x64":
optional: true
"@esbuild/linux-arm":
optional: true
"@esbuild/linux-arm64":
optional: true
"@esbuild/linux-ia32":
optional: true
"@esbuild/linux-loong64":
optional: true
"@esbuild/linux-mips64el":
optional: true
"@esbuild/linux-ppc64":
optional: true
"@esbuild/linux-riscv64":
optional: true
"@esbuild/linux-s390x":
optional: true
"@esbuild/linux-x64":
optional: true
"@esbuild/netbsd-x64":
optional: true
"@esbuild/openbsd-x64":
optional: true
"@esbuild/sunos-x64":
optional: true
"@esbuild/win32-arm64":
optional: true
"@esbuild/win32-ia32":
optional: true
"@esbuild/win32-x64":
optional: true
bin:
esbuild: bin/esbuild
checksum: 2936e29107b43e65a775b78b7bc66ddd7d76febd73840ac7e825fb22b65029422ff51038a08d19b05154f543584bd3afe7d1ef1c63900429475b17fbe61cb61f
languageName: node
linkType: hard
"escalade@npm:^3.1.1":
version: 3.1.1
resolution: "escalade@npm:3.1.1"
@@ -29066,7 +28825,7 @@ __metadata:
languageName: node
linkType: hard
"fsevents@npm:^2.3.2, fsevents@npm:~2.3.2, fsevents@npm:~2.3.3":
"fsevents@npm:^2.3.2, fsevents@npm:~2.3.2":
version: 2.3.3
resolution: "fsevents@npm:2.3.3"
dependencies:
@@ -29085,7 +28844,7 @@ __metadata:
languageName: node
linkType: hard
"fsevents@patch:fsevents@^2.3.2#~builtin<compat/fsevents>, fsevents@patch:fsevents@~2.3.2#~builtin<compat/fsevents>, fsevents@patch:fsevents@~2.3.3#~builtin<compat/fsevents>":
"fsevents@patch:fsevents@^2.3.2#~builtin<compat/fsevents>, fsevents@patch:fsevents@~2.3.2#~builtin<compat/fsevents>":
version: 2.3.3
resolution: "fsevents@patch:fsevents@npm%3A2.3.3#~builtin<compat/fsevents>::version=2.3.3&hash=18f3a7"
dependencies:
@@ -29323,7 +29082,7 @@ __metadata:
languageName: node
linkType: hard
"get-tsconfig@npm:^4.7.0, get-tsconfig@npm:^4.7.2":
"get-tsconfig@npm:^4.7.0":
version: 4.7.2
resolution: "get-tsconfig@npm:4.7.2"
dependencies:
@@ -38240,7 +37999,7 @@ __metadata:
languageName: node
linkType: hard
"pirates@npm:^4.0.1, pirates@npm:^4.0.4, pirates@npm:^4.0.5":
"pirates@npm:^4.0.1, pirates@npm:^4.0.4, pirates@npm:^4.0.5, pirates@npm:^4.0.6":
version: 4.0.6
resolution: "pirates@npm:4.0.6"
checksum: 46a65fefaf19c6f57460388a5af9ab81e3d7fd0e7bc44ca59d753cb5c4d0df97c6c6e583674869762101836d68675f027d60f841c105d72734df9dfca97cbcc6
@@ -44328,22 +44087,6 @@ __metadata:
languageName: node
linkType: hard
"tsx@npm:^4.0.0":
version: 4.7.1
resolution: "tsx@npm:4.7.1"
dependencies:
esbuild: ~0.19.10
fsevents: ~2.3.3
get-tsconfig: ^4.7.2
dependenciesMeta:
fsevents:
optional: true
bin:
tsx: dist/cli.mjs
checksum: 7f77294c0eee9a9b203f89eb299ee80b393d6b4bf79ec01b650502213a23ea08d0dfc52e938b302ef27c97b70557f7f5a590c3174a7e3c8f1140c668eea4a3a2
languageName: node
linkType: hard
"tty-browserify@npm:0.0.0":
version: 0.0.0
resolution: "tty-browserify@npm:0.0.0"