Unverified Commit 717dfb6e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

[Backport] Revert "Fix tracing of server actions imported by client components (#78968) (#82167)

This backports https://github.com/vercel/next.js/pull/82161 to our
next-15-4 release branch
parent 6372ba03
...@@ -2015,7 +2015,6 @@ export default async function getBaseWebpackConfig( ...@@ -2015,7 +2015,6 @@ export default async function getBaseWebpackConfig(
appDirEnabled: hasAppDir, appDirEnabled: hasAppDir,
traceIgnores: [], traceIgnores: [],
compilerType, compilerType,
swcLoaderConfig: swcDefaultLoader,
} }
), ),
// Moment.js is an extremely popular library that bundles large locale files // Moment.js is an extremely popular library that bundles large locale files
......
...@@ -18,10 +18,8 @@ import picomatch from 'next/dist/compiled/picomatch' ...@@ -18,10 +18,8 @@ import picomatch from 'next/dist/compiled/picomatch'
import { getModuleBuildInfo } from '../loaders/get-module-build-info' import { getModuleBuildInfo } from '../loaders/get-module-build-info'
import { getPageFilePath } from '../../entries' import { getPageFilePath } from '../../entries'
import { resolveExternal } from '../../handle-externals' import { resolveExternal } from '../../handle-externals'
import swcLoader, { type SWCLoaderOptions } from '../loaders/next-swc-loader'
import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route' import { isMetadataRouteFile } from '../../../lib/metadata/is-metadata-route'
import { getCompilationSpan } from '../utils' import { getCompilationSpan } from '../utils'
import { isClientComponentEntryModule } from '../loaders/utils'
const PLUGIN_NAME = 'TraceEntryPointsPlugin' const PLUGIN_NAME = 'TraceEntryPointsPlugin'
export const TRACE_IGNORES = [ export const TRACE_IGNORES = [
...@@ -139,10 +137,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -139,10 +137,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
private traceIgnores: string[] private traceIgnores: string[]
private esmExternals?: NextConfigComplete['experimental']['esmExternals'] private esmExternals?: NextConfigComplete['experimental']['esmExternals']
private compilerType: CompilerNameValues private compilerType: CompilerNameValues
private swcLoaderConfig: {
loader: string
options: Partial<SWCLoaderOptions>
}
constructor({ constructor({
rootDir, rootDir,
...@@ -153,7 +147,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -153,7 +147,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
traceIgnores, traceIgnores,
esmExternals, esmExternals,
outputFileTracingRoot, outputFileTracingRoot,
swcLoaderConfig,
}: { }: {
rootDir: string rootDir: string
compilerType: CompilerNameValues compilerType: CompilerNameValues
...@@ -163,7 +156,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -163,7 +156,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
traceIgnores?: string[] traceIgnores?: string[]
outputFileTracingRoot?: string outputFileTracingRoot?: string
esmExternals?: NextConfigComplete['experimental']['esmExternals'] esmExternals?: NextConfigComplete['experimental']['esmExternals']
swcLoaderConfig: TraceEntryPointsPlugin['swcLoaderConfig']
}) { }) {
this.rootDir = rootDir this.rootDir = rootDir
this.appDir = appDir this.appDir = appDir
...@@ -174,7 +166,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -174,7 +166,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
this.traceIgnores = traceIgnores || [] this.traceIgnores = traceIgnores || []
this.tracingRoot = outputFileTracingRoot || rootDir this.tracingRoot = outputFileTracingRoot || rootDir
this.compilerType = compilerType this.compilerType = compilerType
this.swcLoaderConfig = swcLoaderConfig
} }
// Here we output all traced assets and webpack chunks to a // Here we output all traced assets and webpack chunks to a
...@@ -409,18 +400,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -409,18 +400,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
} }
}) })
const readOriginalSource = (path: string) => {
return new Promise<string | Buffer>((resolve) => {
compilation.inputFileSystem.readFile(path, (err, result) => {
if (err) {
// we can't throw here as that crashes build un-necessarily
return resolve('')
}
resolve(result || '')
})
})
}
const readFile = async ( const readFile = async (
path: string path: string
): Promise<Buffer | string | null> => { ): Promise<Buffer | string | null> => {
...@@ -429,60 +408,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance { ...@@ -429,60 +408,6 @@ export class TraceEntryPointsPlugin implements webpack.WebpackPluginInstance {
// map the transpiled source when available to avoid // map the transpiled source when available to avoid
// parse errors in node-file-trace // parse errors in node-file-trace
let source: Buffer | string = mod?.originalSource?.()?.buffer() let source: Buffer | string = mod?.originalSource?.()?.buffer()
try {
// fallback to reading raw source file, this may fail
// due to unsupported syntax but best effort attempt
let usingOriginalSource = false
if (!source || isClientComponentEntryModule(mod)) {
source = await readOriginalSource(path)
usingOriginalSource = true
}
const sourceString = source.toString()
// If this is a client component we need to trace the
// original transpiled source not the client proxy which is
// applied before this plugin is run due to the
// client-module-loader
if (
usingOriginalSource &&
// don't attempt transpiling CSS or image imports
path.match(/\.(tsx|ts|js|cjs|mjs|jsx)$/)
) {
let transformResolve: (result: string) => void
let transformReject: (error: unknown) => void
const transformPromise = new Promise<string>(
(resolve, reject) => {
transformResolve = resolve
transformReject = reject
}
)
// TODO: should we apply all loaders except the
// client-module-loader?
swcLoader.apply(
{
resourcePath: path,
getOptions: () => {
return this.swcLoaderConfig.options
},
async: () => {
return (err: unknown, result: string) => {
if (err) {
return transformReject(err)
}
return transformResolve(result)
}
},
},
[sourceString, undefined]
)
source = await transformPromise
}
} catch {
/* non-fatal */
}
return source || '' return source || ''
} }
......
import { nextTestSetup } from 'e2e-utils'
import path from 'path'
describe('action-tracing', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) return
it('should trace server action imported by client correctly', async () => {
const traceData = (await next.readJSON(
path.join('.next', 'server', 'app', 'page.js.nft.json')
)) as {
files: string[]
}
expect(traceData.files.some((file) => file.includes('data.txt'))).toBe(true)
})
})
'use server'
try {
if (process.env.NEXT_RUNTIME !== 'edge') {
require('fs').readFileSync(
require('path').join(process.cwd(), 'data.txt'),
'utf8'
)
}
} catch {}
export async function action() {
return 'action'
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use client'
import { action } from './action'
export default function Page() {
return (
<button
onClick={() => {
action()
}}
>
click
</button>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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