Skip to content

Commit f27dd6b

Browse files
committed
fix(build): fix validate package json for prod builds
1 parent f32598b commit f27dd6b

7 files changed

Lines changed: 56 additions & 46 deletions

File tree

src/compiler/build/validate-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { validateManifestJson } from '../html/validate-manifest-json';
33
import { validatePackageJson } from '../types/validate-package-json';
44

55

6-
export function validateFiles(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {
6+
export function validateBuildFiles(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {
77
if (buildCtx.hasError) {
88
return null;
99
}

src/compiler/build/write-build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as d from '../../declarations';
22
import { catchError } from '@utils';
33
import { outputPrerender } from '../output-targets/output-prerender';
44
import { outputServiceWorkers } from '../output-targets/output-service-workers';
5-
import { validateFiles as validateBuildFiles } from './validate-files';
5+
import { validateBuildFiles } from './validate-files';
66

77

88
export async function writeBuildFiles(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {

src/compiler/html/validate-manifest-json.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import * as d from '../../declarations';
2-
import { buildError, buildJsonFileWarn } from '@utils';
2+
import { buildError, buildJsonFileError } from '@utils';
33
import { isOutputTargetWww } from '../output-targets/output-utils';
44

55

66
export function validateManifestJson(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) {
7+
if (config.devMode) {
8+
return null;
9+
}
10+
711
const outputTargets = config.outputTargets.filter(isOutputTargetWww);
812

913
return Promise.all(outputTargets.map(async outputsTarget => {
@@ -40,7 +44,7 @@ async function validateManifestJsonIcon(config: d.Config, compilerCtx: d.Compile
4044
let iconSrc = manifestIcon.src;
4145
if (typeof iconSrc !== 'string') {
4246
const msg = `Manifest icon missing "src"`;
43-
buildJsonFileWarn(compilerCtx, buildCtx.diagnostics, manifestFilePath, msg, `"icons"`);
47+
buildJsonFileError(compilerCtx, buildCtx.diagnostics, manifestFilePath, msg, `"icons"`);
4448
return;
4549
}
4650

@@ -53,6 +57,6 @@ async function validateManifestJsonIcon(config: d.Config, compilerCtx: d.Compile
5357
const hasAccess = await compilerCtx.fs.access(iconPath);
5458
if (!hasAccess) {
5559
const msg = `Unable to find manifest icon "${manifestIcon.src}"`;
56-
buildJsonFileWarn(compilerCtx, buildCtx.diagnostics, manifestFilePath, msg, `"${manifestIcon.src}"`);
60+
buildJsonFileError(compilerCtx, buildCtx.diagnostics, manifestFilePath, msg, `"${manifestIcon.src}"`);
5761
}
5862
}

src/compiler/transpile/transpile-app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ async function processMetadata(config: d.Config, compilerCtx: d.CompilerCtx, bui
4646
if (!config._isTesting) {
4747
// now that we've updated the components.d.ts file
4848
// lets do a full typescript build (but in another thread)
49-
validateTypesMain(config, compilerCtx, buildCtx);
49+
validateTypesMain(config, compilerCtx, buildCtx).catch(err => {
50+
catchError(buildCtx.diagnostics, err);
51+
});
5052
}
5153
}
5254
}

src/compiler/types/tests/validate-package-json.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('validate-package-json', () => {
2020
copy: []
2121
};
2222
config = mockConfig();
23+
config.devMode = false;
2324
config.namespace = 'SomeNamespace';
2425
config.fsNamespace = config.namespace.toLowerCase();
2526
compilerCtx = mockCompilerCtx();
@@ -117,8 +118,7 @@ describe('validate-package-json', () => {
117118
it('validate types', async () => {
118119
compilerCtx.fs.writeFile(path.join(root, 'dist', 'types', 'components.d.ts'), '');
119120
buildCtx.packageJson.types = 'dist/types/components.d.ts';
120-
v.validateTypes(config, compilerCtx, buildCtx, outputTarget);
121-
await v.validateTypesExist(config, compilerCtx, buildCtx, outputTarget);
121+
await v.validateTypes(config, compilerCtx, buildCtx, outputTarget);
122122
expect(buildCtx.diagnostics).toHaveLength(0);
123123
});
124124

@@ -131,8 +131,7 @@ describe('validate-package-json', () => {
131131

132132
it('missing types file', async () => {
133133
buildCtx.packageJson.types = 'dist/types/components.d.ts';
134-
v.validateTypes(config, compilerCtx, buildCtx, outputTarget);
135-
await v.validateTypesExist(config, compilerCtx, buildCtx, outputTarget);
134+
await v.validateTypes(config, compilerCtx, buildCtx, outputTarget);
136135
expect(buildCtx.diagnostics).toHaveLength(1);
137136
});
138137

src/compiler/types/validate-package-json.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as d from '../../declarations';
2-
import { COLLECTION_MANIFEST_FILE_NAME, buildJsonFileWarn, normalizePath } from '@utils';
2+
import { COLLECTION_MANIFEST_FILE_NAME, buildJsonFileError, normalizePath } from '@utils';
33
import { getComponentsDtsTypesFilePath, isOutputTargetDistCollection } from '../output-targets/output-utils';
44

55

@@ -23,14 +23,13 @@ function validatePackageJsonOutput(config: d.Config, compilerCtx: d.CompilerCtx,
2323
validateModule(config, compilerCtx, buildCtx, outputTarget),
2424
validateCollection(config, compilerCtx, buildCtx, outputTarget),
2525
validateTypes(config, compilerCtx, buildCtx, outputTarget),
26-
validateTypesExist(config, compilerCtx, buildCtx, outputTarget),
2726
validateBrowser(compilerCtx, buildCtx)
2827
]);
2928
}
3029

3130

3231
export async function validatePackageFiles(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCollection) {
33-
if (Array.isArray(buildCtx.packageJson.files)) {
32+
if (!config.devMode && Array.isArray(buildCtx.packageJson.files)) {
3433
const actualDistDir = normalizePath(config.sys.path.relative(config.rootDir, outputTarget.dir));
3534

3635
const validPaths = [
@@ -52,10 +51,11 @@ export async function validatePackageFiles(config: d.Config, compilerCtx: d.Comp
5251
await Promise.all(buildCtx.packageJson.files.map(async pkgFile => {
5352
const packageJsonDir = config.sys.path.dirname(buildCtx.packageJsonFilePath);
5453
const absPath = config.sys.path.join(packageJsonDir, pkgFile);
54+
5555
const hasAccess = await compilerCtx.fs.access(absPath);
5656
if (!hasAccess) {
5757
const msg = `Unable to find "${pkgFile}" within the package.json "files" array.`;
58-
packageJsonWarn(compilerCtx, buildCtx, msg, `"${pkgFile}"`);
58+
packageJsonError(compilerCtx, buildCtx, msg, `"${pkgFile}"`);
5959
}
6060
}));
6161
}
@@ -92,7 +92,11 @@ export function validateModule(config: d.Config, compilerCtx: d.CompilerCtx, bui
9292
}
9393

9494

95-
export function validateTypes(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCollection) {
95+
export async function validateTypes(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCollection) {
96+
if (config.devMode) {
97+
return;
98+
}
99+
96100
if (typeof buildCtx.packageJson.types !== 'string' || buildCtx.packageJson.types === '') {
97101
const recommendedPath = getRecommendedTypesPath(config, outputTarget);
98102
const msg = `package.json "types" property is required when generating a distribution. It's recommended to set the "types" property to: ${recommendedPath}`;
@@ -101,24 +105,18 @@ export function validateTypes(config: d.Config, compilerCtx: d.CompilerCtx, buil
101105
} else if (!buildCtx.packageJson.types.endsWith('.d.ts')) {
102106
const msg = `package.json "types" file must have a ".d.ts" extension: ${buildCtx.packageJson.types}`;
103107
packageJsonWarn(compilerCtx, buildCtx, msg, `"types"`);
104-
}
105-
}
106108

107-
108-
export async function validateTypesExist(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, outputTarget: d.OutputTargetDistCollection) {
109-
if (typeof buildCtx.packageJson.types !== 'string') {
110-
return;
111-
}
112-
113-
const pkgFile = config.sys.path.join(config.rootDir, buildCtx.packageJson.types);
114-
const fileExists = await compilerCtx.fs.access(pkgFile);
115-
if (!fileExists) {
116-
const recommendedPath = getRecommendedTypesPath(config, outputTarget);
117-
let msg = `package.json "types" property is set to "${buildCtx.packageJson.types}" but cannot be found.`;
118-
if (buildCtx.packageJson.types !== recommendedPath) {
119-
msg += ` It's recommended to set the "types" property to: ${recommendedPath}`;
109+
} else {
110+
const typesFile = config.sys.path.join(config.rootDir, buildCtx.packageJson.types);
111+
const typesFileExists = await compilerCtx.fs.access(typesFile);
112+
if (!typesFileExists) {
113+
const recommendedPath = getRecommendedTypesPath(config, outputTarget);
114+
let msg = `package.json "types" property is set to "${buildCtx.packageJson.types}" but cannot be found.`;
115+
if (buildCtx.packageJson.types !== recommendedPath) {
116+
msg += ` It's recommended to set the "types" property to: ${recommendedPath}`;
117+
}
118+
packageJsonError(compilerCtx, buildCtx, msg, `"types"`);
120119
}
121-
packageJsonWarn(compilerCtx, buildCtx, msg, `"types"`);
122120
}
123121
}
124122

@@ -148,8 +146,15 @@ export function getRecommendedTypesPath(config: d.Config, outputTarget: d.Output
148146
}
149147

150148

149+
function packageJsonError(compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, msg: string, warnKey: string) {
150+
const err = buildJsonFileError(compilerCtx, buildCtx.diagnostics, buildCtx.packageJsonFilePath, msg, warnKey);
151+
err.header = `Package Json`;
152+
return err;
153+
}
154+
151155
function packageJsonWarn(compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, msg: string, warnKey: string) {
152-
const warn = buildJsonFileWarn(compilerCtx, buildCtx.diagnostics, buildCtx.packageJsonFilePath, msg, warnKey);
156+
const warn = buildJsonFileError(compilerCtx, buildCtx.diagnostics, buildCtx.packageJsonFilePath, msg, warnKey);
153157
warn.header = `Package Json`;
158+
warn.level = 'warn';
154159
return warn;
155160
}

src/utils/message-utils.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function buildWarn(diagnostics: d.Diagnostic[]) {
2222
const diagnostic: d.Diagnostic = {
2323
level: 'warn',
2424
type: 'build',
25-
header: 'build warn',
25+
header: 'Build Warn',
2626
messageText: 'build warn',
2727
relFilePath: null,
2828
absFilePath: null,
@@ -35,31 +35,31 @@ export function buildWarn(diagnostics: d.Diagnostic[]) {
3535
}
3636

3737

38-
export function buildJsonFileWarn(compilerCtx: d.CompilerCtx, diagnostics: d.Diagnostic[], jsonFilePath: string, msg: string, warnKey: string) {
39-
const warn = buildWarn(diagnostics);
40-
warn.messageText = msg;
41-
warn.absFilePath = jsonFilePath;
38+
export function buildJsonFileError(compilerCtx: d.CompilerCtx, diagnostics: d.Diagnostic[], jsonFilePath: string, msg: string, pkgKey: string) {
39+
const err = buildError(diagnostics);
40+
err.messageText = msg;
41+
err.absFilePath = jsonFilePath;
4242

43-
if (typeof warnKey === 'string') {
43+
if (typeof pkgKey === 'string') {
4444
try {
4545
const jsonStr = compilerCtx.fs.readFileSync(jsonFilePath);
4646
const lines = jsonStr.replace(/\r/g, '\n').split('\n');
4747

4848
for (let i = 0; i < lines.length; i++) {
4949
const txtLine = lines[i];
50-
const txtIndex = txtLine.indexOf(warnKey);
50+
const txtIndex = txtLine.indexOf(pkgKey);
5151

5252
if (txtIndex > -1) {
5353
const warnLine: d.PrintLine = {
5454
lineIndex: i,
5555
lineNumber: i + 1,
5656
text: txtLine,
5757
errorCharStart: txtIndex,
58-
errorLength: warnKey.length
58+
errorLength: pkgKey.length
5959
};
60-
warn.lineNumber = warnLine.lineNumber;
61-
warn.columnNumber = txtIndex + 1;
62-
warn.lines.push(warnLine);
60+
err.lineNumber = warnLine.lineNumber;
61+
err.columnNumber = txtIndex + 1;
62+
err.lines.push(warnLine);
6363

6464
if (i >= 0) {
6565
const beforeWarnLine: d.PrintLine = {
@@ -69,7 +69,7 @@ export function buildJsonFileWarn(compilerCtx: d.CompilerCtx, diagnostics: d.Dia
6969
errorCharStart: -1,
7070
errorLength: -1
7171
};
72-
warn.lines.unshift(beforeWarnLine);
72+
err.lines.unshift(beforeWarnLine);
7373
}
7474

7575
if (i < lines.length) {
@@ -80,7 +80,7 @@ export function buildJsonFileWarn(compilerCtx: d.CompilerCtx, diagnostics: d.Dia
8080
errorCharStart: -1,
8181
errorLength: -1
8282
};
83-
warn.lines.push(afterWarnLine);
83+
err.lines.push(afterWarnLine);
8484
}
8585

8686
break;
@@ -89,7 +89,7 @@ export function buildJsonFileWarn(compilerCtx: d.CompilerCtx, diagnostics: d.Dia
8989
} catch (e) {}
9090
}
9191

92-
return warn;
92+
return err;
9393
}
9494

9595

0 commit comments

Comments
 (0)