From c0f0808caa351791651d5a7450ad8b8f927c7a74 Mon Sep 17 00:00:00 2001 From: Cameron Hunter Date: Thu, 26 Aug 2021 10:07:10 -0700 Subject: [PATCH 1/3] Add an nvmrc file because `rollup-plugin-dts` has a node requirement >=12.22.1 --- .nvmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..25615cc --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +12.22.1 From e94f641ff857b57d431aceabe3d09bb8b6cf9070 Mon Sep 17 00:00:00 2001 From: Cameron Hunter Date: Thu, 26 Aug 2021 11:14:06 -0700 Subject: [PATCH 2/3] Fix issue with esbuild's outbase property when building a single file --- package.json | 1 + src/index.ts | 19 ++++++++++++++++--- src/lowest-common-ancestor.d.ts | 3 +++ yarn.lock | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/lowest-common-ancestor.d.ts 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..9f94f0c 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,6 +47,14 @@ 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; @@ -65,6 +77,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, }; @@ -159,9 +175,6 @@ function globPlugin({ } }); } else { - const resolvedEntryPoints = ( - await Promise.all(build.initialOptions.entryPoints.map((entryPoint) => glob(entryPoint))) - ).flat(); build.initialOptions.entryPoints = resolvedEntryPoints; } }, 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" From 9782f3880ef87ffbeef27f2662f1ff03cfbc331e Mon Sep 17 00:00:00 2001 From: Cameron Hunter Date: Thu, 26 Aug 2021 11:14:42 -0700 Subject: [PATCH 3/3] Ensure that esbuild's absWorkingDir option is respected --- src/index.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9f94f0c..985bab8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,7 +58,10 @@ function globPlugin({ // 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; @@ -112,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); @@ -186,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 };