Skip to content

Commit

Permalink
fix(js): merge target defaults and misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Oct 16, 2024
1 parent af94303 commit ed18928
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 84 deletions.
17 changes: 14 additions & 3 deletions packages/esbuild/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
formatFiles,
joinPathFragments,
readNxJson,
readProjectConfiguration,
Tree,
updateJson,
Expand All @@ -10,11 +11,12 @@ import {
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { basename, dirname, join, normalize, relative } from 'node:path/posix';
import { mergeTargetConfigurations } from 'nx/src/devkit-internals';
import { PackageJson } from 'nx/src/utils/package-json';
import { EsBuildExecutorOptions } from '../../executors/esbuild/schema';
import { esbuildInitGenerator } from '../init/init';
import { EsBuildProjectSchema } from './schema';
import { basename, dirname, join, normalize, relative } from 'node:path/posix';
import { PackageJson } from 'nx/src/utils/package-json';

export async function configurationGenerator(
tree: Tree,
Expand Down Expand Up @@ -121,14 +123,23 @@ function updatePackageJson(
options: EsBuildProjectSchema,
isTsSolutionSetup: boolean
) {
// TODO(esbuild): consider add exports and use module
const project = readProjectConfiguration(tree, options.project);
const nxJson = readNxJson(tree);
const projectTarget = project.targets[options.buildTarget];
const mergedTarget = mergeTargetConfigurations(
projectTarget,
(projectTarget.executor
? nxJson.targetDefaults?.[projectTarget.executor]
: undefined) ?? nxJson.targetDefaults?.[options.buildTarget]
);

const {
declarationRootDir = '.',
main,
outputPath,
outputFileName,
} = project.targets[options.buildTarget].options;
} = mergedTarget.options;
const mainName = basename(main).replace(/\.[tj]s$/, '');
const mainDir = dirname(main);
const relativeDeclarationDir = relative(
Expand Down
139 changes: 87 additions & 52 deletions packages/js/src/generators/setup-build/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@nx/devkit';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
import { basename, dirname, join, normalize, relative } from 'node:path/posix';
import { mergeTargetConfigurations } from 'nx/src/devkit-internals';
import type { PackageJson } from 'nx/src/utils/package-json';
import { ensureProjectIsIncludedInPluginRegistrations } from '../..//utils/typescript/plugin';
import { getImportPath } from '../../utils/get-import-path';
Expand Down Expand Up @@ -91,6 +92,10 @@ export async function setupBuildGenerator(
options.tsConfig = tsConfigFile;

const isTsSolutionSetup = isUsingTsSolutionSetup(tree);
const nxJson = readNxJson(tree);
const addPlugin =
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false;

switch (options.bundler) {
case 'vite': {
Expand All @@ -101,10 +106,11 @@ export async function setupBuildGenerator(
const task = await viteConfigurationGenerator(tree, {
buildTarget: options.buildTarget,
project: options.project,
newProject: true,
newProject: false,
uiFramework: 'none',
includeVitest: false,
includeLib: true,
addPlugin,
skipFormat: true,
});
tasks.push(task);
Expand Down Expand Up @@ -134,6 +140,7 @@ export async function setupBuildGenerator(
project: options.project,
compiler: 'tsc',
format: ['cjs', 'esm'],
addPlugin,
skipFormat: true,
skipValidation: true,
});
Expand Down Expand Up @@ -211,79 +218,72 @@ function updatePackageJsonForTsc(
options: SetupBuildGeneratorSchema,
project: ProjectConfiguration
) {
if (!ts) {
ts = ensureTypescript();
}
let main: string;
let rootDir: string;
let outputPath: string;
if (project.targets?.[options.buildTarget]) {
const mergedTarget = mergeTargetDefaults(
tree,
project,
options.buildTarget
);
({ main, rootDir, outputPath } = mergedTarget.options);
} else {
main = options.main;

let main = options.main;
const tsconfig = readTsConfig(options.tsConfig, {
...ts.sys,
readFile: (p) => tree.read(p, 'utf-8'),
fileExists: (p) => tree.exists(p),
});
if (!ts) {
ts = ensureTypescript();
}

let {
rootDir: tsRootDir = project.root,
outDir: tsOutDir,
outFile: tsOutFile,
} = tsconfig.options;
const tsconfig = readTsConfig(options.tsConfig, {
...ts.sys,
readFile: (p) => tree.read(p, 'utf-8'),
fileExists: (p) => tree.exists(p),
});

if (tsOutFile) {
main = join(project.root, basename(tsOutFile));
tsOutDir = dirname(tsOutFile);
}
({ rootDir = project.root, outDir: outputPath } = tsconfig.options);
const tsOutFile = tsconfig.options.outFile;

if (tsOutFile) {
main = join(project.root, basename(tsOutFile));
outputPath = dirname(tsOutFile);
}

if (!tsOutDir) {
tsOutDir = project.root;
if (!outputPath) {
outputPath = project.root;
}
}

const mainName = basename(main).replace(/\.[tj]s$/, '');
const mainDir = dirname(main);
const relativeMainDir = relative(
join(tree.root, tsRootDir),
join(tree.root, rootDir),
join(tree.root, mainDir)
);
const relativeOutputPath = relative(
join(tree.root, project.root),
join(tree.root, tsOutDir)
join(tree.root, outputPath)
);
const outputDir = `./${join(relativeOutputPath, relativeMainDir)}`;

const packageJsonPath = joinPathFragments(project.root, 'package.json');
if (tree.exists(packageJsonPath)) {
if (!relativeOutputPath.startsWith('../')) {
// the output is contained within the project root, we ensure the main
// and types fields are set accordingly
updateJson(tree, packageJsonPath, (json) => {
json.main = `${outputDir}/${mainName}.js`;
json.types = `${outputDir}/${mainName}.d.ts`;

return json;
});
}
} else {
const importPath = getImportPath(tree, options.project);
const packageJson: PackageJson = {
name: importPath,
version: '0.0.1',
};
if (!relativeOutputPath.startsWith('../')) {
// the output is contained within the project root, we ensure the main
// and types fields are set accordingly
packageJson.main = `${outputDir}/${mainName}.js`;
packageJson.types = `${outputDir}/${mainName}.d.ts`;
}

writeJson(tree, packageJsonPath, packageJson);
}
updatePackageJson(
tree,
packageJsonPath,
options.project,
outputDir,
mainName,
relativeOutputPath
);
}

function updatePackageJsonForSwc(
tree: Tree,
options: SetupBuildGeneratorSchema,
project: ProjectConfiguration
) {
const { main, outputPath } = project.targets[options.buildTarget].options;
const mergedTarget = mergeTargetDefaults(tree, project, options.buildTarget);
const { main, outputPath } = mergedTarget.options;
const mainName = basename(main).replace(/\.[tj]s$/, '');
const relativeOutputPath = relative(
join(tree.root, project.root),
Expand All @@ -292,6 +292,25 @@ function updatePackageJsonForSwc(
const outputDir = `./${normalize(relativeOutputPath)}`;

const packageJsonPath = joinPathFragments(project.root, 'package.json');
updatePackageJson(
tree,
packageJsonPath,
options.project,
outputDir,
mainName,
relativeOutputPath
);
}

function updatePackageJson(
tree: Tree,
packageJsonPath: string,
projectName: string,
outputDir: string,
mainName: string,
relativeOutputPath: string
) {
// TODO(tsc): consider add exports and use module
if (tree.exists(packageJsonPath)) {
if (!relativeOutputPath.startsWith('../')) {
// the output is contained within the project root, we ensure the main
Expand All @@ -302,8 +321,8 @@ function updatePackageJsonForSwc(
return json;
});
}
} else if (!tree.exists(packageJsonPath)) {
const importPath = getImportPath(tree, options.project);
} else {
const importPath = getImportPath(tree, projectName);
const packageJson: PackageJson = {
name: importPath,
version: '0.0.1',
Expand All @@ -318,3 +337,19 @@ function updatePackageJsonForSwc(
writeJson(tree, packageJsonPath, packageJson);
}
}

function mergeTargetDefaults(
tree: Tree,
project: ProjectConfiguration,
buildTarget: string
) {
const nxJson = readNxJson(tree);
const projectTarget = project.targets[buildTarget];

return mergeTargetConfigurations(
projectTarget,
(projectTarget.executor
? nxJson.targetDefaults?.[projectTarget.executor]
: undefined) ?? nxJson.targetDefaults?.[buildTarget]
);
}
Loading

0 comments on commit ed18928

Please sign in to comment.