Skip to content

Commit f04787f

Browse files
author
Seu Nome
committed
fix: resolve dynamic import aliases in template literals
Fixes #520 Previously, Webpack failed to resolve Nuxt aliases (e.g., `~`, `@`) within dynamic `import()` statements that used template literals (e.g., `~/components/${component}.vue`). This commit introduces the use of Webpack's `NormalModuleReplacementPlugin` to intercept and correctly resolve these aliased paths before Webpack processes them, ensuring proper module resolution during the build process.
1 parent 056307d commit f04787f

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports = function (api) {
1111
node: 'current'
1212
}
1313
}]
14+
],
15+
plugins: [
16+
'@babel/plugin-transform-runtime'
1417
]
1518
}
1619
}

packages/typescript-build/src/index.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Module } from '@nuxt/types'
55
import type { Options as TsLoaderOptions } from 'ts-loader'
66
import type { ForkTsCheckerWebpackPluginOptions as TsCheckerOptions } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPluginOptions'
77
import type TsCheckerLogger from 'fork-ts-checker-webpack-plugin/lib/logger/Logger'
8-
import type { RuleSetUseItem } from 'webpack'
8+
import type { RuleSetUseItem, NormalModuleReplacementPlugin } from 'webpack'
99

1010
export interface Options {
1111
ignoreNotFoundWarnings?: boolean
@@ -53,23 +53,36 @@ const tsModule: Module<Options> = function (moduleOptions) {
5353
const jsxRuleLoaders = config.module!.rules.find(r => (r.test as RegExp).test('.jsx'))!.use as RuleSetUseItem[]
5454
const babelLoader = jsxRuleLoaders[jsxRuleLoaders.length - 1]
5555

56-
config.module!.rules.push(...(['ts', 'tsx'] as const).map(ext =>
57-
({
58-
test: new RegExp(`\\.${ext}$`, 'i'),
56+
config.module!.rules.push(...(['ts', 'tsx'] as const).map(ext => ({
57+
test: new RegExp(`\\.${ext}$`),
5958
use: [
6059
babelLoader,
6160
{
6261
loader: 'ts-loader',
6362
options: {
6463
transpileOnly: true,
65-
appendTsxSuffixTo: ext === 'tsx' ? [/\.vue$/] : [],
64+
appendTsxSuffixTo: ext === 'tsx' ? [/.vue$/] : [],
6665
...(options.loaders && options.loaders[ext])
6766
}
6867
}
6968
]
7069
})
7170
))
72-
71+
// Fix paths not resolving in async imports
72+
// https://github.com/nuxt/typescript/issues/520
73+
if (this.nuxt.options.alias) {
74+
const { NormalModuleReplacementPlugin } = require('webpack')
75+
const aliases = Object.keys(this.nuxt.options.alias)
76+
config.plugins!.push(new NormalModuleReplacementPlugin(
77+
new RegExp(aliases.join('|')),
78+
(resource: any) => {
79+
const alias = aliases.find(alias => resource.request.startsWith(alias))
80+
if (alias) {
81+
resource.request = resource.request.replace(alias, this.nuxt.options.alias[alias])
82+
}
83+
}
84+
))
85+
}
7386
if (options.typeCheck && isClient && !isModern) {
7487
// eslint-disable-next-line @typescript-eslint/no-var-requires
7588
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
@@ -95,4 +108,4 @@ const tsModule: Module<Options> = function (moduleOptions) {
95108
})
96109
}
97110

98-
export default tsModule
111+
export default tsModule

0 commit comments

Comments
 (0)