Skip to content

Commit

Permalink
Added support for tsconfig.build.json
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimck committed Apr 3, 2024
1 parent c72ecac commit 33a703a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-walls-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/cli": minor
---

Added support for `tsconfig.build.json` config file, which is prioritized for tsChecker plugin in webpack. This allows to have separate tsconfig for build and code editor, which let's you opt out of checking some files not needed for build.
5 changes: 4 additions & 1 deletion packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export interface ImaConfigurationContext extends ImaCliArgs {
js: string;
public: string;
};
useTypescript: boolean;
typescript: {
enabled: boolean;
tsconfigPath: string | undefined;
};
imaEnvironment: Environment;
appDir: string;
lessGlobalsPath: string;
Expand Down
15 changes: 10 additions & 5 deletions packages/cli/src/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import lessPluginGlob from 'less-plugin-glob';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import webpack, {
// eslint-disable-next-line import/default
import {
Configuration,
RuleSetRule,
RuleSetUseItem,
WebpackPluginInstance,
HotModuleReplacementPlugin,
} from 'webpack';

import { getLanguageEntryPoints } from './languages';
Expand Down Expand Up @@ -51,7 +53,7 @@ export default async (
name,
processCss,
outputFolders,
useTypescript,
typescript,
imaEnvironment,
appDir,
useHMR,
Expand Down Expand Up @@ -424,7 +426,7 @@ export default async (
/**
* Handle app Typescript files
*/
useTypescript && {
typescript.enabled && {
test: /\.(ts|tsx)$/,
include: appDir,
loader: require.resolve('swc-loader'),
Expand Down Expand Up @@ -582,8 +584,11 @@ export default async (
* to show errors at least during build so it fails before going to production.
*/
isClientES &&
useTypescript &&
typescript.enabled &&
new ForkTsCheckerWebpackPlugin({
typescript: {
configFile: typescript.tsconfigPath,
},
async: ctx.command === 'dev', // be async only in watch mode,
devServer: false,
// Custom formatter for async mode
Expand Down Expand Up @@ -635,7 +640,7 @@ export default async (
: []),

// Following plugins enable react refresh and hmr in watch mode
useHMR && new webpack.HotModuleReplacementPlugin(),
useHMR && new HotModuleReplacementPlugin(),
useHMR &&
ctx.reactRefresh &&
new ReactRefreshWebpackPlugin({
Expand Down
17 changes: 15 additions & 2 deletions packages/cli/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import webpackConfig from './config';
import { ImaConfigurationContext, ImaConfig, ImaCliArgs } from '../types';

export const IMA_CONF_FILENAME = 'ima.config.js';
const TS_CONFIG_PATHS = ['tsconfig.build.json', 'tsconfig.json'];

/**
* Helper for finding rules with given loader in webpack config.
Expand Down Expand Up @@ -372,7 +373,6 @@ export function createContexts(
!!imaConfig.sourceMaps || args.environment === 'development';
const imaEnvironment = resolveEnvironment(rootDir);
const appDir = path.join(rootDir, 'app');
const useTypescript = fs.existsSync(path.join(rootDir, './tsconfig.json'));
const lessGlobalsPath = path.join(rootDir, 'app/less/globals.less');
const isDevEnv = environment === 'development';
const mode = environment === 'production' ? 'production' : 'development';
Expand All @@ -382,6 +382,16 @@ export function createContexts(
: 'source-map'
: false;

let tsconfigPath: string | undefined = undefined;

// Find tsconfig path in rootDir based on priority set in TS_CONFIG_PATHS
for (const fileName of TS_CONFIG_PATHS) {
if (fs.existsSync(path.join(rootDir, fileName))) {
tsconfigPath = path.join(rootDir, fileName);
break;
}
}

// es2018 targets (taken from 'browserslist-generator')
const targets = [
'and_chr >= 63',
Expand Down Expand Up @@ -415,7 +425,10 @@ export function createContexts(
? 'static/js'
: 'static/js.es',
},
useTypescript,
typescript: {
enabled: !!tsconfigPath,
tsconfigPath,
},
imaEnvironment,
appDir,
useHMR: command === 'dev' && name === 'client.es',
Expand Down

0 comments on commit 33a703a

Please sign in to comment.