2021-11-08 17:35:04 +01:00
const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
2021-11-26 15:30:32 +01:00
const regeneratorRuntimePath = require . resolve ( 'regenerator-runtime' )
2021-11-08 17:35:04 +01:00
function getBaseSWCOptions ( {
filename ,
development ,
hasReactRefresh ,
globalWindow ,
2021-11-13 18:26:13 +01:00
nextConfig ,
resolvedBaseUrl ,
jsConfig ,
2021-11-08 17:35:04 +01:00
} ) {
const isTSFile = filename . endsWith ( '.ts' )
const isTypeScript = isTSFile || filename . endsWith ( '.tsx' )
2021-11-13 18:26:13 +01:00
const paths = jsConfig ? . compilerOptions ? . paths
const enableDecorators = Boolean (
jsConfig ? . compilerOptions ? . experimentalDecorators
)
2021-11-08 17:35:04 +01:00
return {
jsc : {
2021-11-13 18:26:13 +01:00
... ( resolvedBaseUrl && paths
2021-11-11 01:16:15 +01:00
? {
2021-11-13 18:26:13 +01:00
baseUrl : resolvedBaseUrl ,
2021-11-11 01:16:15 +01:00
paths ,
}
: { } ) ,
2021-11-08 17:35:04 +01:00
parser : {
syntax : isTypeScript ? 'typescript' : 'ecmascript' ,
dynamicImport : true ,
2021-11-13 18:26:13 +01:00
decorators : enableDecorators ,
2021-11-08 17:35:04 +01:00
// 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 : {
2021-11-13 18:26:13 +01:00
legacyDecorator : enableDecorators ,
2021-11-08 17:35:04 +01:00
react : {
2021-11-13 18:26:13 +01:00
importSource : jsConfig ? . compilerOptions ? . jsxImportSource || 'react' ,
2021-11-08 17:35:04 +01:00
runtime : 'automatic' ,
pragma : 'React.createElement' ,
pragmaFrag : 'React.Fragment' ,
throwIfNamespace : true ,
development : development ,
useBuiltins : true ,
refresh : hasReactRefresh ,
} ,
optimizer : {
simplify : false ,
globals : {
typeofs : {
window : globalWindow ? 'object' : 'undefined' ,
} ,
2021-11-12 20:59:21 +01:00
envs : {
NODE _ENV : development ? '"development"' : '"production"' ,
} ,
// TODO: handle process.browser to match babel replacing as well
2021-11-08 17:35:04 +01:00
} ,
} ,
regenerator : {
2021-11-26 15:30:32 +01:00
importPath : regeneratorRuntimePath ,
2021-11-08 17:35:04 +01:00
} ,
} ,
} ,
2021-11-13 18:26:13 +01:00
styledComponents : nextConfig ? . experimental ? . styledComponents
2021-11-09 19:24:01 +01:00
? {
displayName : Boolean ( development ) ,
}
: null ,
2021-11-16 10:18:05 +01:00
removeConsole : nextConfig ? . experimental ? . removeConsole ,
2021-11-26 14:45:03 +01:00
reactRemoveProperties : nextConfig ? . experimental ? . reactRemoveProperties ,
2021-11-08 17:35:04 +01:00
}
}
2021-11-11 01:16:15 +01:00
export function getJestSWCOptions ( {
2021-11-11 16:05:32 +01:00
isServer ,
2021-11-11 01:16:15 +01:00
filename ,
esm ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// This is not passed yet as "paths" resolving needs a test first
// resolvedBaseUrl,
2021-11-11 01:16:15 +01:00
} ) {
2021-11-08 17:35:04 +01:00
let baseOptions = getBaseSWCOptions ( {
filename ,
development : false ,
hasReactRefresh : false ,
2021-11-11 16:05:32 +01:00
globalWindow : ! isServer ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// resolvedBaseUrl,
2021-11-08 17:35:04 +01:00
} )
const isNextDist = nextDistPath . test ( filename )
return {
... baseOptions ,
env : {
targets : {
// Targets the current version of Node.js
node : process . versions . node ,
} ,
} ,
module : {
type : esm && ! isNextDist ? 'es6' : 'commonjs' ,
} ,
disableNextSsg : true ,
disablePageConfig : true ,
}
}
export function getLoaderSWCOptions ( {
filename ,
development ,
isServer ,
pagesDir ,
isPageFile ,
hasReactRefresh ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// This is not passed yet as "paths" resolving is handled by webpack currently.
// resolvedBaseUrl,
2021-11-08 17:35:04 +01:00
} ) {
let baseOptions = getBaseSWCOptions ( {
filename ,
development ,
globalWindow : ! isServer ,
hasReactRefresh ,
2021-11-13 18:26:13 +01:00
nextConfig ,
jsConfig ,
// resolvedBaseUrl,
2021-11-08 17:35:04 +01:00
} )
const isNextDist = nextDistPath . test ( filename )
if ( isServer ) {
return {
... baseOptions ,
// Disables getStaticProps/getServerSideProps tree shaking on the server compilation for pages
disableNextSsg : true ,
disablePageConfig : true ,
isDevelopment : development ,
pagesDir ,
isPageFile ,
env : {
targets : {
// Targets the current version of Node.js
node : process . versions . node ,
} ,
} ,
}
} else {
// Matches default @babel/preset-env behavior
baseOptions . jsc . target = 'es5'
return {
... baseOptions ,
// Ensure Next.js internals are output as commonjs modules
... ( isNextDist
? {
module : {
type : 'commonjs' ,
} ,
}
: { } ) ,
disableNextSsg : ! isPageFile ,
isDevelopment : development ,
pagesDir ,
isPageFile ,
}
}
}