Skip to content

Commit c9b6db8

Browse files
committed
Fix turbopack config to interpret files as TSX instead of JS
1 parent 6f186d6 commit c9b6db8

File tree

4 files changed

+45
-93
lines changed

4 files changed

+45
-93
lines changed

server-actions-for-next-pages/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# server-actions-for-next-pages
22

3+
## 1.5.10
4+
5+
### Patch Changes
6+
7+
- Fix turbopack configuration bug where files were interpreted as JS instead of TSX
8+
- Restore `as: '*.tsx'` in turbopack loader config to ensure proper TypeScript/JSX interpretation
9+
- Restore browser/default structure in turbopack.rules for correct client/server detection
10+
- Revert turbopackLoader to use `export default` instead of `module.exports`
11+
- Remove experimental.turbo config, use only turbopack.rules
12+
- Remove incorrect Next.js 16 documentation comments
13+
314
## 1.5.9
415

516
### Patch Changes

server-actions-for-next-pages/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "server-actions-for-next-pages",
3-
"version": "1.5.9",
3+
"version": "1.5.10",
44
"description": "Use Next.js server actions in your pages directory",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

server-actions-for-next-pages/src/index.ts

Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -101,99 +101,47 @@ export function plugins({
101101
}
102102

103103
function applyTurbopackOptions(nextConfig: NextConfig): void {
104-
// ⚠️ IMPORTANT: Turbopack custom loaders are NOT working in Next.js 16.0.0-beta.0
105-
// Users MUST run builds with --webpack flag: `next build --webpack`
106-
// Only Next.js 15 with experimental.turbo is currently supported
107-
//
108-
// Official Turbopack documentation:
109-
// https://nextjs.org/docs/app/api-reference/next-config-js/turbopack
110-
// https://github.com/vercel/next.js/blob/canary/docs/01-app/03-api-reference/05-config/01-next-config-js/turbopack.mdx
111-
//
112-
// Advanced condition syntax (added in Next.js 16.0.0):
113-
// https://nextjs.org/docs/app/api-reference/next-config-js/turbopack#advanced-webpack-loader-conditions
114-
//
115-
// Boolean operators:
116-
// - { all: [...] } - all conditions must be true (AND)
117-
// - { any: [...] } - at least one condition must be true (OR)
118-
// - { not: ... } - negation (NOT)
119-
//
120-
// Customizable operators:
121-
// - { path: string | RegExp } - matches file path (glob or regex)
122-
// - { content: RegExp } - matches file content
123-
// - If path and content are in same object, acts as implicit AND
124-
//
125-
// Built-in conditions (strings):
126-
// - 'browser' - matches client-side code
127-
// - 'foreign' - matches node_modules and Next.js internals
128-
// - 'development' - matches next dev
129-
// - 'production' - matches next build
130-
// - 'node' - matches Node.js runtime
131-
// - 'edge-light' - matches Edge runtime
132-
//
133-
// Rules can be object or array of objects for different conditions
134-
//
135-
// Real-world examples from GitHub:
136-
// - https://github.com/EC-WIN-24-NET/Khala/blob/main/next.config.ts
137-
// - https://github.com/ShizNick84/Scalping_Alchemist_Bolt/blob/main/next.config.js
138-
//
139-
// Example syntax for Next.js 16 (when loaders are fixed):
140-
// turbopack: {
141-
// rules: {
142-
// '**/*.{ts,tsx,js,jsx}': [
143-
// {
144-
// condition: {
145-
// all: [
146-
// 'browser',
147-
// { not: 'foreign' },
148-
// { content: /"poor man's use server"|'poor man's use server'/ }
149-
// ]
150-
// },
151-
// loaders: [{ loader: loaderPath, options: { isServer: false, ... } }],
152-
// as: '*.js' // optional: output file extension
153-
// },
154-
// {
155-
// condition: {
156-
// all: [
157-
// { not: 'browser' },
158-
// { not: 'foreign' },
159-
// { content: /"poor man's use server"|'poor man's use server'/ }
160-
// ]
161-
// },
162-
// loaders: [{ loader: loaderPath, options: { isServer: true, ... } }],
163-
// as: '*.js'
164-
// }
165-
// ]
166-
// }
167-
// }
104+
// Configure Turbopack to apply Babel transformation to TypeScript/JavaScript files
105+
// Docs: https://nextjs.org/docs/app/api-reference/next-config-js/turbopack
168106

169107
const pagesDir = findPagesDir(process.cwd());
170108
const basePath = (nextConfig.basePath as string) || '/';
171109
const loaderPath = require.resolve('../dist/turbopackLoader');
172110

173-
// Next.js 15 experimental.turbo.rules configuration
174111
const loaderConfig = {
175-
loaders: [
176-
{
177-
loader: loaderPath,
178-
options: {
179-
isServer: true,
180-
pagesDir,
181-
isAppDir: false,
182-
basePath,
112+
browser: {
113+
as: '*.tsx',
114+
loaders: [
115+
{
116+
loader: loaderPath,
117+
options: {
118+
isServer: false,
119+
pagesDir,
120+
isAppDir: false,
121+
basePath,
122+
},
183123
},
184-
},
185-
],
124+
],
125+
},
126+
default: {
127+
as: '*.tsx',
128+
loaders: [
129+
{
130+
loader: loaderPath,
131+
options: {
132+
isServer: true,
133+
pagesDir,
134+
isAppDir: false,
135+
basePath,
136+
},
137+
},
138+
],
139+
},
186140
};
187141

188-
// Only configure experimental.turbo for Next.js 15
189-
// Next.js 16 turbopack does not properly support custom loaders yet
190-
nextConfig.experimental ??= {};
191-
(nextConfig.experimental as any).turbo ??= {};
192-
(nextConfig.experimental as any).turbo.rules ??= {};
193-
(nextConfig.experimental as any).turbo.rules['**/*.{ts,tsx,js,jsx}'] = loaderConfig;
194-
195-
// Do NOT configure turbopack.rules for Next.js 16 - it doesn't work in beta.0
196-
// Users must use --webpack flag with Next.js 16
142+
(nextConfig as any).turbopack ??= {};
143+
(nextConfig as any).turbopack.rules ??= {};
144+
(nextConfig as any).turbopack.rules['**/*.{ts,tsx,js,jsx}'] = loaderConfig;
197145
}
198146

199147
// taken from https://github.com/vercel/next.js/blob/v12.1.5/packages/next/lib/find-pages-dir.ts

server-actions-for-next-pages/src/turbopackLoader.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { transform } from '@babel/core';
33
import { plugins } from '.';
44
import { logger } from './utils';
55

6-
module.exports = async function (
6+
export default async function (
77
this: LoaderThis<any>,
88
source: string,
99
map: any,
@@ -13,11 +13,6 @@ module.exports = async function (
1313
map = JSON.parse(map);
1414
}
1515
try {
16-
// Skip transformation if file doesn't have the directive
17-
if (!source.includes('"poor man\'s use server"') && !source.includes("'poor man's use server'")) {
18-
return callback(null, source, map);
19-
}
20-
2116
const options = this.getOptions();
2217

2318
const res = transform(source || '', {
@@ -46,8 +41,6 @@ module.exports = async function (
4641
}
4742
}
4843

49-
module.exports.default = module.exports;
50-
5144
export type LoaderThis<Options> = {
5245
/**
5346
* Path to the file being loaded

0 commit comments

Comments
 (0)