Skip to content

Commit

Permalink
fix(bundling): declaration:true should find the correct package root …
Browse files Browse the repository at this point in the history
…regardless of cwd #26261
  • Loading branch information
Coly010 committed Aug 21, 2024
1 parent 2065033 commit 76a2141
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
7 changes: 5 additions & 2 deletions packages/esbuild/src/executors/esbuild/esbuild.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './lib/build-esbuild-options';
import { getExtraDependencies } from './lib/get-extra-dependencies';
import { DependentBuildableProjectNode } from '@nx/js/src/utils/buildable-libs-utils';
import { join } from 'path';
import { join, relative } from 'path';

const BUILD_WATCH_FAILED = `[ ${chalk.red(
'watch'
Expand Down Expand Up @@ -58,6 +58,7 @@ export async function* esbuildExecutor(
});
}
return acc;
return acc;
}, []);

if (!options.thirdParty) {
Expand Down Expand Up @@ -210,6 +211,7 @@ function getTypeCheckOptions(
context: ExecutorContext
) {
const { watch, tsConfig, outputPath } = options;
const projectRoot = context.projectGraph.nodes[context.projectName].data.root;

const typeCheckOptions: TypeCheckOptions = {
...(options.declaration
Expand All @@ -220,9 +222,10 @@ function getTypeCheckOptions(
: {
mode: 'noEmit',
}),
tsConfigPath: tsConfig,
tsConfigPath: relative(process.cwd(), join(context.root, tsConfig)),
workspaceRoot: context.root,
rootDir: options.declarationRootDir ?? context.root,
projectRoot,
};

if (watch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
joinPathFragments,
normalizePath,
ProjectGraphProjectNode,
workspaceRoot,
} from '@nx/devkit';

import { getClientEnvironment } from '../../../utils/environment-variables';
import { NormalizedEsBuildExecutorOptions } from '../schema';
import { getEntryPoints } from '../../../utils/get-entry-points';
import { join, relative } from 'path';

const ESM_FILE_EXTENSION = '.js';
const CJS_FILE_EXTENSION = '.cjs';
Expand Down Expand Up @@ -38,7 +40,7 @@ export function buildEsbuildOptions(
platform: options.platform,
target: options.target,
metafile: options.metafile,
tsconfig: options.tsConfig,
tsconfig: relative(process.cwd(), join(context.root, options.tsConfig)),
sourcemap:
(options.sourcemap ?? options.userDefinedBuildOptions?.sourcemap) ||
false,
Expand Down
15 changes: 12 additions & 3 deletions packages/js/src/utils/typescript/run-type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface BaseTypeCheckOptions {
cacheDir?: string;
incremental?: boolean;
rootDir?: string;
projectRoot?: string;
}

type Mode = NoEmitMode | EmitDeclarationOnlyMode;
Expand Down Expand Up @@ -118,7 +119,7 @@ export async function runTypeCheck(

async function setupTypeScript(options: TypeCheckOptions) {
const ts = await import('typescript');
const { workspaceRoot, tsConfigPath, cacheDir, incremental, rootDir } =
const { workspaceRoot, tsConfigPath, cacheDir, incremental, projectRoot } =
options;
const config = readTsConfig(tsConfigPath);
if (config.errors.length) {
Expand All @@ -128,15 +129,23 @@ async function setupTypeScript(options: TypeCheckOptions) {

const emitOptions =
options.mode === 'emitDeclarationOnly'
? { emitDeclarationOnly: true, declaration: true, outDir: options.outDir }
? {
emitDeclarationOnly: true,
declaration: true,
outDir: options.outDir,
declarationDir:
options.projectRoot && options.outDir.indexOf(projectRoot)
? options.outDir.replace(projectRoot, '')
: undefined,
}
: { noEmit: true };

const compilerOptions = {
...config.options,
skipLibCheck: true,
...emitOptions,
incremental,
rootDir: rootDir || config.options.rootDir,
rootDir: options.rootDir || config.options.rootDir,
};

return { ts, workspaceRoot, cacheDir, config, compilerOptions };
Expand Down

0 comments on commit 76a2141

Please sign in to comment.