Skip to content

Commit e63aa9b

Browse files
committed
feat(scripts): improve typechecking performance
Explicitly add some options in project config so that it can be improved from userland. See #447
1 parent ee82d38 commit e63aa9b

File tree

7 files changed

+109
-4
lines changed

7 files changed

+109
-4
lines changed

examples/plugin/src/ts/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ console.log('Hello from typescript, I am gonna say foo 30 times');
77
console.log(strRepeat('foo', 10));
88
console.log(strRepeat('foo', 10));
99
console.log(strRepeat('foo', 10));
10+
console.log(strRepeat('10', 20));
11+
console.log(strRepeat('[]', 20));
1012

1113
if (module.hot) {
1214
module.hot.accept('./module.ts', () => {

examples/plugin/wpackio.project.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
main: ['./src/app/index.js'],
3636
mobile: ['./src/app/mobile.js'],
3737
},
38+
hasTypeScript: false,
3839
// Extra webpack config to be passed directly
3940
webpackConfig: (config, merge, appDir, isDev) => {
4041
const svgoLoader = {
@@ -98,6 +99,7 @@ module.exports = {
9899
entry: {
99100
main: ['./src/foo/foo.js'],
100101
},
102+
hasTypeScript: false,
101103
// Extra webpack config to be passed directly
102104
webpackConfig: undefined,
103105
},
@@ -108,6 +110,7 @@ module.exports = {
108110
entry: {
109111
main: ['./src/reactapp/index.jsx'],
110112
},
113+
hasTypeScript: false,
111114
webpackConfig: (config, merge, appDir, isDev) => {
112115
const customRules = {
113116
module: {
@@ -168,6 +171,8 @@ module.exports = {
168171
},
169172
{
170173
name: 'tsapp',
174+
// hasTypeScript: true,
175+
typeWatchFiles: ['src/ts/*.{ts,tsx}', 'src/ts/**/*.{ts,tsx}'],
171176
entry: {
172177
main: ['./src/ts/main.ts'],
173178
},

packages/scripts/src/bin/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export function printFailedCompileMEssage() {
7171
);
7272
}
7373

74+
export function printGeneralInfoMessage(msg: string) {
75+
console.info(addTimeStampToLog(`${logSymbols.info} ${msg}`));
76+
}
77+
7478
export const bulletSymbol = chalk.magenta(figures.pointer);
7579

7680
export const wpackLogoSmall = gradient.instagram('wpack.io');

packages/scripts/src/config/WebpackConfigHelper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from './project.config.default';
2323
import { ServerConfig } from './server.config.default';
2424
import { getFileLoaderForJsAndStyleAssets } from './fileLoader';
25+
import { printGeneralInfoMessage } from '../bin/utils';
2526

2627
interface NormalizedEntry {
2728
[x: string]: string[];
@@ -282,7 +283,10 @@ export class WebpackConfigHelper {
282283
];
283284
// Add ts checker plugin if project has tsconfig.json
284285
const tsconfigPath = path.resolve(this.cwd, './tsconfig.json');
285-
if (this.fileExists(tsconfigPath)) {
286+
if (
287+
this.fileExists(tsconfigPath) &&
288+
this.file.hasTypeScript !== false
289+
) {
286290
// dynamic require forktschecker otherwise it will throw error
287291
try {
288292
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, @typescript-eslint/no-var-requires
@@ -297,6 +301,10 @@ export class WebpackConfigHelper {
297301
formatterOptions: {
298302
highlightCode: true,
299303
},
304+
useTypescriptIncrementalApi: false,
305+
reportFiles: this.file.typeWatchFiles
306+
? this.file.typeWatchFiles
307+
: [],
300308
})
301309
);
302310
} catch (e) {

packages/scripts/src/config/project.config.default.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface EntryConfig {
3131
export interface FileConfig {
3232
name: string;
3333
entry: EntryConfig;
34+
typeWatchFiles?: string[];
35+
hasTypeScript?: boolean;
3436
webpackConfig?:
3537
| webpack.Configuration
3638
| ((

site/docs/apis/project-configuration.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,37 @@ under `entry`, then also (if needed) chunk-splitting will be applied.
184184
If we were to pass multiple file object, then webpack would run in [multi-compiler](https://webpack.js.org/api/node/#multicompiler)
185185
mode, separating dependency tree from each of the file object.
186186

187-
Each file object has three properties:
187+
Each file object has two required and three optional properties. Here's the
188+
interface.
188189

189-
#### `name` (`string`):
190+
```ts
191+
interface EntryConfig {
192+
[x: string]: string[] | string;
193+
}
194+
195+
interface FileConfig {
196+
name: string;
197+
entry: EntryConfig;
198+
typeWatchFiles?: string[];
199+
hasTypeScript?: boolean;
200+
webpackConfig?:
201+
| webpack.Configuration
202+
| ((
203+
config: webpack.Configuration,
204+
api: merge,
205+
appDir: string,
206+
isDev: boolean
207+
) => webpack.Configuration);
208+
}
209+
```
210+
211+
#### `name` (`string`) **required**:
190212

191213
A unique name of this file entry. If you are using more than one file entry,
192214
then make sure to give different names, otherwise the compiler might not work
193215
in development mode.
194216

195-
#### `entry` (`object`):
217+
#### `entry` (`object`) **required**:
196218

197219
This is the path (relative to project root) of files you would like to compile.
198220

@@ -245,6 +267,16 @@ Directory inside `outputPath` where all the assets are to be emitted.
245267

246268
Whether the operation is going for development mode or production build.
247269

270+
#### `typeWatchFiles` (`string[]`)
271+
272+
Array of glob pattern for which typescript reports are to be notified.
273+
274+
#### `hasTypeScript` (`boolean` | `undefined`)
275+
276+
Explicitly disable typescript type assertions.
277+
278+
> More information about typescript related options can be found [here](/tutorials/adding-typescript/).
279+
248280
## `outputPath` (`string`):
249281

250282
Name of the directory (relative) where we would put the bundled and manifest

site/docs/tutorials/adding-typescript.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,55 @@ Now you are good to go. The compiler will also show any `ts` error you might hav
8383
> **NOTE** - Internally wpack.io depends on [`fork-ts-checker-webpack-plugin`](https://github.com/Realytics/fork-ts-checker-webpack-plugin)
8484
> to show type errors during build time. So make sure you install it, otherwise
8585
> it will not work.
86+
87+
## Optimization
88+
89+
By default `@wpackio/scripts` will create an instance of `fork-ts-checker-webpack-plugin`
90+
if a `tsconfig.json` is found in the current project root.
91+
92+
The same plugin would go into all multi-entry compiler instances and would enable
93+
typechecking for all entries.
94+
95+
Sometimes, this could not be the desired feature. For instance, you might have
96+
a plain JS app, alongside a TS app and you don't want the typechecker for the JS
97+
app. Luckily it is not possible to explicitly disable typechecking for individual
98+
file entry using `hasTypeScript`.
99+
100+
Furthermore, due to the nature of TypeScript, you might notice duplicate error
101+
reports across multiple files entry. This can also be remedied using `typeWatchFiles`
102+
config variable.
103+
104+
Under your [`FileConfig`](/apis/project-configuration/#files-array) add the
105+
`hasTypeScript` option. Below is an example of two apps, one explicitly disabling
106+
the typecheck.
107+
108+
**wpackio.project.js**
109+
110+
```js
111+
module.exports = {
112+
// ...
113+
files: [
114+
// Only JavaScript App
115+
{
116+
name: 'jsapp',
117+
entry: {
118+
main: './src/jsapp/index.js',
119+
},
120+
// disable typechecking for this
121+
hasTypeScript: false,
122+
},
123+
// TypeScript App
124+
{
125+
name: 'tsapp',
126+
entry: {
127+
main: './src/tsapp/index.ts',
128+
},
129+
// (optional) enable typechecking for this
130+
hasTypeScript: true,
131+
// (optional but recommended) mention files to report
132+
// through glob pattern
133+
typeWatchFiles: ['src/tsapp/*.{ts,tsx}', 'src/tsapp/**/*.{ts,tsx}'],
134+
},
135+
],
136+
};
137+
```

0 commit comments

Comments
 (0)