Skip to content

Commit 965ccb4

Browse files
authored
fix(webpack): typescript imports in non-ts projects (NativeScript#9714)
1 parent 6ca3d35 commit 965ccb4

4 files changed

Lines changed: 57 additions & 22 deletions

File tree

packages/webpack5/src/configuration/angular.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { ScriptTarget } from 'typescript';
21
import { extname, resolve } from 'path';
32
import Config from 'webpack-chain';
43
import { existsSync } from 'fs';
54

5+
import { getTypescript, readTsConfig } from '../helpers/typescript';
66
import { getDependencyPath } from '../helpers/dependencies';
77
import { getProjectFilePath } from '../helpers/project';
88
import { env as _env, IWebpackEnv } from '../index';
9-
import { readTsConfig } from '../helpers/tsconfig';
109
import { warnOnce } from '../helpers/log';
1110
import {
1211
getEntryDirPath,
@@ -179,6 +178,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
179178
const buildAngularPath = getDependencyPath('@angular-devkit/build-angular');
180179
if (buildAngularPath) {
181180
const tsConfig = readTsConfig(tsConfigPath);
181+
const { ScriptTarget } = getTypescript();
182182
const scriptTarget = tsConfig.options.target ?? ScriptTarget.ESNext;
183183
const buildAngularOptions: any = {
184184
scriptTarget,

packages/webpack5/src/helpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
getPlatform,
2727
getPlatformName,
2828
} from './platform';
29-
import { readTsConfig } from './tsconfig';
29+
import { readTsConfig } from './typescript';
3030

3131
// intentionally populated manually
3232
// as this generates nicer typings

packages/webpack5/src/helpers/tsconfig.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { dirname } from 'path';
2+
import { env } from '..';
3+
4+
import { warnOnce } from './log';
5+
6+
/**
7+
* @internal
8+
*/
9+
let typescript: typeof import('typescript');
10+
11+
/**
12+
* Helper used to import typescript.
13+
*
14+
* The reason this exists is that not all flavors use Typescript, and
15+
* in those cases just importing this helper will throw an exception.
16+
*/
17+
export function getTypescript(): typeof import('typescript') {
18+
if (typescript) {
19+
return typescript;
20+
}
21+
22+
try {
23+
typescript = require('typescript');
24+
return typescript;
25+
} catch (err) {
26+
warnOnce(
27+
'typescript-missing',
28+
`TypeScript is not installed in this project, but a config is trying to use it.`,
29+
env.verbose
30+
? new Error().stack
31+
: 'Run with --env.verbose to log a stack trace to help debug this further.'
32+
);
33+
34+
return {} as any;
35+
}
36+
}
37+
38+
export function readTsConfig(path: string) {
39+
const { readConfigFile, parseJsonConfigFileContent, sys } = getTypescript();
40+
const f = readConfigFile(path, sys.readFile);
41+
42+
const parsed = parseJsonConfigFileContent(
43+
f.config,
44+
{
45+
fileExists: sys.fileExists,
46+
readFile: sys.readFile,
47+
readDirectory: sys.readDirectory,
48+
useCaseSensitiveFileNames: true,
49+
},
50+
dirname(path)
51+
);
52+
53+
return parsed;
54+
}

0 commit comments

Comments
 (0)