Skip to content

Commit

Permalink
fix(nx-plugin): generated root plugin should not have wonky paths (nr…
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Apr 21, 2023
1 parent a908ef5 commit a2e1337
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
16 changes: 11 additions & 5 deletions e2e/workspace-create/src/create-nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ describe('create-nx-plugin', () => {

runCLI(`build ${pluginName}`);

checkFilesExist(`dist/package.json`, `dist/src/index.js`);
checkFilesExist(
`dist/${pluginName}/package.json`,
`dist/${pluginName}/src/index.js`
);

runCLI(
`generate @nrwl/nx-plugin:generator ${generatorName} --project=${pluginName}`
Expand All @@ -44,9 +47,9 @@ describe('create-nx-plugin', () => {
runCLI(`build ${pluginName}`);

checkFilesExist(
`dist/package.json`,
`dist/generators.json`,
`dist/executors.json`
`dist/${pluginName}/package.json`,
`dist/${pluginName}/generators.json`,
`dist/${pluginName}/executors.json`
);
});

Expand All @@ -59,7 +62,10 @@ describe('create-nx-plugin', () => {
});

runCLI(`build ${pluginName}`);
checkFilesExist(`dist/package.json`, `dist/generators.json`);
checkFilesExist(
`dist/${pluginName}/package.json`,
`dist/${pluginName}/generators.json`
);

runCLI(`build create-${pluginName}-package`);
checkFilesExist(`dist/create-${pluginName}-package/bin/index.js`);
Expand Down
17 changes: 14 additions & 3 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ function addProject(
options.bundler !== 'none' &&
options.config !== 'npm-scripts'
) {
const outputPath = destinationDir
? `dist/${destinationDir}/${options.projectDirectory}`
: `dist/${options.projectDirectory}`;
const outputPath = getOutputPath(options, destinationDir);
projectConfiguration.targets.build = {
executor: getBuildExecutor(options.bundler),
outputs: ['{options.outputPath}'],
Expand Down Expand Up @@ -580,5 +578,18 @@ function ensureBabelRootConfigExists(tree: Tree) {
});
}

function getOutputPath(options: NormalizedSchema, destinationDir?: string) {
const parts = ['dist'];
if (destinationDir) {
parts.push(destinationDir);
}
if (options.projectDirectory === '.') {
parts.push(options.name);
} else {
parts.push(options.projectDirectory);
}
return joinPathFragments(...parts);
}

export default libraryGenerator;
export const librarySchematic = convertNxGenerator(libraryGenerator);
13 changes: 4 additions & 9 deletions packages/nx/src/project-graph/nx-deps-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { ProjectGraph } from '../config/project-graph';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import { NxJsonConfiguration } from '../config/nx-json';
import { nxVersion } from '../utils/versions';

describe('nx deps utils', () => {
describe('shouldRecomputeWholeGraph', () => {
Expand Down Expand Up @@ -34,14 +35,11 @@ describe('nx deps utils', () => {
).toEqual(true);
});

it('should be true when version of nrwl/workspace changes', () => {
it('should be true when version of nx changes', () => {
expect(
shouldRecomputeWholeGraph(
createCache({
deps: {
'@nx/workspace': '12.0.1',
plugin: '1.0.0',
},
nxVersion: '12.0.1',
}),
createPackageJsonDeps({}),
createProjectsConfiguration({}),
Expand Down Expand Up @@ -317,10 +315,7 @@ describe('nx deps utils', () => {
function createCache(p: Partial<ProjectGraphCache>): ProjectGraphCache {
const defaults: ProjectGraphCache = {
version: '5.1',
deps: {
'@nx/workspace': '12.0.0',
plugin: '1.0.0',
},
nxVersion: nxVersion,
pathMappings: {
mylib: ['libs/mylib/index.ts'],
},
Expand Down
13 changes: 5 additions & 8 deletions packages/nx/src/project-graph/nx-deps-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import {
readJsonFile,
writeJsonFile,
} from '../utils/fileutils';
import { nxVersion } from '../utils/versions';

export interface ProjectGraphCache {
version: string;
nxVersion: string;
deps: Record<string, string>;
pathMappings: Record<string, any>;
nxJsonPlugins: { name: string; version: string }[];
Expand Down Expand Up @@ -93,7 +95,8 @@ export function createCache(
}));
const newValue: ProjectGraphCache = {
version: projectGraph.version || '5.1',
deps: packageJsonDeps,
nxVersion: nxVersion,
deps: packageJsonDeps, // TODO(v18): We can remove this in favor of nxVersion
// compilerOptions may not exist, especially for package-based repos
pathMappings: tsConfig?.compilerOptions?.paths || {},
nxJsonPlugins,
Expand Down Expand Up @@ -149,13 +152,7 @@ export function shouldRecomputeWholeGraph(
if (cache.version !== '5.1') {
return true;
}
if (
cache.deps['@nx/workspace'] !== packageJsonDeps['@nx/workspace'] ||
cache.deps['@nrwl/workspace'] !== packageJsonDeps['@nrwl/workspace']
) {
return true;
}
if (cache.deps['nx'] !== packageJsonDeps['nx']) {
if (cache.nxVersion !== nxVersion) {
return true;
}

Expand Down
10 changes: 6 additions & 4 deletions packages/plugin/src/generators/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,27 @@ function updatePluginConfig(host: Tree, options: NormalizedSchema) {
if (project.targets.build) {
project.targets.build.options.assets ??= [];

const root =
options.projectRoot === '.' ? './' : './' + options.projectRoot;
project.targets.build.options.assets = [
...project.targets.build.options.assets,
{
input: `./${options.projectRoot}/src`,
input: `${root}/src`,
glob: '**/!(*.ts)',
output: './src',
},
{
input: `./${options.projectRoot}/src`,
input: `${root}/src`,
glob: '**/*.d.ts',
output: './src',
},
{
input: `./${options.projectRoot}`,
input: root,
glob: 'generators.json',
output: '.',
},
{
input: `./${options.projectRoot}`,
input: root,
glob: 'executors.json',
output: '.',
},
Expand Down

0 comments on commit a2e1337

Please sign in to comment.