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. ...@@ -29,12 +29,16 @@ DEALINGS IN THE SOFTWARE.
import { getOptions } from 'next/dist/compiled/loader-utils' import { getOptions } from 'next/dist/compiled/loader-utils'
import { transform } from '../../swc' import { transform } from '../../swc'
const nextDistPath =
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
function getSWCOptions({ function getSWCOptions({
isTypeScript, isTypeScript,
isServer, isServer,
development, development,
isPageFile, isPageFile,
pagesDir, pagesDir,
isNextDist,
}) { }) {
const jsc = { const jsc = {
parser: { parser: {
...@@ -72,6 +76,14 @@ function getSWCOptions({ ...@@ -72,6 +76,14 @@ function getSWCOptions({
// Matches default @babel/preset-env behavior // Matches default @babel/preset-env behavior
jsc.target = 'es5' jsc.target = 'es5'
return { return {
// Ensure Next.js internals are output as commonjs modules
...(isNextDist
? {
module: {
type: 'commonjs',
},
}
: {}),
disableNextSsg: !isPageFile, disableNextSsg: !isPageFile,
pagesDir, pagesDir,
jsc, jsc,
...@@ -90,12 +102,15 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -90,12 +102,15 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
const { isServer, pagesDir } = loaderOptions const { isServer, pagesDir } = loaderOptions
const isPageFile = filename.startsWith(pagesDir) const isPageFile = filename.startsWith(pagesDir)
const isNextDist = nextDistPath.test(filename)
const swcOptions = getSWCOptions({ const swcOptions = getSWCOptions({
pagesDir, pagesDir,
isTypeScript, isTypeScript,
isServer: isServer, isServer: isServer,
isPageFile, isPageFile,
development: this.mode === 'development', development: this.mode === 'development',
isNextDist,
}) })
const programmaticOptions = { const programmaticOptions = {
...@@ -147,10 +162,11 @@ export default function swcLoader(inputSource, inputSourceMap) { ...@@ -147,10 +162,11 @@ export default function swcLoader(inputSource, inputSourceMap) {
loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap) loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap)
) )
.then( .then(
([transformedSource, outputSourceMap]) => ([transformedSource, outputSourceMap]) => {
callback?.(null, transformedSource, outputSourceMap || inputSourceMap), callback(null, transformedSource, outputSourceMap || inputSourceMap)
},
(err) => { (err) => {
callback?.(err) callback(err)
} }
) )
} }
...@@ -151,19 +151,19 @@ export function createRouter(...args: RouterArgs): Router { ...@@ -151,19 +151,19 @@ export function createRouter(...args: RouterArgs): Router {
// This function is used to create the `withRouter` router instance // This function is used to create the `withRouter` router instance
export function makePublicRouterInstance(router: Router): NextRouter { export function makePublicRouterInstance(router: Router): NextRouter {
const _router = router as any const scopedRouter = router as any
const instance = {} as any const instance = {} as any
for (const property of urlPropertyFields) { for (const property of urlPropertyFields) {
if (typeof _router[property] === 'object') { if (typeof scopedRouter[property] === 'object') {
instance[property] = Object.assign( instance[property] = Object.assign(
Array.isArray(_router[property]) ? [] : {}, Array.isArray(scopedRouter[property]) ? [] : {},
_router[property] scopedRouter[property]
) // makes sure query is not stateful ) // makes sure query is not stateful
continue 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 // 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 { ...@@ -171,7 +171,7 @@ export function makePublicRouterInstance(router: Router): NextRouter {
coreMethodFields.forEach((field) => { coreMethodFields.forEach((field) => {
instance[field] = (...args: any[]) => { 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