diff --git a/src/compiler/types/tests/validate-package-json.spec.ts b/src/compiler/types/tests/validate-package-json.spec.ts index dd3e0ae4d38..2ab26c50b1a 100644 --- a/src/compiler/types/tests/validate-package-json.spec.ts +++ b/src/compiler/types/tests/validate-package-json.spec.ts @@ -79,14 +79,36 @@ describe('validate-package-json', () => { }); describe('module', () => { - it('validate module', async () => { + it('validate dist module', async () => { + config.outputTargets = []; compilerCtx.fs.writeFile(path.join(root, 'dist', 'index.js'), ''); buildCtx.packageJson.module = 'dist/index.js'; v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget); expect(buildCtx.diagnostics).toHaveLength(0); }); - it('missing module', async () => { + it('validate custom elements module', async () => { + config.outputTargets = [{ + type: 'dist-custom-elements-bundle', + dir: path.join(root, 'custom-elements') + }]; + compilerCtx.fs.writeFile(path.join(root, 'dist', 'index.js'), ''); + buildCtx.packageJson.module = 'custom-elements/index.js'; + v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget); + expect(buildCtx.diagnostics).toHaveLength(0); + }); + + it('missing dist module', async () => { + config.outputTargets = []; + v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget); + expect(buildCtx.diagnostics).toHaveLength(1); + }); + + it('missing dist module, but has custom elements output', async () => { + config.outputTargets = [{ + type: 'dist-custom-elements-bundle', + dir: path.join(root, 'custom-elements') + }]; v.validateModule(config, compilerCtx, buildCtx, collectionOutputTarget); expect(buildCtx.diagnostics).toHaveLength(1); }); diff --git a/src/compiler/types/validate-build-package-json.ts b/src/compiler/types/validate-build-package-json.ts index d9ba2878b23..597cf27da27 100644 --- a/src/compiler/types/validate-build-package-json.ts +++ b/src/compiler/types/validate-build-package-json.ts @@ -1,7 +1,7 @@ import type * as d from '../../declarations'; import { COLLECTION_MANIFEST_FILE_NAME, buildJsonFileError, isGlob, normalizePath, isString } from '@utils'; import { dirname, join, relative } from 'path'; -import { getComponentsDtsTypesFilePath, isOutputTargetDistCollection, isOutputTargetDistTypes } from '../output-targets/output-utils'; +import { getComponentsDtsTypesFilePath, isOutputTargetDistCollection, isOutputTargetDistCustomElementsBundle, isOutputTargetDistTypes } from '../output-targets/output-utils'; export const validateBuildPackageJson = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => { if (config.watch) { @@ -78,14 +78,22 @@ export const validateMain = (config: d.Config, compilerCtx: d.CompilerCtx, build }; export const validateModule = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCollection) => { - const moduleAbs = join(outputTarget.dir, 'index.js'); - const moduleRel = relative(config.rootDir, moduleAbs); + const customElementsOutput = config.outputTargets.find(isOutputTargetDistCustomElementsBundle); + const currentModule = buildCtx.packageJson.module; + const distAbs = join(outputTarget.dir, 'index.js'); + const distRel = relative(config.rootDir, distAbs); + + let recommendedRelPath = distRel; + if (customElementsOutput) { + const customElementsAbs = join(customElementsOutput.dir, 'index.js'); + recommendedRelPath = relative(config.rootDir, customElementsAbs); + } - if (!isString(buildCtx.packageJson.module)) { - const msg = `package.json "module" property is required when generating a distribution. It's recommended to set the "module" property to: ${moduleRel}`; + if (!isString(currentModule)) { + const msg = `package.json "module" property is required when generating a distribution. It's recommended to set the "module" property to: ${recommendedRelPath}`; packageJsonWarn(config, compilerCtx, buildCtx, msg, `"module"`); - } else if (normalizePath(buildCtx.packageJson.module) !== normalizePath(moduleRel)) { - const msg = `package.json "module" property is set to "${buildCtx.packageJson.module}". It's recommended to set the "module" property to: ${moduleRel}`; + } else if (normalizePath(currentModule) !== normalizePath(recommendedRelPath) && normalizePath(currentModule) !== normalizePath(distRel)) { + const msg = `package.json "module" property is set to "${currentModule}". It's recommended to set the "module" property to: ${recommendedRelPath}`; packageJsonWarn(config, compilerCtx, buildCtx, msg, `"module"`); } };