Unverified Commit 10daef96 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure Next.js dist files are commonjs compatible with swcLoader enabled (#29138)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 60560a94
......@@ -29,12 +29,16 @@ DEALINGS IN THE SOFTWARE.
import { getOptions } from 'next/dist/compiled/loader-utils'
import { transform } from '../../swc'
const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
function getSWCOptions({
isTypeScript,
isServer,
development,
isPageFile,
pagesDir,
isNextDist,
}) {
const jsc = {
parser: {
......@@ -72,6 +76,14 @@ function getSWCOptions({
// Matches default @babel/preset-env behavior
jsc.target = 'es5'
return {
// Ensure Next.js internals are output as commonjs modules
...(isNextDist
? {
module: {
type: 'commonjs',
},
}
: {}),
disableNextSsg: !isPageFile,
pagesDir,
jsc,
......@@ -90,12 +102,15 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
const { isServer, pagesDir } = loaderOptions
const isPageFile = filename.startsWith(pagesDir)
const isNextDist = nextDistPath.test(filename)
const swcOptions = getSWCOptions({
pagesDir,
isTypeScript,
isServer: isServer,
isPageFile,
development: this.mode === 'development',
isNextDist,
})
const programmaticOptions = {
......@@ -147,10 +162,11 @@ export default function swcLoader(inputSource, inputSourceMap) {
loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap)
)
.then(
([transformedSource, outputSourceMap]) =>
callback?.(null, transformedSource, outputSourceMap || inputSourceMap),
([transformedSource, outputSourceMap]) => {
callback(null, transformedSource, outputSourceMap || inputSourceMap)
},
(err) => {
callback?.(err)
callback(err)
}
)
}
......@@ -151,19 +151,19 @@ export function createRouter(...args: RouterArgs): Router {
// This function is used to create the `withRouter` router instance
export function makePublicRouterInstance(router: Router): NextRouter {
const _router = router as any
const scopedRouter = router as any
const instance = {} as any
for (const property of urlPropertyFields) {
if (typeof _router[property] === 'object') {
if (typeof scopedRouter[property] === 'object') {
instance[property] = Object.assign(
Array.isArray(_router[property]) ? [] : {},
_router[property]
Array.isArray(scopedRouter[property]) ? [] : {},
scopedRouter[property]
) // makes sure query is not stateful
continue
}
instance[property] = _router[property]
instance[property] = scopedRouter[property]
}
// Events is a static property on the router, the router doesn't have to be initialized to use it
......@@ -171,7 +171,7 @@ export function makePublicRouterInstance(router: Router): NextRouter {
coreMethodFields.forEach((field) => {
instance[field] = (...args: any[]) => {
return _router[field](...args)
return scopedRouter[field](...args)
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment