Skip to content

Commit

Permalink
fix(custom-elements): update package json module recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Aug 31, 2020
1 parent c5d216f commit 9f29dbd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
26 changes: 24 additions & 2 deletions src/compiler/types/tests/validate-package-json.spec.ts
Expand Up @@ -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);
});
Expand Down
22 changes: 15 additions & 7 deletions 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) {
Expand Down Expand Up @@ -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"`);
}
};
Expand Down

0 comments on commit 9f29dbd

Please sign in to comment.