11import path from 'path'
22import { defu } from 'defu'
3- import consola from 'consola'
43import type { Module } from '@nuxt/types'
54import type { Options as TsLoaderOptions } from 'ts-loader'
65import type { ForkTsCheckerWebpackPluginOptions as TsCheckerOptions } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPluginOptions'
7- import type TsCheckerLogger from 'fork-ts-checker-webpack-plugin/lib/logger/Logger'
86import type { RuleSetUseItem } from 'webpack'
97import { NormalModuleReplacementPlugin } from 'webpack'
108
@@ -48,7 +46,7 @@ const tsModule: Module<Options> = function (moduleOptions) {
4846 )
4947 }
5048
51- this . extendBuild ( ( config , { isClient , isModern } ) => {
49+ this . extendBuild ( ( config , { _isClient , _isModern } ) => {
5250 config . resolve ! . extensions ! . push ( '.ts' , '.tsx' )
5351
5452 // Add alias for @babel /runtime/helpers
@@ -61,7 +59,68 @@ const tsModule: Module<Options> = function (moduleOptions) {
6159 const babelLoader = jsxRuleLoaders [ jsxRuleLoaders . length - 1 ]
6260
6361 config . module ! . rules . push ( ...( [ 'ts' , 'tsx' ] as const ) . map ( ext => ( {
64- test : new RegExp ( `\.${ ext } $` ) ,
62+ test : new RegExp ( `\.${ ext } import path from 'path '
63+ import { defu } from 'defu '
64+ import type { Module } from '@nuxt/types'
65+ import type { Options as TsLoaderOptions } from 'ts-loader'
66+ import type { ForkTsCheckerWebpackPluginOptions as TsCheckerOptions } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPluginOptions'
67+ import type { RuleSetUseItem } from 'webpack'
68+ import { NormalModuleReplacementPlugin } from 'webpack'
69+
70+ export interface Options {
71+ ignoreNotFoundWarnings ?: boolean
72+ loaders ?: {
73+ ts ?: Partial < TsLoaderOptions >
74+ tsx ?: Partial < TsLoaderOptions >
75+ }
76+ typeCheck ?: TsCheckerOptions | boolean
77+ }
78+
79+ declare module '@nuxt/types' {
80+ interface NuxtOptions {
81+ typescript : Options
82+ }
83+ }
84+
85+ const defaults : Options = {
86+ ignoreNotFoundWarnings : false ,
87+ typeCheck : true
88+ }
89+
90+ const tsModule : Module < Options > = function ( moduleOptions ) {
91+ // Combine options
92+ const options = defu ( this . options . typescript , moduleOptions , defaults )
93+
94+ // Change color of CLI banner
95+ this . options . cli . bannerColor = 'blue'
96+
97+ if ( ! this . options . extensions . includes ( 'ts' ) ) {
98+ this . options . extensions . push ( 'ts' )
99+ }
100+
101+ // Extend Builder to handle .ts/.tsx files as routes and watch them
102+ this . options . build . additionalExtensions = [ 'ts' , 'tsx' ]
103+
104+ if ( options . ignoreNotFoundWarnings ) {
105+ this . options . build . warningIgnoreFilters ! . push ( warn =>
106+ warn . name === 'ModuleDependencyWarning' && / e x p o r t .* w a s n o t f o u n d i n / . test ( warn . message )
107+ )
108+ }
109+
110+ this . extendBuild ( ( config , { _isClient, _isModern } ) => {
111+ config . resolve ! . extensions ! . push ( '.ts' , '.tsx' )
112+
113+ // Add alias for @babel /runtime/helpers
114+ config . resolve ! . alias = {
115+ ...config . resolve ! . alias ,
116+ '@babel/runtime/helpers' : path . resolve ( this . options . rootDir ! , 'node_modules/@babel/runtime/helpers' )
117+ }
118+
119+ const jsxRuleLoaders = config . module ! . rules . find ( r => ( r . test as RegExp ) . test ( '.jsx' ) ) ! . use as RuleSetUseItem [ ]
120+ const babelLoader = jsxRuleLoaders [ jsxRuleLoaders . length - 1 ]
121+
122+ config . module ! . rules . push ( ...( [ 'ts' , 'tsx' ] as const ) . map ( ext => ( {
123+ ) ,
65124 use : [
66125 babelLoader ,
67126 {
@@ -88,4 +147,6 @@ const tsModule: Module<Options> = function (moduleOptions) {
88147 }
89148 }
90149 ) )
91- }
150+ }
151+ } )
152+ }
0 commit comments