Skip to content

Commit

Permalink
Ensure optional chaining in swc matches babel (#33995)
Browse files Browse the repository at this point in the history
This ensures we always transpile optional chaining and nullish coalescing with swc the same as we do [with babel](https://github.com/vercel/next.js/blob/4812e229928dc01ae21ee0533685f6813694c136/packages/next/build/babel/preset.ts#L97-L98) since it can cause issues with webpack even when the node target supports these features. 

The specific case this seems to cause issues with webpack is when a value is imported and optional chaining is used on the import value webpack is stripping the optional chaining cc @sokra 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: #33915
  • Loading branch information
ijjk committed Feb 4, 2022
1 parent 25d93de commit a65e361
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/next/build/swc/options.js
Expand Up @@ -117,6 +117,13 @@ export function getJestSWCOptions({
// Targets the current version of Node.js
node: process.versions.node,
},
// we always transpile optional chaining and nullish coalescing
// since it can cause issues with webpack even if the node target
// supports them
include: [
'proposal-optional-chaining',
'proposal-nullish-coalescing-operator',
],
},
module: {
type: esm && !isNextDist ? 'es6' : 'commonjs',
Expand Down Expand Up @@ -165,6 +172,13 @@ export function getLoaderSWCOptions({
// Targets the current version of Node.js
node: process.versions.node,
},
// we always transpile optional chaining and nullish coalescing
// since it can cause issues with webpack even if the node target
// supports them
include: [
'proposal-optional-chaining',
'proposal-nullish-coalescing-operator',
],
},
}
} else {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/production/pages/about.js
@@ -1 +1,5 @@
import info from '../info.json'

console.log('info:', info?.something?.nested)

export default () => <div className="about-page">About Page</div>

0 comments on commit a65e361

Please sign in to comment.