Skip to content

Commit

Permalink
feat(plugin): change nodeModulesDir filed to actual absolute path t…
Browse files Browse the repository at this point in the history
…o node_modules
  • Loading branch information
TomokiMiyauci committed Jun 5, 2024
1 parent 835e691 commit ac97ee6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 19 deletions.
1 change: 0 additions & 1 deletion docs/dependence.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ The following flags are required to run this plugin:

The following fields may be referenced in the build options:

- [`absWorkingDir`](https://esbuild.github.io/api/#working-directory)
- [`platform`](https://esbuild.github.io/api/#platform)
- [`conditions`](https://esbuild.github.io/api/#conditions)
- [`mainFields`](https://esbuild.github.io/api/#main-fields)
Expand Down
1 change: 0 additions & 1 deletion src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export function resolvePlatform(platForm: Platform): string | null {

export type DependentBuildOptions = Pick<
BuildOptions,
| "absWorkingDir"
| "platform"
| "mainFields"
| "resolveExtensions"
Expand Down
20 changes: 4 additions & 16 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import { resolveReferrer } from "./referrer.ts";
import { existDir, existFile, readFile, realURL } from "./io.ts";

export interface Options {
/**
* @default false
*/
nodeModulesDir?: boolean;
/** Path to `node_modules` dir. */
nodeModulesDir?: string;

/**
* @default new DenoDir().root
Expand All @@ -31,7 +29,7 @@ export function denoSpecifierPlugin(options: Options = {}): Plugin {
name: "deno-specifier",
setup(build) {
const DENO_DIR = options.denoDir ?? new DenoDir().root;
const infoOptions = options.nodeModulesDir
const infoOptions = typeof options.nodeModulesDir === "string"
? {
json: true,
noConfig: true,
Expand Down Expand Up @@ -61,17 +59,7 @@ export function denoSpecifierPlugin(options: Options = {}): Plugin {
}

const strategy = options.nodeModulesDir
? (() => {
const root = build.initialOptions.absWorkingDir;

if (!root) {
throw new Error(
`'absWorkingDir' is required when nodeModulesDir is 'true'`,
);
}

return new LocalStrategy(root);
})()
? new LocalStrategy(options.nodeModulesDir)
: new GlobalStrategy(DENO_DIR);

const resolveOptions = {
Expand Down
4 changes: 3 additions & 1 deletion src/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join } from "@std/url/join";
import { toFileUrl } from "@std/path/to-file-url";
import { dirname } from "@std/path/dirname";
import { getParents } from "./npm/cjs/utils.ts";
import { IO } from "./types.ts";
import { createNpmRegistryURL } from "./utils.ts";
Expand Down Expand Up @@ -27,7 +28,8 @@ export class LocalStrategy implements Strategy {

resolveSymbolic = true;
constructor(nodeModulesDir: string) {
this.#root = toFileUrl(nodeModulesDir);
const rootDir = dirname(nodeModulesDir);
this.#root = toFileUrl(rootDir);
}

async getPackageURL(args: PackageArgs): Promise<URL | null> {
Expand Down

0 comments on commit ac97ee6

Please sign in to comment.