Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect output path due to esbuild outbase. #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.22.1
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 23 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path = "./lowest-common-ancestor.d.ts" />
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a pull-request adding types for lowest-common-ancestor to @DefinitelyTyped, but they're awaiting approval. These lines can be deleted once it's merged.


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';
Expand Down Expand Up @@ -43,10 +47,21 @@ function globPlugin<TControls extends boolean = false>({
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;

Expand All @@ -65,6 +80,10 @@ function globPlugin<TControls extends boolean = false>({
// 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,
};
Expand Down Expand Up @@ -96,7 +115,7 @@ function globPlugin<TControls extends boolean = false>({
.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);
Expand Down Expand Up @@ -159,9 +178,6 @@ function globPlugin<TControls extends boolean = false>({
}
});
} else {
const resolvedEntryPoints = (
await Promise.all(build.initialOptions.entryPoints.map((entryPoint) => glob(entryPoint)))
).flat();
build.initialOptions.entryPoints = resolvedEntryPoints;
}
},
Expand All @@ -173,8 +189,8 @@ function globPlugin<TControls extends boolean = false>({
// 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 };
3 changes: 3 additions & 0 deletions src/lowest-common-ancestor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'lowest-common-ancestor' {
export function lowestCommonAncestor(...filepaths: string[]): string;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down