Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): normalize recommended collection path for package.json validation #4522

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/compiler/types/tests/validate-package-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type * as d from '@stencil/core/declarations';
import { mockBuildCtx, mockCompilerCtx, mockValidatedConfig } from '@stencil/core/testing';
import { normalizePath } from '@utils';
import path from 'path';

import * as v from '../validate-build-package-json';
Expand Down Expand Up @@ -99,9 +100,33 @@ describe('validate-package-json', () => {
});

describe('collection', () => {
it('should error when missing collection property', async () => {
it('should produce a warning when missing collection property', async () => {
v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);

expect(buildCtx.diagnostics[0].messageText).toMatch(/package.json "collection" property is required/);
expect(buildCtx.diagnostics[0].level).toBe('warn');
});

it('should produce a warning if the supplied path does not match the recommended path', () => {
buildCtx.packageJson.collection = 'bad/path';

v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);

expect(buildCtx.diagnostics[0].messageText).toBe(
`package.json "collection" property is required when generating a distribution and must be set to: ${normalizePath(
'dist/collection/collection-manifest.json',
false
)}`
);
expect(buildCtx.diagnostics[0].level).toBe('warn');
});

it('should not produce a warning if the normalized paths are the same', () => {
buildCtx.packageJson.collection = './dist/collection/collection-manifest.json';

v.validateCollection(config, compilerCtx, buildCtx, collectionOutputTarget);

expect(buildCtx.diagnostics.length).toEqual(0);
});
});
});
7 changes: 5 additions & 2 deletions src/compiler/types/validate-build-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ export const validateCollection = (
outputTarget: d.OutputTargetDistCollection
) => {
if (outputTarget.collectionDir) {
const collectionRel = join(relative(config.rootDir, outputTarget.collectionDir), COLLECTION_MANIFEST_FILE_NAME);
if (!buildCtx.packageJson.collection || normalizePath(buildCtx.packageJson.collection) !== collectionRel) {
const collectionRel = normalizePath(
join(relative(config.rootDir, outputTarget.collectionDir), COLLECTION_MANIFEST_FILE_NAME),
false
);
if (!buildCtx.packageJson.collection || normalizePath(buildCtx.packageJson.collection, false) !== collectionRel) {
const msg = `package.json "collection" property is required when generating a distribution and must be set to: ${collectionRel}`;
packageJsonWarn(config, compilerCtx, buildCtx, msg, `"collection"`);
}
Expand Down