Skip to content

Commit

Permalink
chore(repo): require exports entry
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Dec 18, 2024
1 parent 6bf9063 commit cc6251a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
33 changes: 32 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,38 @@
"conformance": {
"rules": [
{
"rule": "@nx/workspace-plugin/conformance-rules/project-package-json"
"rule": "@nx/workspace-plugin/conformance-rules/project-package-json",
"projects": [
"!.",
"!create-nx-*",
"!cypress",
"!detox",
"!devkit",
"!esbuild",
"!eslint-plugin",
"!eslint",
"!expo",
"!express",
"!jest",
"!js",
"!module-federation",
"!nest",
"!next",
"!node",
"!nuxt",
"!packages/nx/**",
"!plugin",
"!react-native",
"!react",
"!rollup",
"!rsbuild",
"!rspack",
"!storybook",
"!vue",
"!web",
"!webpack",
"!workspace"
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const VALID_PACKAGE_JSON_BASE = {
publishConfig: {
access: 'public',
},
exports: {
'./package.json': './package.json',
},
};

describe('project-package-json', () => {
Expand Down Expand Up @@ -257,5 +260,30 @@ describe('project-package-json', () => {
]
`);
});

it('should return a violation if the project does not specify an exports object in the package.json', () => {
const sourceProject = 'test-project';
const sourceProjectRoot = '/path/to/test-project';

expect(
validateProjectPackageJson(
{
...VALID_PACKAGE_JSON_BASE,
exports: undefined,
},
sourceProject,
sourceProjectRoot,
`${sourceProjectRoot}/package.json`
)
).toMatchInlineSnapshot(`
[
{
"file": "/path/to/test-project/package.json",
"message": "The project package.json should have an "exports" object specified",
"sourceProject": "test-project",
},
]
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export function validateProjectPackageJson(
): ProjectFilesViolation[] {
const violations: ProjectFilesViolation[] = [];

// Private packages are exempt from this rule
if (projectPackageJson.private === true) {
return [];
}

if (typeof projectPackageJson.name !== 'string') {
violations.push({
message: 'The project package.json should have a "name" field',
Expand All @@ -71,16 +76,14 @@ export function validateProjectPackageJson(
}
}

// Public packages
if (!projectPackageJson.private) {
if ((projectPackageJson.publishConfig as any)?.access !== 'public') {
violations.push({
message:
'Public packages should have "publishConfig": { "access": "public" } set in their package.json',
sourceProject,
file: projectPackageJsonPath,
});
}
// Publish config
if ((projectPackageJson.publishConfig as any)?.access !== 'public') {
violations.push({
message:
'Public packages should have "publishConfig": { "access": "public" } set in their package.json',
sourceProject,
file: projectPackageJsonPath,
});
}

// Nx config properties
Expand All @@ -105,5 +108,18 @@ export function validateProjectPackageJson(
}
}

const hasExportsEntries =
typeof projectPackageJson.exports === 'object' &&
Object.keys(projectPackageJson.exports ?? {}).length > 0;

if (!hasExportsEntries) {
violations.push({
message:
'The project package.json should have an "exports" object specified',
sourceProject,
file: projectPackageJsonPath,
});
}

return violations;
}

0 comments on commit cc6251a

Please sign in to comment.