forked from preconstruct/preconstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.ts
181 lines (169 loc) Β· 5.47 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Project } from "./project";
import { Entrypoint } from "./entrypoint";
import { errors, successes, infos } from "./messages";
import { BatchError, FatalError, FixableError } from "./errors";
import { validFields } from "./utils";
import * as logger from "./logger";
import equal from "fast-deep-equal";
import { validatePackage } from "./validate-package";
import chalk from "chalk";
// this doesn't offer to fix anything
// just does validation
// used in build and watch
export const isFieldValid = {
main(entrypoint: Entrypoint) {
return entrypoint.json.main === validFields.main(entrypoint);
},
module(entrypoint: Entrypoint) {
return entrypoint.json.module === validFields.module(entrypoint);
},
"umd:main"(entrypoint: Entrypoint) {
return entrypoint.json["umd:main"] === validFields["umd:main"](entrypoint);
},
browser(entrypoint: Entrypoint): boolean {
return equal(entrypoint.json.browser, validFields.browser(entrypoint));
},
worker(entrypoint: Entrypoint): boolean {
return equal(entrypoint.json.worker, validFields.worker(entrypoint));
},
};
export function isUmdNameSpecified(entrypoint: Entrypoint) {
return typeof entrypoint.json.preconstruct.umdName === "string";
}
let projectsShownOldDistNamesInfo = new WeakSet<Project>();
function validateEntrypoint(entrypoint: Entrypoint, log: boolean) {
if (log) {
logger.info(infos.validEntrypoint, entrypoint.name);
}
const fatalErrors: FatalError[] = [];
for (const field of [
"main",
"module",
"umd:main",
"browser",
"worker",
] as const) {
if (field !== "main" && entrypoint.json[field] === undefined) {
continue;
}
if (!isFieldValid[field](entrypoint)) {
let isUsingOldDistFilenames =
validFields[field](entrypoint, "unscoped-package-name") ===
entrypoint.json[field];
if (
isUsingOldDistFilenames &&
!projectsShownOldDistNamesInfo.has(entrypoint.package.project)
) {
projectsShownOldDistNamesInfo.add(entrypoint.package.project);
logger.info(
`it looks like you're using the dist filenames of Preconstruct v1, the default dist filename strategy has changed in v2`
);
logger.info(
`you can run ${chalk.green(
"preconstruct fix"
)} to use the new dist filenames`
);
logger.info(
'if you want to keep the dist filename strategy of v1, add `"distFilenameStrategy": "unscoped-package-name"` to the Preconstruct config in your root package.json'
);
}
fatalErrors.push(
// they're both fixable but we don't want the message about running preconstruct fix if they're using the old dist file names since we have a custom message
new (isUsingOldDistFilenames ? FatalError : FixableError)(
errors.invalidField(
field,
entrypoint.json[field],
validFields[field](entrypoint)
),
entrypoint.name
)
);
}
if (field === "umd:main" && !isUmdNameSpecified(entrypoint)) {
fatalErrors.push(
new FixableError(errors.umdNameNotSpecified, entrypoint.name)
);
}
if (log && !fatalErrors.length) {
logger.info(infos.validField(field), entrypoint.name);
}
}
if (fatalErrors.length) {
throw new BatchError(fatalErrors);
}
}
export const FORMER_FLAGS_THAT_ARE_ENABLED_NOW = new Set<string>([
"newEntrypoints",
"newDistFilenames",
"newProcessEnvNodeEnvReplacementStrategyAndSkipTerserOnCJSProdBuild",
]);
export const EXPERIMENTAL_FLAGS = new Set([
"logCompiledFiles",
"typeScriptProxyFileWithImportEqualsRequireAndExportEquals",
"keepDynamicImportAsDynamicImportInCommonJS",
]);
export function validateProject(project: Project, log = false) {
let errors: FatalError[] = [];
if (project.json.preconstruct.___experimentalFlags_WILL_CHANGE_IN_PATCH) {
Object.keys(
project.json.preconstruct.___experimentalFlags_WILL_CHANGE_IN_PATCH
).forEach((key) => {
if (FORMER_FLAGS_THAT_ARE_ENABLED_NOW.has(key)) {
errors.push(
new FixableError(
`The behaviour from the experimental flag ${JSON.stringify(
key
)} is the current behaviour now, the flag should be removed`,
project.name
)
);
} else if (!EXPERIMENTAL_FLAGS.has(key)) {
errors.push(
new FatalError(
`The experimental flag ${JSON.stringify(
key
)} in your config does not exist`,
project.name
)
);
}
});
}
for (let pkg of project.packages) {
try {
validatePackage(pkg);
} catch (err) {
if (err instanceof BatchError) {
errors.push(...err.errors);
} else if (err instanceof FatalError) {
errors.push(err);
} else {
throw err;
}
}
for (let entrypoint of pkg.entrypoints) {
try {
validateEntrypoint(entrypoint, log);
} catch (err) {
if (err instanceof BatchError) {
errors.push(...err.errors);
} else if (err instanceof FatalError) {
errors.push(err);
} else {
throw err;
}
}
}
}
if (errors.length) {
if (errors.length === 1) {
throw errors[0];
}
throw new BatchError(errors);
}
}
export default async function validate(directory: string) {
let project: Project = await Project.create(directory);
validateProject(project, true);
logger.success(successes.validProject);
}