Skip to content

Commit 3bf79d1

Browse files
chore(scripts): turn on 'strict' tsconfig option (#5376)
1 parent 7d7aae4 commit 3bf79d1

20 files changed

+203
-136
lines changed

scripts/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export async function bundleBuild(opts: BuildOptions): Promise<void> {
8787
await bundle.write(output);
8888
}),
8989
);
90-
} else {
90+
} else if (rollupOption.output) {
9191
await bundle.write(rollupOption.output);
9292
}
9393
}),

scripts/bundles/compiler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export async function compiler(opts: BuildOptions) {
144144
name: 'compilerMinify',
145145
async generateBundle(_, bundleFiles) {
146146
if (opts.isProd) {
147-
const compilerFilename = Object.keys(bundleFiles).find((f) => f.includes('stencil'));
147+
const compilerFilename = Object.keys(bundleFiles).find((f) => f.includes('stencil')) as string;
148148
const compilerBundle = bundleFiles[compilerFilename] as OutputChunk;
149149
const minified = await minifyStencilCompiler(compilerBundle.code, opts);
150150
await fs.writeFile(join(opts.output.compilerDir, compilerFilename.replace('.js', '.min.js')), minified);

scripts/bundles/dev-server.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function devServer(opts: BuildOptions) {
6060
'zlib',
6161
];
6262

63-
const plugins = [
63+
const plugins: Plugin[] = [
6464
contentTypesPlugin(opts),
6565
{
6666
name: 'devServerWorkerResolverPlugin',
@@ -174,7 +174,7 @@ export async function devServer(opts: BuildOptions) {
174174
},
175175
});
176176

177-
if (tsResults.diagnostics.length > 0) {
177+
if (tsResults.diagnostics?.length) {
178178
throw new Error(tsResults.diagnostics as any);
179179
}
180180

@@ -189,7 +189,9 @@ export async function devServer(opts: BuildOptions) {
189189
compress: { hoist_vars: true, hoist_funs: true, ecma: 5 },
190190
format: { ecma: 5 },
191191
});
192-
code = minifyResults.code;
192+
if (minifyResults.code) {
193+
code = minifyResults.code;
194+
}
193195
}
194196

195197
code = banner + code + footer;

scripts/bundles/plugins/alias-plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function aliasPlugin(opts: BuildOptions): Plugin {
3636
* @param id the importee exactly as it is written in an import statement in the source code
3737
* @returns a resolution to an import to a different id, potentially externalizing it from the bundle simultaneously
3838
*/
39-
resolveId(id: string): PartialResolvedId | string | null {
39+
resolveId(id: string): PartialResolvedId | string | null | undefined {
4040
const externalId = alias.get(id);
4141
if (externalId) {
4242
return {

scripts/bundles/plugins/content-types-plugin.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function createContentTypeData(opts: BuildOptions) {
3333
Object.keys(mimeDbJson).forEach((mimeType) => {
3434
const mimeTypeData = mimeDbJson[mimeType];
3535
if (Array.isArray(mimeTypeData.extensions)) {
36-
mimeTypeData.extensions.forEach((ext) => {
36+
mimeTypeData.extensions.forEach((ext: string) => {
3737
extData.push({
3838
ext,
3939
mimeType,
@@ -42,14 +42,14 @@ export async function createContentTypeData(opts: BuildOptions) {
4242
}
4343
});
4444

45-
const exts = {};
45+
const exts: Record<string, string> = {};
4646
extData
4747
.sort((a, b) => {
4848
if (a.ext < b.ext) return -1;
4949
if (a.ext > b.ext) return 1;
5050
return 0;
5151
})
52-
.forEach((x) => (exts[x.ext] = x.mimeType));
52+
.forEach((x: any) => (exts[x.ext] = x.mimeType));
5353

5454
return `export default ${JSON.stringify(exts)}`;
5555
}

scripts/bundles/plugins/parse5-plugin.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function parse5Plugin(opts: BuildOptions): Plugin {
3131
* @param id the path of the module to load
3232
* @returns parse5, pre-bundled
3333
*/
34-
async load(id: string): Promise<string> | null {
34+
async load(id: string): Promise<string | null> {
3535
if (id === 'parse5') {
3636
const [contents] = await bundleParse5(opts);
3737
return contents;
@@ -115,7 +115,9 @@ export async function bundleParse5(opts: BuildOptions): Promise<[contents: strin
115115
comments: false,
116116
},
117117
});
118-
code = minified.code;
118+
if (minified.code) {
119+
code = minified.code;
120+
}
119121
}
120122

121123
code = `// Parse5 ${opts.parse5Version}\n` + code;

scripts/bundles/plugins/pretty-minify.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { OutputChunk, Plugin } from 'rollup';
22

33
import type { BuildOptions } from '../../utils/options';
44

5-
export function prettyMinifyPlugin(opts: BuildOptions, preamble?: string): Plugin {
5+
export function prettyMinifyPlugin(opts: BuildOptions, preamble?: string): Plugin | undefined {
66
if (opts.isProd) {
77
return {
88
name: 'prettyMinifyPlugin',
@@ -26,11 +26,15 @@ export function prettyMinifyPlugin(opts: BuildOptions, preamble?: string): Plugi
2626
format: { ecma: 2018, indent_level: 1, beautify: true, comments: false, preamble },
2727
sourceMap: false,
2828
});
29-
b.code = minifyResults.code;
29+
if (minifyResults.code) {
30+
b.code = minifyResults.code;
31+
}
3032
}
3133
}),
3234
);
3335
},
3436
};
37+
} else {
38+
return undefined;
3539
}
3640
}

scripts/bundles/plugins/terser-plugin.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function terserPlugin(opts: BuildOptions): Plugin {
2828
* @param id the path of the module to load
2929
* @returns the Terser source
3030
*/
31-
async load(id: string): Promise<string> | null {
31+
async load(id: string): Promise<string | null> {
3232
if (id === 'terser') {
3333
const [content] = await bundleTerser(opts);
3434
return content;
@@ -45,6 +45,10 @@ export function terserPlugin(opts: BuildOptions): Plugin {
4545
* was written
4646
*/
4747
export async function bundleTerser(opts: BuildOptions): Promise<[content: string, path: string]> {
48+
if (!opts.terserVersion) {
49+
throw new Error('Terser version not set on build opts!');
50+
}
51+
4852
const fileName = `terser-${opts.terserVersion.replace(/\./g, '_')}-bundle-cache${opts.isProd ? '.min' : ''}.js`;
4953
const cacheFile = join(opts.scriptsBuildDir, fileName);
5054

@@ -80,7 +84,9 @@ export async function bundleTerser(opts: BuildOptions): Promise<[content: string
8084
comments: false,
8185
},
8286
});
83-
code = minified.code;
87+
if (minified.code) {
88+
code = minified.code;
89+
}
8490
}
8591

8692
code = `// Terser ${opts.terserVersion}\n` + code;

scripts/bundles/sys-node.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import rollupCommonjs from '@rollup/plugin-commonjs';
22
import rollupResolve from '@rollup/plugin-node-resolve';
33
import fs from 'fs-extra';
44
import { join } from 'path';
5+
import resolve from 'resolve';
56
import type { RollupOptions } from 'rollup';
67
import webpack, { Configuration } from 'webpack';
78

@@ -162,9 +163,9 @@ export function bundleExternal(opts: BuildOptions, outputDir: string, cachedDir:
162163
return callback(null, '../../mock-doc');
163164
}
164165

165-
if (whitelist.has(request)) {
166+
if (typeof request === 'string' && whitelist.has(request)) {
166167
// we specifically do not want to bundle these imports
167-
require.resolve(request);
168+
resolve.sync(request);
168169
return callback(null, request);
169170
}
170171

@@ -189,9 +190,9 @@ export function bundleExternal(opts: BuildOptions, outputDir: string, cachedDir:
189190
const { minify } = await import('terser');
190191
if (err && err.message) {
191192
rejectBundle(err);
192-
} else {
193+
} else if (stats) {
193194
const info = stats.toJson({ errors: true });
194-
if (stats.hasErrors()) {
195+
if (stats.hasErrors() && info && info.errors) {
195196
const webpackError = info.errors.join('\n');
196197
rejectBundle(webpackError);
197198
} else {
@@ -200,7 +201,9 @@ export function bundleExternal(opts: BuildOptions, outputDir: string, cachedDir:
200201
if (opts.isProd) {
201202
try {
202203
const minifyResults = await minify(code);
203-
code = minifyResults.code;
204+
if (minifyResults.code) {
205+
code = minifyResults.code;
206+
}
204207
} catch (e) {
205208
rejectBundle(e);
206209
return;

scripts/esbuild/dev-server.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { bundleExternal, sysNodeBundleCacheDir } from '../bundles/sys-node';
1111
import { getBanner } from '../utils/banner';
1212
import { type BuildOptions, createReplaceData } from '../utils/options';
1313
import { writePkgJson } from '../utils/write-pkg-json';
14-
import { getBaseEsbuildOptions, getEsbuildAliases, runBuilds } from './util';
14+
import { getBaseEsbuildOptions, getEsbuildAliases, getFirstOutputFile, runBuilds } from './util';
1515

1616
const CONNECTOR_NAME = 'connector.html';
1717

@@ -199,7 +199,10 @@ function clientConnectorPlugin(opts: BuildOptions): Plugin {
199199
name: 'clientConnectorPlugin',
200200
setup(build) {
201201
build.onEnd(async (buildResult) => {
202-
const bundle = buildResult.outputFiles.find((b) => b.path.endsWith(CONNECTOR_NAME));
202+
const bundle = buildResult.outputFiles?.find((b) => b.path.endsWith(CONNECTOR_NAME));
203+
if (!bundle) {
204+
throw "Couldn't find build result!";
205+
}
203206
let code = Buffer.from(bundle.contents).toString();
204207

205208
const tsResults = ts.transpileModule(code, {
@@ -208,7 +211,7 @@ function clientConnectorPlugin(opts: BuildOptions): Plugin {
208211
},
209212
});
210213

211-
if (tsResults.diagnostics.length > 0) {
214+
if (tsResults.diagnostics?.length) {
212215
throw new Error(tsResults.diagnostics as any);
213216
}
214217

@@ -221,7 +224,9 @@ function clientConnectorPlugin(opts: BuildOptions): Plugin {
221224
compress: { hoist_vars: true, hoist_funs: true, ecma: 5 },
222225
format: { ecma: 5 },
223226
});
224-
code = minifyResults.code;
227+
if (minifyResults.code) {
228+
code = minifyResults.code;
229+
}
225230
}
226231

227232
code = banner + code + footer;
@@ -243,7 +248,7 @@ function serverProcessAliasPlugin(): Plugin {
243248
name: 'serverProcessAlias',
244249
setup(build) {
245250
build.onEnd(async (buildResult) => {
246-
const bundle = buildResult.outputFiles[0];
251+
const bundle = getFirstOutputFile(buildResult);
247252
let code = Buffer.from(bundle.contents).toString();
248253
code = code.replace('await import("@dev-server-process")', '(await import("./server-process.js")).default');
249254
return fs.writeFile(bundle.path, code);
@@ -262,7 +267,7 @@ function esm2CJSPlugin(): Plugin {
262267
name: 'esm2CJS',
263268
setup(build) {
264269
build.onEnd(async (buildResult) => {
265-
const bundle = buildResult.outputFiles[0];
270+
const bundle = getFirstOutputFile(buildResult);
266271
let code = Buffer.from(bundle.contents).toString();
267272
code = code.replace('import_meta.url', 'new (require("url").URL)("file:" + __filename).href');
268273
return fs.writeFile(bundle.path, code);
@@ -276,7 +281,7 @@ function esm2CJSPlugin(): Plugin {
276281
* @param opts build options
277282
* @returns an esbuild plugin
278283
*/
279-
function contentTypesPlugin(opts) {
284+
function contentTypesPlugin(opts: BuildOptions): Plugin {
280285
return {
281286
name: 'contentTypesPlugin',
282287
setup(build) {

scripts/esbuild/testing.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import { copyTestingInternalDts } from '../bundles/testing';
66
import { getBanner } from '../utils/banner';
77
import type { BuildOptions } from '../utils/options';
88
import { writePkgJson } from '../utils/write-pkg-json';
9-
import { externalAlias, getBaseEsbuildOptions, getEsbuildAliases, getEsbuildExternalModules, runBuilds } from './util';
9+
import {
10+
externalAlias,
11+
getBaseEsbuildOptions,
12+
getEsbuildAliases,
13+
getEsbuildExternalModules,
14+
getFirstOutputFile,
15+
runBuilds,
16+
} from './util';
1017

1118
const EXTERNAL_TESTING_MODULES = [
1219
'constants',
@@ -86,7 +93,7 @@ function lazyRequirePlugin(opts: BuildOptions, moduleIds: string[]): Plugin {
8693
name: 'lazyRequirePlugin',
8794
setup(build) {
8895
build.onEnd(async (buildResult) => {
89-
const bundle = buildResult.outputFiles[0];
96+
const bundle = getFirstOutputFile(buildResult);
9097
let code = Buffer.from(bundle.contents).toString();
9198

9299
for (const moduleId of moduleIds) {

scripts/esbuild/util.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BuildOptions as ESBuildOptions, BuildResult as ESBuildResult, Plugin } from 'esbuild';
1+
import type { BuildOptions as ESBuildOptions, BuildResult as ESBuildResult, OutputFile, Plugin } from 'esbuild';
22
import * as esbuild from 'esbuild';
33
import { join } from 'path';
44

@@ -146,3 +146,20 @@ export function externalAlias(moduleId: string, resolveToPath: string): Plugin {
146146
},
147147
};
148148
}
149+
150+
/**
151+
* Extract the first {@link OutputFile} record from an Esbuild
152+
* {@link BuildResult}. This _may_ not be present, so in order to guarantee
153+
* type safety this function throws if such an `OutputFile` cannot be found.
154+
*
155+
* @throws if no `OutputFile` can be found.
156+
* @param buildResult the Esbuild build result in which to look
157+
* @returns the OutputFile
158+
*/
159+
export function getFirstOutputFile(buildResult: ESBuildResult): OutputFile {
160+
const bundle = buildResult.outputFiles?.[0];
161+
if (!bundle) {
162+
throw new Error('Could not find an output file in the BuildResult!');
163+
}
164+
return bundle;
165+
}

scripts/release.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export async function release(rootDir: string, args: ReadonlyArray<string>): Pro
2929
console.log(`\n${color.bold.red('No `--version [VERSION]` argument was found. Exiting')}\n`);
3030
process.exit(1);
3131
}
32-
prepareOpts.version = getNewVersion(prepareOpts.packageJson.version, args[versionIdx + 1]);
32+
if (prepareOpts.packageJson.version) {
33+
prepareOpts.version = getNewVersion(prepareOpts.packageJson.version, args[versionIdx + 1]);
34+
}
3335

3436
await prepareRelease(prepareOpts, args);
3537
console.log(`${color.bold.blue('Release Prepared!')}`);
@@ -42,7 +44,9 @@ export async function release(rootDir: string, args: ReadonlyArray<string>): Pro
4244
isProd: true,
4345
});
4446
// this was bumped already, we just need to copy it from package.json into this field
45-
prepareOpts.version = prepareOpts.packageJson.version;
47+
if (prepareOpts.packageJson.version) {
48+
prepareOpts.version = prepareOpts.packageJson.version;
49+
}
4650

4751
// we generated a vermoji during the preparation step, let's grab it from the changelog
4852
prepareOpts.vermoji = getLatestVermoji(prepareOpts.changelogPath);
@@ -72,7 +76,7 @@ export async function release(rootDir: string, args: ReadonlyArray<string>): Pro
7276
isCI: prepareOpts.isCI,
7377
isPublishRelease: true,
7478
isProd: true,
75-
tag: newTag,
79+
tag: newTag ?? undefined,
7680
});
7781
return await publishRelease(publishOpts, args);
7882
}

scripts/test/validate-build.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function validatePackage(opts: BuildOptions, testPkg: TestPackage, dtsEntries: s
174174
fs.accessSync(pkgFile);
175175
});
176176
testPkg.packageJsonFiles.forEach((testPkgFile) => {
177-
if (!pkgJson.files.includes(testPkgFile)) {
177+
if (!pkgJson.files?.includes(testPkgFile)) {
178178
throw new Error(testPkg.packageJson + ' missing file ' + testPkgFile);
179179
}
180180

@@ -189,8 +189,10 @@ function validatePackage(opts: BuildOptions, testPkg: TestPackage, dtsEntries: s
189189

190190
if (pkgJson.bin) {
191191
Object.keys(pkgJson.bin).forEach((k) => {
192-
const binExe = join(pkgDir, pkgJson.bin[k]);
193-
fs.accessSync(binExe);
192+
if (pkgJson.bin?.[k]) {
193+
const binExe = join(pkgDir, pkgJson.bin[k]);
194+
fs.accessSync(binExe);
195+
}
194196
});
195197
}
196198

@@ -296,7 +298,7 @@ async function validateCompiler(opts: BuildOptions): Promise<void> {
296298
console.log(`🐋 Validated compiler.transpileSync()`);
297299

298300
const orgConsoleLog = console.log;
299-
let loggedVersion = null;
301+
let loggedVersion = '';
300302
console.log = (value: string) => (loggedVersion = value);
301303

302304
// this runTask is intentionally not wrapped in telemetry helpers

0 commit comments

Comments
 (0)