Skip to content
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

Fixed "Expected jsx identifier" error on TypeScript generics & angle bracket type assertions in .ts files #30619

Merged
merged 5 commits into from
Oct 30, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/next/build/webpack/loaders/next-swc-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/

function getSWCOptions({
isTypeScript,
filename,
isServer,
development,
isPageFile,
Expand All @@ -42,11 +42,15 @@ function getSWCOptions({
hasReactRefresh,
isCommonJS,
}) {
const isTSFile = filename.endsWith('.ts')
const isTypeScript = isTSFile || filename.endsWith('.tsx')

const jsc = {
parser: {
syntax: isTypeScript ? 'typescript' : 'ecmascript',
dynamicImport: true,
[isTypeScript ? 'tsx' : 'jsx']: true,
// Exclude regular TypeScript files from React transformation to prevent e.g. generic parameters and angle-bracket type assertion from being interpreted as JSX tags.
[isTypeScript ? 'tsx' : 'jsx']: isTSFile ? false : true,
},

transform: {
Expand Down Expand Up @@ -119,8 +123,6 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
// Make the loader async
const filename = this.resourcePath

const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx')

let loaderOptions = getOptions(this) || {}

const { isServer, pagesDir, hasReactRefresh } = loaderOptions
Expand All @@ -131,7 +133,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {

const swcOptions = getSWCOptions({
pagesDir,
isTypeScript,
filename,
isServer: isServer,
isPageFile,
development: this.mode === 'development',
Expand Down