UNPKG

2.58 kBJavaScriptView Raw
1import path from 'path'
2import gm from 'global-modules'
3const fs = require('fs')
4
5import { load } from '../'
6
7let aliases
8try {
9 aliases = require('../webpack').default.resolve.alias
10} catch (e) {
11 aliases = {}
12}
13aliases = Object.entries(aliases)
14
15const getExcludedTypes = () => {
16 const pathCwd = path.resolve(process.cwd(), 'node_modules', '@types')
17
18 if (!fs.existsSync(pathCwd)) {
19 return []
20 }
21
22 const isDirectory = source => fs.lstatSync(path.join(pathCwd, source)).isDirectory()
23 const getDirectories = source => fs.readdirSync(source).filter(isDirectory)
24 const excluded = []
25
26 getDirectories(pathCwd).forEach(dir => {
27 const p = path.resolve(gm, '@mhy/mhy', 'node_modules', '@types', dir)
28 const pFallback = path.resolve('/home/node/.npm-global/lib/node_modules/@mhy/mhy/node_modules/@types', dir)
29 excluded.push(p)
30 excluded.push(pFallback)
31 })
32
33 return excluded
34}
35
36const tsconfig = (module.exports = module.exports.default = load('typescript', {
37 compilerOptions: {
38 module: 'esNext',
39 target: 'esnext',
40 moduleResolution: 'node',
41 allowJs: false,
42 emitDeclarationOnly: true,
43 strict: true,
44 jsx: 'preserve',
45 resolveJsonModule: true,
46 esModuleInterop: true,
47 noImplicitAny: false,
48 declaration: true,
49 typeRoots: [
50 path.resolve(process.cwd(), 'node_modules', '@types'),
51 path.resolve(gm, '@mhy/mhy', 'node_modules', '@types'),
52 path.resolve('/home/node/.npm-global/lib/node_modules/@mhy/mhy/node_modules/@types')
53 ],
54 baseUrl: path.resolve(process.cwd(), 'src'),
55 paths: aliases.reduce(
56 function(acc, [k]) {
57 const folder = k.replace('@', ``)
58 acc[k] = [`${folder}/index`]
59 acc[`${k}/*`] = [`${folder}/*`]
60 return acc
61 },
62 {
63 '*': [
64 path.resolve(gm, '@mhy/mhy', 'node_modules', '*'),
65 path.resolve(
66 gm,
67 '@mhy/mhy',
68 'node_modules',
69 '@types',
70 '*'
71 )
72 ]
73 }
74 )
75 },
76 exclude: [
77 ...getExcludedTypes()
78 ],
79 include: [path.resolve(process.cwd(), 'src/**/*')],
80 files: [require.resolve('../../typescript/mhy.d.ts')]
81}))
82
83// Generate fresh tsconfig.json on each run
84require('../_utils/tsconfig')(process.cwd(), tsconfig)