Skip to content

Commit

Permalink
refactor: enable TypeScript isolatedDeclarations (#2709)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jun 26, 2024
1 parent 0e4b20a commit 7e96510
Show file tree
Hide file tree
Showing 118 changed files with 412 additions and 269 deletions.
4 changes: 3 additions & 1 deletion packages/compat/babel-preset/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { generateBaseConfig } from './base';
import type { BabelConfig, NodePresetOptions } from './types';

export const getBabelConfigForNode = (options: NodePresetOptions = {}) => {
export const getBabelConfigForNode = (
options: NodePresetOptions = {},
): BabelConfig => {
if (options.presetEnv !== false) {
options.presetEnv ??= {};
options.presetEnv.targets ??= ['node >= 16'];
Expand Down
4 changes: 2 additions & 2 deletions packages/compat/babel-preset/src/pluginLockCorejsVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const REWRITE_TARGETS: Record<string, string> = {
const matchedKey = (value: string) =>
Object.keys(REWRITE_TARGETS).find((name) => value.startsWith(`${name}/`));

export const getCoreJsVersion = () => {
export const getCoreJsVersion = (): string => {
try {
const { version } = JSON.parse(readFileSync(CORE_JS_PKG_PATH, 'utf-8'));
const [major, minor] = version.split('.');
Expand All @@ -26,7 +26,7 @@ export const getCoreJsVersion = () => {

export default (_: any) => {
return {
post({ path }: any) {
post({ path }: any): void {
for (const node of path.node.body as t.Node[]) {
// import
if (t.isImportDeclaration(node)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/compat/babel-preset/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const getDefaultPresetEnvOption = (
};
};

export const getBabelConfigForWeb = (options: WebPresetOptions) => {
export const getBabelConfigForWeb = (
options: WebPresetOptions,
): BabelConfig => {
if (options.presetEnv !== false) {
options.presetEnv = {
...getDefaultPresetEnvOption(options),
Expand Down
3 changes: 2 additions & 1 deletion packages/compat/babel-preset/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"outDir": "./dist",
"baseUrl": "./",
"rootDir": "src",
"composite": true
"composite": true,
"isolatedDeclarations": true
},
"references": [
{
Expand Down
8 changes: 4 additions & 4 deletions packages/compat/plugin-swc/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'node:path';

export const CORE_JS_PKG_PATH = require.resolve('core-js/package.json');
export const CORE_JS_DIR = path.dirname(CORE_JS_PKG_PATH);
export const SWC_HELPERS_DIR = path.dirname(
export const CORE_JS_PKG_PATH: string = require.resolve('core-js/package.json');
export const CORE_JS_DIR: string = path.dirname(CORE_JS_PKG_PATH);
export const SWC_HELPERS_DIR: string = path.dirname(
require.resolve('@swc/helpers/package.json'),
);
export const JS_REGEX = /\.js$/;
export const JS_REGEX: RegExp = /\.js$/;
7 changes: 6 additions & 1 deletion packages/compat/plugin-swc/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ export function createLoader(): LoaderDefinitionFunction {
};
}

export default createLoader();
const loader: LoaderDefinitionFunction<
Record<string, unknown>,
Record<string, unknown>
> = createLoader();

export default loader;
4 changes: 2 additions & 2 deletions packages/compat/plugin-swc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const isBeyondReact17 = async (cwd: string) => {
export async function determinePresetReact(
root: string,
pluginConfig: ObjPluginSwcOptions,
) {
): Promise<void> {
pluginConfig.presetReact ??= {};
pluginConfig.presetReact.runtime ??= (await isBeyondReact17(root))
? 'automatic'
Expand All @@ -78,7 +78,7 @@ export function checkUseMinify(
options: ObjPluginSwcOptions,
config: NormalizedEnvironmentConfig,
isProd: boolean,
) {
): boolean {
return (
isProd &&
config.output.minify &&
Expand Down
3 changes: 2 additions & 1 deletion packages/compat/plugin-swc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"outDir": "./dist",
"baseUrl": "./",
"rootDir": "src",
"composite": true
"composite": true,
"isolatedDeclarations": true
},
"references": [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/webpack/src/webpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function generateWebpackConfig({
environment: string;
target: RsbuildTarget;
context: InternalContext;
}) {
}): Promise<WebpackConfig> {
const chainUtils = await getChainUtils(target, environment);
const { default: webpack } = await import('webpack');
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const applyServerOptions = (command: Command) => {
.option('--host <host>', 'specify the host that the server listens to');
};

export function runCli() {
export function runCli(): void {
program.name('rsbuild').usage('<command> [options]').version(RSBUILD_VERSION);

const devCommand = program.command('dev');
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isDev } from '../helpers';
import { loadEnv } from '../loadEnv';
import { logger } from '../logger';
import { onBeforeRestartServer } from '../server/restart';
import type { RsbuildInstance } from '../types';
import type { CommonOptions } from './commands';

let commonOpts: CommonOptions = {};
Expand All @@ -13,7 +14,7 @@ export async function init({
}: {
cliOptions?: CommonOptions;
isRestart?: boolean;
}) {
}): Promise<RsbuildInstance | undefined> {
if (cliOptions) {
commonOpts = cliOptions;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/cli/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function initNodeEnv() {
}
}

export function prepareCli() {
export function prepareCli(): void {
initNodeEnv();

// Print a blank line to keep the greet log nice.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/client/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let clearOverlay: undefined | (() => void);
export const registerOverlay = (
createFn: (err: string[]) => void,
clearFn: () => void,
) => {
): void => {
createOverlay = createFn;
clearOverlay = clearFn;
};
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function getDefaultEntry(root: string): RsbuildEntry {
export const withDefaultConfig = async (
rootPath: string,
config: RsbuildConfig,
) => {
): Promise<RsbuildConfig> => {
const merged = mergeRsbuildConfig(createDefaultConfig(), config);

merged.source ||= {};
Expand Down Expand Up @@ -292,7 +292,7 @@ const resolveConfigPath = (root: string, customConfig?: string) => {
return null;
};

export async function watchFiles(files: string[]) {
export async function watchFiles(files: string[]): Promise<void> {
if (!files.length) {
return;
}
Expand Down Expand Up @@ -407,7 +407,7 @@ export async function outputInspectConfigFiles({
inspectOptions: InspectConfigOptions & {
outputPath: string;
};
}) {
}): Promise<void> {
const { outputPath } = inspectOptions;

const files = [
Expand Down Expand Up @@ -455,7 +455,7 @@ export async function outputInspectConfigFiles({
);
}

export async function stringifyConfig(config: unknown, verbose?: boolean) {
export function stringifyConfig(config: unknown, verbose?: boolean): string {
// webpackChain.toString can be used as a common stringify method
const stringify = RspackChain.toString as (
config: unknown,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { isPlainObject } from './helpers';
import { logger } from './logger';
import type { RsbuildConfig } from './types';

export async function getBundlerChain() {
export function getBundlerChain(): RspackChain {
const bundlerChain = new RspackChain();

return bundlerChain as unknown as RspackChain;
Expand All @@ -30,7 +30,7 @@ export async function modifyBundlerChain(
): Promise<RspackChain> {
logger.debug('modify bundler chain');

const bundlerChain = await getBundlerChain();
const bundlerChain = getBundlerChain();

const [modifiedBundlerChain] = await context.hooks.modifyBundlerChain.call(
bundlerChain,
Expand Down
36 changes: 25 additions & 11 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export const FONT_DIST_DIR = 'static/font';
export const WASM_DIST_DIR = 'static/wasm';
export const IMAGE_DIST_DIR = 'static/image';
export const MEDIA_DIST_DIR = 'static/media';
export const LOADER_PATH = join(__dirname);
export const STATIC_PATH = join(__dirname, '../static');
export const COMPILED_PATH = join(__dirname, '../compiled');
export const LOADER_PATH: string = join(__dirname);
export const STATIC_PATH: string = join(__dirname, '../static');
export const COMPILED_PATH: string = join(__dirname, '../compiled');
export const TS_CONFIG_FILE = 'tsconfig.json';
export const HMR_SOCKET_PATH = '/rsbuild-hmr';

Expand All @@ -23,29 +23,36 @@ export const DEFAULT_DATA_URL_SIZE = 4096;
export const DEFAULT_MOUNT_ID = 'root';
export const DEFAULT_DEV_HOST = '0.0.0.0';
export const DEFAULT_ASSET_PREFIX = '/';
export const DEFAULT_WEB_BROWSERSLIST = [
export const DEFAULT_WEB_BROWSERSLIST: string[] = [
'chrome >= 87',
'edge >= 88',
'firefox >= 78',
'safari >= 14',
];
export const DEFAULT_BROWSERSLIST = {
export const DEFAULT_BROWSERSLIST: Record<string, string[]> = {
web: DEFAULT_WEB_BROWSERSLIST,
'web-worker': DEFAULT_WEB_BROWSERSLIST,
node: ['node >= 16'],
};

// RegExp
export const HTML_REGEX = /\.html$/;
export const CSS_REGEX = /\.css$/;
export const HTML_REGEX: RegExp = /\.html$/;
export const CSS_REGEX: RegExp = /\.css$/;

// Plugins
export const PLUGIN_SWC_NAME = 'rsbuild:swc';
export const PLUGIN_CSS_NAME = 'rsbuild:css';

// Extensions
export const FONT_EXTENSIONS = ['woff', 'woff2', 'eot', 'ttf', 'otf', 'ttc'];
export const IMAGE_EXTENSIONS = [
export const FONT_EXTENSIONS: string[] = [
'woff',
'woff2',
'eot',
'ttf',
'otf',
'ttc',
];
export const IMAGE_EXTENSIONS: string[] = [
'png',
'jpg',
'jpeg',
Expand All @@ -61,5 +68,12 @@ export const IMAGE_EXTENSIONS = [
'tiff',
'jfif',
];
export const VIDEO_EXTENSIONS = ['mp4', 'webm', 'ogg', 'mov'];
export const AUDIO_EXTENSIONS = ['mp3', 'wav', 'flac', 'aac', 'm4a', 'opus'];
export const VIDEO_EXTENSIONS: string[] = ['mp4', 'webm', 'ogg', 'mov'];
export const AUDIO_EXTENSIONS: string[] = [
'mp3',
'wav',
'flac',
'aac',
'm4a',
'opus',
];
6 changes: 3 additions & 3 deletions packages/core/src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function createContextByConfig(
// using cache to avoid multiple calls to loadConfig
const browsersListCache = new Map<string, string[]>();

export async function getBrowserslist(path: string) {
export async function getBrowserslist(path: string): Promise<string[] | null> {
const env = process.env.NODE_ENV;
const cacheKey = path + env;

Expand Down Expand Up @@ -131,7 +131,7 @@ const getEnvironmentHTMLPaths = (
export async function updateEnvironmentContext(
context: RsbuildContext,
configs: Record<string, NormalizedEnvironmentConfig>,
) {
): Promise<void> {
context.environments ||= {};

for (const [name, config] of Object.entries(configs)) {
Expand All @@ -158,7 +158,7 @@ export async function updateEnvironmentContext(
}
}

export function updateContextByNormalizedConfig(context: RsbuildContext) {
export function updateContextByNormalizedConfig(context: RsbuildContext): void {
// Try to get the parent dist path from all environments
const distPaths = Object.values(context.environments).map(
(item) => item.distPath,
Expand Down
Loading

0 comments on commit 7e96510

Please sign in to comment.