-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathindex.ts
More file actions
232 lines (214 loc) · 7.67 KB
/
index.ts
File metadata and controls
232 lines (214 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import type { NextConfig } from 'next';
import semver from 'semver';
import {
getNextBuilder,
shouldUseDeferredBuilder,
WORKFLOW_DEFERRED_ENTRIES,
} from './builder.js';
export function withWorkflow(
nextConfigOrFn:
| NextConfig
| ((
phase: string,
ctx: { defaultConfig: NextConfig }
) => Promise<NextConfig>),
{
workflows,
}: {
workflows?: {
lazyDiscovery?: boolean;
dirs?: string[];
local?: {
port?: number;
dataDir?: string;
};
};
} = {}
) {
if (workflows?.lazyDiscovery) {
process.env.WORKFLOW_NEXT_LAZY_DISCOVERY = '1';
}
if (!process.env.VERCEL_DEPLOYMENT_ID) {
if (!process.env.WORKFLOW_TARGET_WORLD) {
process.env.WORKFLOW_TARGET_WORLD = 'local';
process.env.WORKFLOW_LOCAL_DATA_DIR = '.next/workflow-data';
}
const maybePort = workflows?.local?.port;
if (maybePort) {
process.env.PORT = maybePort.toString();
}
} else {
if (!process.env.WORKFLOW_TARGET_WORLD) {
process.env.WORKFLOW_TARGET_WORLD = 'vercel';
}
}
return async function buildConfig(
phase: string,
ctx: { defaultConfig: NextConfig }
) {
const loaderPath = require.resolve('./loader');
let runDeferredBuildFromCallback: (() => Promise<void>) | undefined;
let nextConfig: NextConfig;
if (typeof nextConfigOrFn === 'function') {
nextConfig = await nextConfigOrFn(phase, ctx);
} else {
nextConfig = nextConfigOrFn;
}
// shallow clone to avoid read-only on top-level
nextConfig = Object.assign({}, nextConfig);
// configure the loader if turbopack is being used
if (!nextConfig.turbopack) {
nextConfig.turbopack = {};
}
if (!nextConfig.turbopack.rules) {
nextConfig.turbopack.rules = {};
}
const existingRules = nextConfig.turbopack.rules as any;
const nextVersion = require('next/package.json').version;
const supportsTurboCondition = semver.gte(nextVersion, 'v16.0.0');
const useDeferredBuilder = shouldUseDeferredBuilder(nextVersion);
// Deferred builder discovers files via loader socket notifications, so
// turbopack content conditions are only needed with the eager builder.
const shouldApplyTurboCondition =
supportsTurboCondition && !useDeferredBuilder;
const shouldWatch = process.env.NODE_ENV === 'development';
let workflowBuilderPromise: Promise<any> | undefined;
const getWorkflowBuilder = async () => {
if (!workflowBuilderPromise) {
workflowBuilderPromise = (async () => {
const NextBuilder = await getNextBuilder(nextVersion);
return new NextBuilder({
watch: shouldWatch,
// discover workflows from pages/app entries
dirs: workflows?.dirs || ['pages', 'app', 'src/pages', 'src/app'],
dirsAreEntrypoints: !workflows?.dirs,
workingDir: process.cwd(),
distDir: nextConfig.distDir || '.next',
buildTarget: 'next',
workflowsBundlePath: '', // not used in base
stepsBundlePath: '', // not used in base
webhookBundlePath: '', // node used in base
suppressCreateWorkflowsBundleLogs: useDeferredBuilder,
suppressCreateWorkflowsBundleWarnings: useDeferredBuilder,
suppressCreateWebhookBundleLogs: useDeferredBuilder,
suppressCreateManifestLogs: useDeferredBuilder,
externalPackages: [
// server-only and client-only are pseudo-packages handled by Next.js
// during its build process. We mark them as external to prevent esbuild
// from failing when bundling code that imports them.
// See: https://nextjs.org/docs/app/getting-started/server-and-client-components
'server-only',
'client-only',
...(nextConfig.serverExternalPackages || []),
],
});
})();
}
return workflowBuilderPromise;
};
if (useDeferredBuilder) {
runDeferredBuildFromCallback = async () => {
const workflowBuilder = await getWorkflowBuilder();
if (typeof workflowBuilder.onBeforeDeferredEntries === 'function') {
await workflowBuilder.onBeforeDeferredEntries();
}
};
const existingExperimental = (nextConfig.experimental ?? {}) as Record<
string,
any
>;
const existingDeferredEntries = Array.isArray(
existingExperimental.deferredEntries
)
? existingExperimental.deferredEntries
: [];
const existingOnBeforeDeferredEntries =
typeof existingExperimental.onBeforeDeferredEntries === 'function'
? existingExperimental.onBeforeDeferredEntries
: undefined;
nextConfig.experimental = {
...existingExperimental,
// biome-ignore lint/suspicious/noTsIgnore: expect-error is wrong as it will work on valid version
// @ts-ignore this is only available in canary Next.js
deferredEntries: [
...new Set([
...existingDeferredEntries,
...WORKFLOW_DEFERRED_ENTRIES,
]),
],
onBeforeDeferredEntries: async (...args: unknown[]) => {
if (existingOnBeforeDeferredEntries) {
await existingOnBeforeDeferredEntries(...args);
}
if (runDeferredBuildFromCallback) {
await runDeferredBuildFromCallback();
}
},
};
}
for (const key of [
'*.tsx',
'*.ts',
'*.jsx',
'*.js',
'*.mjs',
'*.mts',
'*.cjs',
'*.cts',
]) {
nextConfig.turbopack.rules[key] = {
...(shouldApplyTurboCondition
? {
condition: {
// Use 'all' to combine: must match content AND must NOT be in generated path
// Merge with any existing 'all' conditions from user config
all: [
...(existingRules[key]?.condition?.all || []),
// Exclude generated workflow route files from transformation
{ not: { path: /[/\\]\.well-known[/\\]workflow[/\\]/ } },
// Match files with workflow directives or custom serialization patterns
// Uses backreferences (\2, \3) to ensure matching quote types
{
content:
/(use workflow|use step|from\s+(['"])@workflow\/serde\2|Symbol\.for\s*\(\s*(['"])workflow-(?:serialize|deserialize)\3\s*\))/,
},
],
},
}
: {}),
loaders: [...(existingRules[key]?.loaders || []), loaderPath],
};
}
// configure the loader for webpack
const existingWebpackModify = nextConfig.webpack;
nextConfig.webpack = (...args) => {
const [webpackConfig] = args;
if (!webpackConfig.module) {
webpackConfig.module = {};
}
if (!webpackConfig.module.rules) {
webpackConfig.module.rules = [];
}
// loaders in webpack apply bottom->up so ensure
// ours comes before the default swc transform
webpackConfig.module.rules.push({
test: /.*\.(mjs|cjs|cts|ts|tsx|js|jsx)$/,
loader: loaderPath,
});
return existingWebpackModify
? existingWebpackModify(...args)
: webpackConfig;
};
// only run this in the main process so it only runs once
// as Next.js uses child processes for different builds
if (
!process.env.WORKFLOW_NEXT_PRIVATE_BUILT &&
phase !== 'phase-production-server'
) {
const workflowBuilder = await getWorkflowBuilder();
await workflowBuilder.build();
process.env.WORKFLOW_NEXT_PRIVATE_BUILT = '1';
}
return nextConfig;
};
}