diff --git a/.nvmrc b/.nvmrc
new file mode 100644
index 0000000..25615cc
--- /dev/null
+++ b/.nvmrc
@@ -0,0 +1 @@
+12.22.1
diff --git a/package.json b/package.json
index 3ab6331..199df53 100644
--- a/package.json
+++ b/package.json
@@ -52,6 +52,7 @@
},
"dependencies": {
"chokidar": "^3.5.2",
+ "lowest-common-ancestor": "^2.0.1",
"minimatch": "^3.0.4",
"tiny-glob": "^0.2.9",
"tiny-invariant": "^1.1.0"
diff --git a/src/index.ts b/src/index.ts
index 308f1e5..985bab8 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,10 @@
+// eslint-disable-next-line @typescript-eslint/triple-slash-reference
+///
+
import chokidar from 'chokidar';
import * as esbuild from 'esbuild';
import fs from 'fs';
+import { lowestCommonAncestor } from 'lowest-common-ancestor';
import match from 'minimatch';
import path from 'path';
import glob from 'tiny-glob';
@@ -43,10 +47,21 @@ function globPlugin({
throw new TypeError('GlobPlugin currently only supports array entrypoints');
}
+ const resolvedEntryPoints = (
+ await Promise.all(
+ build.initialOptions.entryPoints.map((entryPoint) =>
+ glob(entryPoint, { cwd: build.initialOptions.absWorkingDir, filesOnly: true }),
+ ),
+ )
+ ).flat();
+
// Watch mode
if (build.initialOptions.watch) {
const entryGlobs = build.initialOptions.entryPoints;
- const watcher = chokidar.watch(entryGlobs, chokidarOptions);
+ const watcher = chokidar.watch(entryGlobs, {
+ cwd: build.initialOptions.absWorkingDir,
+ ...chokidarOptions,
+ });
context.watcher = watcher;
@@ -65,6 +80,10 @@ function globPlugin({
// Plugin relies on incremental and metafile options
const sharedOptions = {
...build.initialOptions,
+ // Calculate the lowest common ancestor or esbuild will incorrectly
+ // determine it from the single entrypoint that is added/changed.
+ // @see https://esbuild.github.io/api/#outbase
+ outbase: build.initialOptions.outbase || lowestCommonAncestor(...resolvedEntryPoints),
incremental: true,
metafile: true,
};
@@ -96,7 +115,7 @@ function globPlugin({
.flatMap((output) =>
Object.keys(output.inputs)
.filter((input) => !input.includes('node_modules'))
- .map((input) => normalizePath(input)),
+ .map((input) => normalizePath(input, build.initialOptions.absWorkingDir)),
);
watcher.add(inputs);
@@ -159,9 +178,6 @@ function globPlugin({
}
});
} else {
- const resolvedEntryPoints = (
- await Promise.all(build.initialOptions.entryPoints.map((entryPoint) => glob(entryPoint)))
- ).flat();
build.initialOptions.entryPoints = resolvedEntryPoints;
}
},
@@ -173,8 +189,8 @@ function globPlugin({
// UTILITIES
// ---------
-function normalizePath(filePath: string): string {
- return path.relative(process.cwd(), filePath.replace(/^(\w+:)/, ''));
+function normalizePath(filePath: string, cwd: string = process.cwd()): string {
+ return path.relative(cwd, filePath.replace(/^(\w+:)/, ''));
}
export { globPlugin };
diff --git a/src/lowest-common-ancestor.d.ts b/src/lowest-common-ancestor.d.ts
new file mode 100644
index 0000000..60b10e5
--- /dev/null
+++ b/src/lowest-common-ancestor.d.ts
@@ -0,0 +1,3 @@
+declare module 'lowest-common-ancestor' {
+ export function lowestCommonAncestor(...filepaths: string[]): string;
+}
diff --git a/yarn.lock b/yarn.lock
index b478fd8..f1afba0 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2329,6 +2329,11 @@ lowercase-keys@^2.0.0:
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479"
integrity sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==
+lowest-common-ancestor@^2.0.1:
+ version "2.0.1"
+ resolved "https://registry.yarnpkg.com/lowest-common-ancestor/-/lowest-common-ancestor-2.0.1.tgz#e9ccfc339424c46b58a4cb1d1dd310c223a21345"
+ integrity sha512-csg92ZIP3jkhL0+DqZFUphYYR31gxtuQeg2CTJrftFlwUQ7IiPGadCe37MYQRo4BWJkkFEHxtrQscuArgf5IbQ==
+
lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"