Skip to content

Commit 150ea05

Browse files
author
Seu Nome
committed
fix(typescript-build): resolve aliases in dynamic imports
1 parent 447a77e commit 150ea05

File tree

1 file changed

+70
-28
lines changed

1 file changed

+70
-28
lines changed

packages/typescript-build/src/index.ts

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,80 @@ const tsModule: Module<Options> = function (moduleOptions) {
5151
this.extendBuild((config, { isClient, isModern }) => {
5252
config.resolve!.extensions!.push('.ts', '.tsx')
5353

54+
// Add alias for @babel/runtime/helpers
55+
config.resolve!.alias = {
56+
...config.resolve!.alias,
57+
'@babel/runtime/helpers': path.resolve(this.options.rootDir!, 'node_modules/@babel/runtime/helpers')
58+
}
59+
5460
const jsxRuleLoaders = config.module!.rules.find(r => (r.test as RegExp).test('.jsx'))!.use as RuleSetUseItem[]
5561
const babelLoader = jsxRuleLoaders[jsxRuleLoaders.length - 1]
5662

5763
config.module!.rules.push(...(['ts', 'tsx'] as const).map(ext => ({
58-
test: new RegExp(`\\.${ext}$`),
59-
use: [
64+
test: new RegExp(`\.${ext}import path from 'path'
65+
import { defu } from 'defu'
66+
import consola from 'consola'
67+
import type { Module } from '@nuxt/types'
68+
import type { Options as TsLoaderOptions } from 'ts-loader'
69+
import type { ForkTsCheckerWebpackPluginOptions as TsCheckerOptions } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPluginOptions'
70+
import type TsCheckerLogger from 'fork-ts-checker-webpack-plugin/lib/logger/Logger'
71+
import type { RuleSetUseItem } from 'webpack'
72+
import { NormalModuleReplacementPlugin } from 'webpack'
73+
74+
export interface Options {
75+
ignoreNotFoundWarnings?: boolean
76+
loaders?: {
77+
ts?: Partial<TsLoaderOptions>
78+
tsx?: Partial<TsLoaderOptions>
79+
}
80+
typeCheck?: TsCheckerOptions | boolean
81+
}
82+
83+
declare module '@nuxt/types' {
84+
interface NuxtOptions {
85+
typescript: Options
86+
}
87+
}
88+
89+
const defaults: Options = {
90+
ignoreNotFoundWarnings: false,
91+
typeCheck: true
92+
}
93+
94+
const tsModule: Module<Options> = function (moduleOptions) {
95+
// Combine options
96+
const options = defu(this.options.typescript, moduleOptions, defaults)
97+
98+
// Change color of CLI banner
99+
this.options.cli.bannerColor = 'blue'
100+
101+
if (!this.options.extensions.includes('ts')) {
102+
this.options.extensions.push('ts')
103+
}
104+
105+
// Extend Builder to handle .ts/.tsx files as routes and watch them
106+
this.options.build.additionalExtensions = ['ts', 'tsx']
107+
108+
if (options.ignoreNotFoundWarnings) {
109+
this.options.build.warningIgnoreFilters!.push(warn =>
110+
warn.name === 'ModuleDependencyWarning' && /export .* was not found in /.test(warn.message)
111+
)
112+
}
113+
114+
this.extendBuild((config, { isClient, isModern }) => {
115+
config.resolve!.extensions!.push('.ts', '.tsx')
116+
117+
// Add alias for @babel/runtime/helpers
118+
config.resolve!.alias = {
119+
...config.resolve!.alias,
120+
'@babel/runtime/helpers': path.resolve(this.options.rootDir!, 'node_modules/@babel/runtime/helpers')
121+
}
122+
123+
const jsxRuleLoaders = config.module!.rules.find(r => (r.test as RegExp).test('.jsx'))!.use as RuleSetUseItem[]
124+
const babelLoader = jsxRuleLoaders[jsxRuleLoaders.length - 1]
125+
126+
),
127+
), use: [
60128
babelLoader,
61129
{
62130
loader: 'ts-loader',
@@ -83,29 +151,3 @@ const tsModule: Module<Options> = function (moduleOptions) {
83151
}
84152
))
85153
}
86-
if (options.typeCheck && isClient && !isModern) {
87-
// eslint-disable-next-line @typescript-eslint/no-var-requires
88-
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
89-
const logger = consola.withTag('nuxt:typescript')
90-
/* istanbul ignore next */
91-
const loggerInterface: TsCheckerLogger = {
92-
log (message) { logger.log(message) },
93-
info (message) { logger.info(message) },
94-
error (message) { logger.error(message) }
95-
}
96-
config.plugins!.push(new ForkTsCheckerWebpackPlugin(defu(options.typeCheck, {
97-
typescript: {
98-
configFile: path.resolve(this.options.rootDir!, 'tsconfig.json'),
99-
extensions: {
100-
vue: true
101-
}
102-
},
103-
logger: {
104-
issues: loggerInterface
105-
}
106-
} as TsCheckerOptions)))
107-
}
108-
})
109-
}
110-
111-
export default tsModule

0 commit comments

Comments
 (0)