Skip to content

Commit

Permalink
feat(js): update rollup and vite output path to be contained in the p…
Browse files Browse the repository at this point in the history
…roject
  • Loading branch information
leosvelperez committed Sep 20, 2024
1 parent a26383c commit 44af27a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 51 deletions.
5 changes: 4 additions & 1 deletion packages/js/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { readModulePackageJson } from 'nx/src/utils/package-json';
import { join } from 'path';
import { satisfies, valid } from 'semver';
import { createNodesV2 } from '../../plugins/typescript/plugin';
import { isUsingTypeScriptPlugin } from '../../utils/typescript-plugin';
import { getRootTsConfigFileName } from '../../utils/typescript/ts-config';
import {
nxVersion,
Expand Down Expand Up @@ -68,9 +69,11 @@ export async function initGenerator(
tree: Tree,
schema: InitSchema
): Promise<GeneratorCallback> {
const isUsingTsPlugin = isUsingTypeScriptPlugin(tree);
schema.formatter ??= isUsingTsPlugin ? 'none' : 'prettier';

return initGeneratorInternal(tree, {
addTsConfigBase: true,
formatter: 'prettier',
...schema,
});
}
Expand Down
37 changes: 17 additions & 20 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { dirname, join } from 'path';
import type { CompilerOptions, System } from 'typescript';
import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { getSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { isUsingTypeScriptPlugin } from '../../utils/typescript-plugin';
import { tsConfigBaseOptions } from '../../utils/typescript/create-ts-config';
import { ensureTypescript } from '../../utils/typescript/ensure-typescript';
import {
Expand Down Expand Up @@ -707,21 +708,7 @@ async function normalizeOptions(
tree: Tree,
options: LibraryGeneratorSchema
): Promise<NormalizedLibraryGeneratorOptions> {
const nxJson = readNxJson(tree);

options.addPlugin ??=
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false;
const addTsPlugin =
options.addPlugin && process.env.NX_ADD_TS_PLUGIN === 'true';
// is going to be added or it's already there
const hasPlugin =
addTsPlugin ||
nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/js/typescript'
: p.plugin === '@nx/js/typescript'
);
const hasPlugin = isUsingTypeScriptPlugin(tree);

if (hasPlugin) {
if (options.bundler === 'esbuild' || options.bundler === 'swc') {
Expand Down Expand Up @@ -1292,16 +1279,26 @@ function determineEntryFields(
return {
// Since we're publishing both formats, skip the type field.
// Bundlers or Node will determine the entry point to use.
main: './index.cjs',
module: './index.js',
main: options.isUsingTsSolutionConfig
? './dist/index.cjs'
: './index.cjs',
module: options.isUsingTsSolutionConfig
? './dist/index.js'
: './index.js',
};
case 'vite':
return {
// Since we're publishing both formats, skip the type field.
// Bundlers or Node will determine the entry point to use.
main: './index.js',
module: './index.mjs',
typings: './index.d.ts',
main: options.isUsingTsSolutionConfig
? './dist/index.js'
: './index.js',
module: options.isUsingTsSolutionConfig
? './dist/index.mjs'
: './index.mjs',
typings: options.isUsingTsSolutionConfig
? './dist/index.d.ts'
: './index.d.ts',
};
case 'esbuild':
// For libraries intended for Node, use CJS.
Expand Down
21 changes: 1 addition & 20 deletions packages/js/src/generators/setup-verdaccio/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
generateFiles,
ProjectConfiguration,
readJson,
readNxJson,
TargetConfiguration,
Tree,
updateJson,
} from '@nx/devkit';
import * as path from 'path';
import { SetupVerdaccioGeneratorSchema } from './schema';
import { isUsingTypeScriptPlugin } from '../../utils/typescript-plugin';
import { verdaccioVersion } from '../../utils/versions';
import { execSync } from 'child_process';

Expand Down Expand Up @@ -83,22 +83,3 @@ export async function setupVerdaccio(
}

export default setupVerdaccio;

function isUsingTypeScriptPlugin(tree: Tree) {
const nxJson = readNxJson(tree);

const addPlugin =
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false;
const addTsPlugin = addPlugin && process.env.NX_ADD_TS_PLUGIN === 'true';
// is going to be added or it's already there
const hasPlugin =
addTsPlugin ||
nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/js/typescript'
: p.plugin === '@nx/js/typescript'
);

return hasPlugin;
}
22 changes: 22 additions & 0 deletions packages/js/src/utils/typescript-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readNxJson } from '@nx/devkit';
import type { Tree } from '@nx/devkit';

export function isUsingTypeScriptPlugin(tree: Tree): boolean {
const nxJson = readNxJson(tree);

const addPlugin =
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson?.useInferencePlugins !== false;
const addTsPlugin = addPlugin && process.env.NX_ADD_TS_PLUGIN === 'true';
// is going to be added or it's already there
const hasPlugin =
addTsPlugin ||
(nxJson?.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/js/typescript'
: p.plugin === '@nx/js/typescript'
) ??
false);

return hasPlugin;
}
14 changes: 9 additions & 5 deletions packages/rollup/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
writeJson,
} from '@nx/devkit';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { isUsingTypeScriptPlugin } from '@nx/js/src/utils/typescript-plugin';

import { rollupInitGenerator } from '../init/init';
import { RollupExecutorOptions } from '../../executors/rollup/schema';
Expand Down Expand Up @@ -56,13 +57,16 @@ export async function configurationGenerator(
}

function createRollupConfig(tree: Tree, options: RollupProjectSchema) {
const isUsingTsPlugin = options.addPlugin && isUsingTypeScriptPlugin(tree);
const project = readProjectConfiguration(tree, options.project);
const buildOptions: RollupWithNxPluginOptions = {
outputPath: joinPathFragments(
offsetFromRoot(project.root),
'dist',
project.root === '.' ? project.name : project.root
),
outputPath: isUsingTsPlugin
? './dist'
: joinPathFragments(
offsetFromRoot(project.root),
'dist',
project.root === '.' ? project.name : project.root
),
compiler: options.compiler ?? 'babel',
main: options.main ?? './src/index.ts',
tsConfig: options.tsConfig ?? './tsconfig.lib.json',
Expand Down
15 changes: 10 additions & 5 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
import { isUsingTypeScriptPlugin } from '@nx/js/src/utils/typescript-plugin';
import { ViteBuildExecutorOptions } from '../executors/build/schema';
import { VitePreviewServerExecutorOptions } from '../executors/preview-server/schema';
import { VitestExecutorOptions } from '../executors/test/schema';
import { ViteConfigurationGeneratorSchema } from '../generators/configuration/schema';
import { ensureViteConfigIsCorrect } from './vite-config-edit-utils';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';

export type Target = 'build' | 'serve' | 'test' | 'preview';
export type TargetFlags = Partial<Record<Target, boolean>>;
Expand Down Expand Up @@ -362,10 +363,14 @@ export function createOrEditViteConfig(
? `${projectRoot}/vitest.config.ts`
: `${projectRoot}/vite.config.ts`;

const buildOutDir =
projectRoot === '.'
? `./dist/${options.project}`
: `${offsetFromRoot(projectRoot)}dist/${projectRoot}`;
const isUsingTsPlugin = isUsingTypeScriptPlugin(tree);
const buildOutDir = isUsingTsPlugin
? './dist'
: joinPathFragments(
offsetFromRoot(projectRoot),
'dist',
projectRoot === '.' ? options.project : projectRoot
);

const buildOption = onlyVitest
? ''
Expand Down

0 comments on commit 44af27a

Please sign in to comment.