Skip to content

fix: resolve dynamic import aliases in template literals #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ coverage
.DS_Store
.AppleDouble
.LSOverride

GEMINI.md
commit_message.txt
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = function (api) {
node: 'current'
}
}]
],
plugins: [
'@babel/plugin-transform-runtime'
]
}
}
Expand Down
1 change: 0 additions & 1 deletion examples/class-api/minimal/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Component, Vue } from 'vue-property-decorator'

@Component
export default class PageIndex extends Vue {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
message: string = 'This is a message'
}
</script>
1 change: 0 additions & 1 deletion examples/options-api/minimal/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { defineComponent } from 'vue'

export default defineComponent({
data () {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
const message: string = 'This is a message'

return {
Expand Down
80 changes: 46 additions & 34 deletions packages/typescript-build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import consola from 'consola'
import type { Module } from '@nuxt/types'
import type { Options as TsLoaderOptions } from 'ts-loader'
import type { ForkTsCheckerWebpackPluginOptions as TsCheckerOptions } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPluginOptions'
import type TsCheckerLogger from 'fork-ts-checker-webpack-plugin/lib/logger/Logger'
import type { RuleSetUseItem } from 'webpack'

export interface Options {
ignoreNotFoundWarnings?: boolean
ignoreNotFoundWarnings?: boolean;
loaders?: {
ts?: Partial<TsLoaderOptions>
tsx?: Partial<TsLoaderOptions>
}
typeCheck?: TsCheckerOptions | boolean
ts?: Partial<TsLoaderOptions>;
tsx?: Partial<TsLoaderOptions>;
};
typeCheck?: TsCheckerOptions | boolean;
}

declare module '@nuxt/types' {
interface NuxtOptions {
typescript: Options
typescript: Options;
}
}

Expand All @@ -42,55 +41,68 @@ const tsModule: Module<Options> = function (moduleOptions) {
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.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[]
// Add alias for @babel/runtime/helpers
// https://github.com/nuxt/typescript/issues/645
try {
config.resolve!.alias = {
...config.resolve!.alias,
'@babel/runtime/helpers': path.resolve(
this.options.rootDir!,
'node_modules/@babel/runtime/helpers'
)
}
} catch (e) {
// @babel/runtime may not be present
}

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'),
config.module!.rules.push(
...(['ts', 'tsx'] as const).map(ext => ({
test: new RegExp(`\\.${ext}$`),
use: [
babelLoader,
{
loader: 'ts-loader',
options: {
transpileOnly: true,
appendTsxSuffixTo: ext === 'tsx' ? [/\.vue$/] : [],
appendTsxSuffixTo: ext === 'tsx' ? [/.vue$/] : [],
...(options.loaders && options.loaders[ext])
}
}
]
})
))
}))
)

if (options.typeCheck && isClient && !isModern) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const logger = consola.withTag('nuxt:typescript')
/* istanbul ignore next */
const loggerInterface: TsCheckerLogger = {
log (message) { logger.log(message) },
info (message) { logger.info(message) },
error (message) { logger.error(message) }
}
config.plugins!.push(new ForkTsCheckerWebpackPlugin(defu(options.typeCheck, {
typescript: {
configFile: path.resolve(this.options.rootDir!, 'tsconfig.json'),
extensions: {
vue: true
}
},
logger: {
issues: loggerInterface
}
} as TsCheckerOptions)))
config.plugins!.push(
new ForkTsCheckerWebpackPlugin(
defu(options.typeCheck, {
typescript: {
configFile: path.resolve(this.options.rootDir!, 'tsconfig.json'),
extensions: {
vue: true
}
},
logger: consola
} as TsCheckerOptions)
)
)
}
})
}
Expand Down
1 change: 0 additions & 1 deletion packages/typescript-build/test/fixture/pages/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { defineComponent } from 'vue'
export default defineComponent({
name: 'About',
render (h) {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
const text: string = 'About Page'
return h('div', text)
}
Expand Down
1 change: 0 additions & 1 deletion packages/typescript-build/test/fixture/pages/contact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { defineComponent } from 'vue'
export default defineComponent({
name: 'Contact',
data () {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
const text: string = 'Contact Page'
return { text }
},
Expand Down
1 change: 0 additions & 1 deletion packages/typescript-build/test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { defineComponent } from 'vue'

export default defineComponent({
data () {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
const text: string = 'Index Page'
return { text }
}
Expand Down
Loading