-
Notifications
You must be signed in to change notification settings - Fork 125
/
index.ts
90 lines (77 loc) · 2.57 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import path from 'path'
import consola from 'consola'
import { Module } from '@nuxt/types'
import { Options as TsLoaderOptions } from 'ts-loader'
import { ForkTsCheckerWebpackPluginOptions as TsCheckerOptions } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPluginOptions'
import { RuleSetUseItem } from 'webpack'
declare module '@nuxt/types' {
interface NuxtOptions {
typescript: Options
}
}
export interface Options {
ignoreNotFoundWarnings?: boolean
loaders?: {
ts?: Partial<TsLoaderOptions>
tsx?: Partial<TsLoaderOptions>
}
typeCheck?: TsCheckerOptions | boolean
}
const defaults: Options = {
ignoreNotFoundWarnings: false,
typeCheck: true
}
const tsModule: Module<Options> = function (moduleOptions) {
// Combine options
const options = Object.assign(
defaults,
this.options.typescript,
moduleOptions
)
// Change color of CLI banner
this.options.cli.bannerColor = 'blue'
if (!this.options.extensions.includes('ts')) {
this.options.extensions.push('ts')
}
// Extend Builder to handle .ts/.tsx files as routes and watch them
this.options.build.additionalExtensions = ['ts', 'tsx']
if (options.ignoreNotFoundWarnings) {
this.options.build.warningIgnoreFilters!.push(warn =>
warn.name === 'ModuleDependencyWarning' && /export .* was not found in /.test(warn.message)
)
}
this.extendBuild((config, { isClient, isModern }) => {
config.resolve!.extensions!.push('.ts', '.tsx')
const jsxRuleLoaders = config.module!.rules.find(r => (r.test as RegExp).test('.jsx'))!.use as RuleSetUseItem[]
const babelLoader = jsxRuleLoaders[jsxRuleLoaders.length - 1]
config.module!.rules.push(...(['ts', 'tsx'] as const).map(ext =>
({
test: new RegExp(`\\.${ext}$`, 'i'),
use: [
babelLoader,
{
loader: 'ts-loader',
options: {
transpileOnly: true,
appendTsxSuffixTo: ext === 'tsx' ? [/\.vue$/] : [],
...(options.loaders && options.loaders[ext])
}
}
]
})
))
if (options.typeCheck && isClient && !isModern) {
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
config.plugins!.push(new ForkTsCheckerWebpackPlugin(Object.assign({
typescript: {
configFile: path.resolve(this.options.rootDir!, 'tsconfig.json'),
extensions: {
vue: true
}
},
logger: consola.withScope('nuxt:typescript')
} as TsCheckerOptions, options.typeCheck)))
}
})
}
export default tsModule