Skip to content

Commit 447a77e

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 447a77e

File tree

8 files changed

+34
-20
lines changed

8 files changed

+34
-20
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ coverage
2828
.DS_Store
2929
.AppleDouble
3030
.LSOverride
31+
32+
GEMINI.md
33+
commit_message.txt

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
}

examples/class-api/minimal/pages/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Component, Vue } from 'vue-property-decorator'
99
1010
@Component
1111
export default class PageIndex extends Vue {
12-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
1312
message: string = 'This is a message'
1413
}
1514
</script>

examples/options-api/minimal/pages/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { defineComponent } from 'vue'
99
1010
export default defineComponent({
1111
data () {
12-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
1312
const message: string = 'This is a message'
1413
1514
return {

packages/typescript-build/src/index.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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'
88
import type { RuleSetUseItem } from 'webpack'
9+
import { NormalModuleReplacementPlugin } from 'webpack'
910

1011
export interface Options {
1112
ignoreNotFoundWarnings?: boolean
@@ -53,23 +54,35 @@ const tsModule: Module<Options> = function (moduleOptions) {
5354
const jsxRuleLoaders = config.module!.rules.find(r => (r.test as RegExp).test('.jsx'))!.use as RuleSetUseItem[]
5455
const babelLoader = jsxRuleLoaders[jsxRuleLoaders.length - 1]
5556

56-
config.module!.rules.push(...(['ts', 'tsx'] as const).map(ext =>
57-
({
58-
test: new RegExp(`\\.${ext}$`, 'i'),
59-
use: [
60-
babelLoader,
61-
{
62-
loader: 'ts-loader',
63-
options: {
64-
transpileOnly: true,
65-
appendTsxSuffixTo: ext === 'tsx' ? [/\.vue$/] : [],
66-
...(options.loaders && options.loaders[ext])
67-
}
57+
config.module!.rules.push(...(['ts', 'tsx'] as const).map(ext => ({
58+
test: new RegExp(`\\.${ext}$`),
59+
use: [
60+
babelLoader,
61+
{
62+
loader: 'ts-loader',
63+
options: {
64+
transpileOnly: true,
65+
appendTsxSuffixTo: ext === 'tsx' ? [/.vue$/] : [],
66+
...(options.loaders && options.loaders[ext])
6867
}
69-
]
70-
})
68+
}
69+
]
70+
})
7171
))
72-
72+
// Fix paths not resolving in async imports
73+
// https://github.com/nuxt/typescript/issues/520
74+
if (this.nuxt.options.alias) {
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')

packages/typescript-build/test/fixture/pages/about.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defineComponent } from 'vue'
33
export default defineComponent({
44
name: 'About',
55
render (h) {
6-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
76
const text: string = 'About Page'
87
return h('div', text)
98
}

packages/typescript-build/test/fixture/pages/contact.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defineComponent } from 'vue'
33
export default defineComponent({
44
name: 'Contact',
55
data () {
6-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
76
const text: string = 'Contact Page'
87
return { text }
98
},

packages/typescript-build/test/fixture/pages/index.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { defineComponent } from 'vue'
77
88
export default defineComponent({
99
data () {
10-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
1110
const text: string = 'Index Page'
1211
return { text }
1312
}

0 commit comments

Comments
 (0)