create-payload-app 3.0.0-canary.a0b06af → 3.0.0-canary.a192632

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,17 @@
1
+ import { parse } from '@swc/core';
1
2
  import chalk from 'chalk';
2
3
  import { Syntax, parseModule } from 'esprima-next';
3
4
  import fs from 'fs';
4
5
  import { log, warning } from '../utils/log.js';
5
6
  export const withPayloadStatement = {
6
- cjs: `const { withPayload } = require('@payloadcms/next/withPayload')\n`,
7
- esm: `import { withPayload } from '@payloadcms/next/withPayload'\n`
7
+ cjs: `const { withPayload } = require("@payloadcms/next/withPayload");`,
8
+ esm: `import { withPayload } from "@payloadcms/next/withPayload";`,
9
+ ts: `import { withPayload } from "@payloadcms/next/withPayload";`
8
10
  };
9
- export const wrapNextConfig = (args)=>{
11
+ export const wrapNextConfig = async (args)=>{
10
12
  const { nextConfigPath, nextConfigType: configType } = args;
11
13
  const configContent = fs.readFileSync(nextConfigPath, 'utf8');
12
- const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(configContent, configType);
14
+ const { modifiedConfigContent: newConfig, success } = await parseAndModifyConfigContent(configContent, configType);
13
15
  if (!success) {
14
16
  return;
15
17
  }
@@ -17,80 +19,103 @@ export const wrapNextConfig = (args)=>{
17
19
  };
18
20
  /**
19
21
  * Parses config content with AST and wraps it with withPayload function
20
- */ export function parseAndModifyConfigContent(content, configType) {
21
- content = withPayloadStatement[configType] + content;
22
- let ast;
23
- try {
24
- ast = parseModule(content, {
25
- loc: true
26
- });
27
- } catch (error) {
28
- if (error instanceof Error) {
29
- warning(`Unable to parse Next config. Error: ${error.message} `);
30
- warnUserWrapNotSuccessful(configType);
31
- }
32
- return {
33
- modifiedConfigContent: content,
34
- success: false
35
- };
36
- }
37
- if (configType === 'esm') {
38
- const exportDefaultDeclaration = ast.body.find((p)=>p.type === Syntax.ExportDefaultDeclaration);
39
- const exportNamedDeclaration = ast.body.find((p)=>p.type === Syntax.ExportNamedDeclaration);
40
- if (!exportDefaultDeclaration && !exportNamedDeclaration) {
41
- throw new Error('Could not find ExportDefaultDeclaration in next.config.js');
42
- }
43
- if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {
44
- const modifiedConfigContent = insertBeforeAndAfter(content, exportDefaultDeclaration.declaration.loc);
22
+ */ export async function parseAndModifyConfigContent(content, configType) {
23
+ content = withPayloadStatement[configType] + '\n' + content;
24
+ console.log({
25
+ configType,
26
+ content
27
+ });
28
+ if (configType === 'cjs' || configType === 'esm') {
29
+ try {
30
+ const ast = parseModule(content, {
31
+ loc: true
32
+ });
33
+ if (configType === 'cjs') {
34
+ // Find `module.exports = X`
35
+ const moduleExports = ast.body.find((p)=>p.type === Syntax.ExpressionStatement && p.expression?.type === Syntax.AssignmentExpression && p.expression.left?.type === Syntax.MemberExpression && p.expression.left.object?.type === Syntax.Identifier && p.expression.left.object.name === 'module' && p.expression.left.property?.type === Syntax.Identifier && p.expression.left.property.name === 'exports');
36
+ if (moduleExports && moduleExports.expression.right?.loc) {
37
+ const modifiedConfigContent = insertBeforeAndAfter(content, moduleExports.expression.right.loc);
38
+ return {
39
+ modifiedConfigContent,
40
+ success: true
41
+ };
42
+ }
43
+ return Promise.resolve({
44
+ modifiedConfigContent: content,
45
+ success: false
46
+ });
47
+ } else if (configType === 'esm') {
48
+ const exportDefaultDeclaration = ast.body.find((p)=>p.type === Syntax.ExportDefaultDeclaration);
49
+ const exportNamedDeclaration = ast.body.find((p)=>p.type === Syntax.ExportNamedDeclaration);
50
+ if (!exportDefaultDeclaration && !exportNamedDeclaration) {
51
+ throw new Error('Could not find ExportDefaultDeclaration in next.config.js');
52
+ }
53
+ if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {
54
+ const modifiedConfigContent = insertBeforeAndAfter(content, exportDefaultDeclaration.declaration.loc);
55
+ return {
56
+ modifiedConfigContent,
57
+ success: true
58
+ };
59
+ } else if (exportNamedDeclaration) {
60
+ const exportSpecifier = exportNamedDeclaration.specifiers.find((s)=>s.type === 'ExportSpecifier' && s.exported?.name === 'default' && s.local?.type === 'Identifier' && s.local?.name);
61
+ if (exportSpecifier) {
62
+ warning('Could not automatically wrap next.config.js with withPayload.');
63
+ warning('Automatic wrapping of named exports as default not supported yet.');
64
+ warnUserWrapNotSuccessful(configType);
65
+ return {
66
+ modifiedConfigContent: content,
67
+ success: false
68
+ };
69
+ }
70
+ }
71
+ warning('Could not automatically wrap Next config with withPayload.');
72
+ warnUserWrapNotSuccessful(configType);
73
+ return Promise.resolve({
74
+ modifiedConfigContent: content,
75
+ success: false
76
+ });
77
+ }
78
+ } catch (error) {
79
+ if (error instanceof Error) {
80
+ warning(`Unable to parse Next config. Error: ${error.message} `);
81
+ warnUserWrapNotSuccessful(configType);
82
+ }
45
83
  return {
46
- modifiedConfigContent,
47
- success: true
84
+ modifiedConfigContent: content,
85
+ success: false
48
86
  };
49
- } else if (exportNamedDeclaration) {
50
- const exportSpecifier = exportNamedDeclaration.specifiers.find((s)=>s.type === 'ExportSpecifier' && s.exported?.name === 'default' && s.local?.type === 'Identifier' && s.local?.name);
51
- if (exportSpecifier) {
52
- warning('Could not automatically wrap next.config.js with withPayload.');
53
- warning('Automatic wrapping of named exports as default not supported yet.');
87
+ }
88
+ } else if (configType === 'ts') {
89
+ const { moduleItems, parseOffset } = await compileTypeScriptFileToAST(content);
90
+ const exportDefaultDeclaration = moduleItems.find((m)=>m.type === 'ExportDefaultExpression' && (m.expression.type === 'Identifier' || m.expression.type === 'CallExpression'));
91
+ if (exportDefaultDeclaration) {
92
+ if (!('span' in exportDefaultDeclaration.expression)) {
93
+ warning('Could not automatically wrap Next config with withPayload.');
54
94
  warnUserWrapNotSuccessful(configType);
55
- return {
95
+ return Promise.resolve({
56
96
  modifiedConfigContent: content,
57
97
  success: false
58
- };
98
+ });
59
99
  }
60
- }
61
- warning('Could not automatically wrap Next config with withPayload.');
62
- warnUserWrapNotSuccessful(configType);
63
- return {
64
- modifiedConfigContent: content,
65
- success: false
66
- };
67
- } else if (configType === 'cjs') {
68
- // Find `module.exports = X`
69
- const moduleExports = ast.body.find((p)=>p.type === Syntax.ExpressionStatement && p.expression?.type === Syntax.AssignmentExpression && p.expression.left?.type === Syntax.MemberExpression && p.expression.left.object?.type === Syntax.Identifier && p.expression.left.object.name === 'module' && p.expression.left.property?.type === Syntax.Identifier && p.expression.left.property.name === 'exports');
70
- if (moduleExports && moduleExports.expression.right?.loc) {
71
- const modifiedConfigContent = insertBeforeAndAfter(content, moduleExports.expression.right.loc);
100
+ const modifiedConfigContent = insertBeforeAndAfterSWC(content, exportDefaultDeclaration.expression.span, parseOffset);
72
101
  return {
73
102
  modifiedConfigContent,
74
103
  success: true
75
104
  };
76
105
  }
77
- return {
78
- modifiedConfigContent: content,
79
- success: false
80
- };
81
106
  }
82
107
  warning('Could not automatically wrap Next config with withPayload.');
83
108
  warnUserWrapNotSuccessful(configType);
84
- return {
109
+ return Promise.resolve({
85
110
  modifiedConfigContent: content,
86
111
  success: false
87
- };
112
+ });
88
113
  }
89
114
  function warnUserWrapNotSuccessful(configType) {
90
115
  // Output directions for user to update next.config.js
91
116
  const withPayloadMessage = `
92
117
 
93
- ${chalk.bold(`Please manually wrap your existing next.config.js with the withPayload function. Here is an example:`)}
118
+ ${chalk.bold(`Please manually wrap your existing Next config with the withPayload function. Here is an example:`)}
94
119
 
95
120
  ${withPayloadStatement[configType]}
96
121
 
@@ -98,7 +123,7 @@ function warnUserWrapNotSuccessful(configType) {
98
123
  // Your Next.js config here
99
124
  }
100
125
 
101
- ${configType === 'esm' ? 'export default withPayload(nextConfig)' : 'module.exports = withPayload(nextConfig)'}
126
+ ${configType === 'cjs' ? 'module.exports = withPayload(nextConfig)' : 'export default withPayload(nextConfig)'}
102
127
 
103
128
  `;
104
129
  log(withPayloadMessage);
@@ -119,5 +144,44 @@ function insertBeforeAndAfter(content, loc) {
119
144
  }
120
145
  return lines.join('\n');
121
146
  }
147
+ function insertBeforeAndAfterSWC(content, span, /**
148
+ * WARNING: This is ONLY for unit tests. Defaults to 0 otherwise.
149
+ *
150
+ * @see compileTypeScriptFileToAST
151
+ */ parseOffset) {
152
+ const { end: preOffsetEnd, start: preOffsetStart } = span;
153
+ const start = preOffsetStart - parseOffset;
154
+ const end = preOffsetEnd - parseOffset;
155
+ const insert = (pos, text)=>{
156
+ return content.slice(0, pos) + text + content.slice(pos);
157
+ };
158
+ // insert ) after end
159
+ content = insert(end - 1, ')');
160
+ // insert withPayload before start
161
+ content = insert(start - 1, 'withPayload(');
162
+ return content;
163
+ }
164
+ /**
165
+ * Compile typescript to AST using the swc compiler
166
+ */ async function compileTypeScriptFileToAST(fileContent) {
167
+ let parseOffset = 0;
168
+ /**
169
+ * WARNING: This is ONLY for unit tests.
170
+ *
171
+ * Multiple instances of swc DO NOT reset the .span.end value.
172
+ * During unit tests, the .spawn.end value is read and accounted for.
173
+ *
174
+ * https://github.com/swc-project/swc/issues/1366
175
+ */ if (process.env.NODE_ENV === 'test') {
176
+ parseOffset = (await parse('')).span.end;
177
+ }
178
+ const module = await parse(fileContent, {
179
+ syntax: 'typescript'
180
+ });
181
+ return {
182
+ moduleItems: module.body,
183
+ parseOffset
184
+ };
185
+ }
122
186
 
123
187
  //# sourceMappingURL=wrap-next-config.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/wrap-next-config.ts"],"sourcesContent":["import type { Program } from 'esprima-next'\n\nimport chalk from 'chalk'\nimport { Syntax, parseModule } from 'esprima-next'\nimport fs from 'fs'\n\nimport { log, warning } from '../utils/log.js'\n\nexport const withPayloadStatement = {\n cjs: `const { withPayload } = require('@payloadcms/next/withPayload')\\n`,\n esm: `import { withPayload } from '@payloadcms/next/withPayload'\\n`,\n}\n\ntype NextConfigType = 'cjs' | 'esm'\n\nexport const wrapNextConfig = (args: {\n nextConfigPath: string\n nextConfigType: NextConfigType\n}) => {\n const { nextConfigPath, nextConfigType: configType } = args\n const configContent = fs.readFileSync(nextConfigPath, 'utf8')\n const { modifiedConfigContent: newConfig, success } = parseAndModifyConfigContent(\n configContent,\n configType,\n )\n\n if (!success) {\n return\n }\n\n fs.writeFileSync(nextConfigPath, newConfig)\n}\n\n/**\n * Parses config content with AST and wraps it with withPayload function\n */\nexport function parseAndModifyConfigContent(\n content: string,\n configType: NextConfigType,\n): { modifiedConfigContent: string; success: boolean } {\n content = withPayloadStatement[configType] + content\n\n let ast: Program | undefined\n try {\n ast = parseModule(content, { loc: true })\n } catch (error: unknown) {\n if (error instanceof Error) {\n warning(`Unable to parse Next config. Error: ${error.message} `)\n warnUserWrapNotSuccessful(configType)\n }\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n\n if (configType === 'esm') {\n const exportDefaultDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportDefaultDeclaration,\n ) as Directive | undefined\n\n const exportNamedDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportNamedDeclaration,\n ) as ExportNamedDeclaration | undefined\n\n if (!exportDefaultDeclaration && !exportNamedDeclaration) {\n throw new Error('Could not find ExportDefaultDeclaration in next.config.js')\n }\n\n if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n exportDefaultDeclaration.declaration.loc,\n )\n return { modifiedConfigContent, success: true }\n } else if (exportNamedDeclaration) {\n const exportSpecifier = exportNamedDeclaration.specifiers.find(\n (s) =>\n s.type === 'ExportSpecifier' &&\n s.exported?.name === 'default' &&\n s.local?.type === 'Identifier' &&\n s.local?.name,\n )\n\n if (exportSpecifier) {\n warning('Could not automatically wrap next.config.js with withPayload.')\n warning('Automatic wrapping of named exports as default not supported yet.')\n\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n } else if (configType === 'cjs') {\n // Find `module.exports = X`\n const moduleExports = ast.body.find(\n (p) =>\n p.type === Syntax.ExpressionStatement &&\n p.expression?.type === Syntax.AssignmentExpression &&\n p.expression.left?.type === Syntax.MemberExpression &&\n p.expression.left.object?.type === Syntax.Identifier &&\n p.expression.left.object.name === 'module' &&\n p.expression.left.property?.type === Syntax.Identifier &&\n p.expression.left.property.name === 'exports',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ) as any\n\n if (moduleExports && moduleExports.expression.right?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n moduleExports.expression.right.loc,\n )\n return { modifiedConfigContent, success: true }\n }\n\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n}\n\nfunction warnUserWrapNotSuccessful(configType: NextConfigType) {\n // Output directions for user to update next.config.js\n const withPayloadMessage = `\n\n ${chalk.bold(`Please manually wrap your existing next.config.js with the withPayload function. Here is an example:`)}\n\n ${withPayloadStatement[configType]}\n\n const nextConfig = {\n // Your Next.js config here\n }\n\n ${configType === 'esm' ? 'export default withPayload(nextConfig)' : 'module.exports = withPayload(nextConfig)'}\n\n`\n\n log(withPayloadMessage)\n}\n\ntype Directive = {\n declaration?: {\n loc: Loc\n }\n}\n\ntype ExportNamedDeclaration = {\n declaration: null\n loc: Loc\n specifiers: {\n exported: {\n loc: Loc\n name: string\n type: string\n }\n loc: Loc\n local: {\n loc: Loc\n name: string\n type: string\n }\n type: string\n }[]\n type: string\n}\n\ntype Loc = {\n end: { column: number; line: number }\n start: { column: number; line: number }\n}\n\nfunction insertBeforeAndAfter(content: string, loc: Loc) {\n const { end, start } = loc\n const lines = content.split('\\n')\n\n const insert = (line: string, column: number, text: string) => {\n return line.slice(0, column) + text + line.slice(column)\n }\n\n // insert ) after end\n lines[end.line - 1] = insert(lines[end.line - 1], end.column, ')')\n // insert withPayload before start\n if (start.line === end.line) {\n lines[end.line - 1] = insert(lines[end.line - 1], start.column, 'withPayload(')\n } else {\n lines[start.line - 1] = insert(lines[start.line - 1], start.column, 'withPayload(')\n }\n\n return lines.join('\\n')\n}\n"],"names":["chalk","Syntax","parseModule","fs","log","warning","withPayloadStatement","cjs","esm","wrapNextConfig","args","nextConfigPath","nextConfigType","configType","configContent","readFileSync","modifiedConfigContent","newConfig","success","parseAndModifyConfigContent","writeFileSync","content","ast","loc","error","Error","message","warnUserWrapNotSuccessful","exportDefaultDeclaration","body","find","p","type","ExportDefaultDeclaration","exportNamedDeclaration","ExportNamedDeclaration","declaration","insertBeforeAndAfter","exportSpecifier","specifiers","s","exported","name","local","moduleExports","ExpressionStatement","expression","AssignmentExpression","left","MemberExpression","object","Identifier","property","right","withPayloadMessage","bold","end","start","lines","split","insert","line","column","text","slice","join"],"mappings":"AAEA,OAAOA,WAAW,QAAO;AACzB,SAASC,MAAM,EAAEC,WAAW,QAAQ,eAAc;AAClD,OAAOC,QAAQ,KAAI;AAEnB,SAASC,GAAG,EAAEC,OAAO,QAAQ,kBAAiB;AAE9C,OAAO,MAAMC,uBAAuB;IAClCC,KAAK,CAAC,iEAAiE,CAAC;IACxEC,KAAK,CAAC,4DAA4D,CAAC;AACrE,EAAC;AAID,OAAO,MAAMC,iBAAiB,CAACC;IAI7B,MAAM,EAAEC,cAAc,EAAEC,gBAAgBC,UAAU,EAAE,GAAGH;IACvD,MAAMI,gBAAgBX,GAAGY,YAAY,CAACJ,gBAAgB;IACtD,MAAM,EAAEK,uBAAuBC,SAAS,EAAEC,OAAO,EAAE,GAAGC,4BACpDL,eACAD;IAGF,IAAI,CAACK,SAAS;QACZ;IACF;IAEAf,GAAGiB,aAAa,CAACT,gBAAgBM;AACnC,EAAC;AAED;;CAEC,GACD,OAAO,SAASE,4BACdE,OAAe,EACfR,UAA0B;IAE1BQ,UAAUf,oBAAoB,CAACO,WAAW,GAAGQ;IAE7C,IAAIC;IACJ,IAAI;QACFA,MAAMpB,YAAYmB,SAAS;YAAEE,KAAK;QAAK;IACzC,EAAE,OAAOC,OAAgB;QACvB,IAAIA,iBAAiB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KAAK,CAACF;IACnD;IAEA,qBAAqB;IACrBJ,KAAK,CAACF,IAAIK,IAAI,GAAG,EAAE,GAAGD,OAAOF,KAAK,CAACF,IAAIK,IAAI,GAAG,EAAE,EAAEL,IAAIM,MAAM,EAAE;IAC9D,kCAAkC;IAClC,IAAIL,MAAMI,IAAI,KAAKL,IAAIK,IAAI,EAAE;QAC3BH,KAAK,CAACF,IAAIK,IAAI,GAAG,EAAE,GAAGD,OAAOF,KAAK,CAACF,IAAIK,IAAI,GAAG,EAAE,EAAEJ,MAAMK,MAAM,EAAE;IAClE,OAAO;QACLJ,KAAK,CAACD,MAAMI,IAAI,GAAG,EAAE,GAAGD,OAAOF,KAAK,CAACD,MAAMI,IAAI,GAAG,EAAE,EAAEJ,MAAMK,MAAM,EAAE;IACtE;IAEA,OAAOJ,MAAMO,IAAI,CAAC;AACpB"}
1
+ {"version":3,"sources":["../../src/lib/wrap-next-config.ts"],"sourcesContent":["import type { ExportDefaultExpression, ModuleItem } from '@swc/core'\n\nimport { parse } from '@swc/core'\nimport chalk from 'chalk'\nimport { Syntax, parseModule } from 'esprima-next'\nimport fs from 'fs'\n\nimport type { NextConfigType } from '../types.js'\n\nimport { log, warning } from '../utils/log.js'\n\nexport const withPayloadStatement = {\n cjs: `const { withPayload } = require(\"@payloadcms/next/withPayload\");`,\n esm: `import { withPayload } from \"@payloadcms/next/withPayload\";`,\n ts: `import { withPayload } from \"@payloadcms/next/withPayload\";`,\n}\n\nexport const wrapNextConfig = async (args: {\n nextConfigPath: string\n nextConfigType: NextConfigType\n}) => {\n const { nextConfigPath, nextConfigType: configType } = args\n const configContent = fs.readFileSync(nextConfigPath, 'utf8')\n const { modifiedConfigContent: newConfig, success } = await parseAndModifyConfigContent(\n configContent,\n configType,\n )\n\n if (!success) {\n return\n }\n\n fs.writeFileSync(nextConfigPath, newConfig)\n}\n\n/**\n * Parses config content with AST and wraps it with withPayload function\n */\nexport async function parseAndModifyConfigContent(\n content: string,\n configType: NextConfigType,\n): Promise<{ modifiedConfigContent: string; success: boolean }> {\n content = withPayloadStatement[configType] + '\\n' + content\n\n console.log({ configType, content })\n\n if (configType === 'cjs' || configType === 'esm') {\n try {\n const ast = parseModule(content, { loc: true })\n\n if (configType === 'cjs') {\n // Find `module.exports = X`\n const moduleExports = ast.body.find(\n (p) =>\n p.type === Syntax.ExpressionStatement &&\n p.expression?.type === Syntax.AssignmentExpression &&\n p.expression.left?.type === Syntax.MemberExpression &&\n p.expression.left.object?.type === Syntax.Identifier &&\n p.expression.left.object.name === 'module' &&\n p.expression.left.property?.type === Syntax.Identifier &&\n p.expression.left.property.name === 'exports',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ) as any\n\n if (moduleExports && moduleExports.expression.right?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n moduleExports.expression.right.loc,\n )\n return { modifiedConfigContent, success: true }\n }\n\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n } else if (configType === 'esm') {\n const exportDefaultDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportDefaultDeclaration,\n ) as Directive | undefined\n\n const exportNamedDeclaration = ast.body.find(\n (p) => p.type === Syntax.ExportNamedDeclaration,\n ) as ExportNamedDeclaration | undefined\n\n if (!exportDefaultDeclaration && !exportNamedDeclaration) {\n throw new Error('Could not find ExportDefaultDeclaration in next.config.js')\n }\n\n if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {\n const modifiedConfigContent = insertBeforeAndAfter(\n content,\n exportDefaultDeclaration.declaration.loc,\n )\n return { modifiedConfigContent, success: true }\n } else if (exportNamedDeclaration) {\n const exportSpecifier = exportNamedDeclaration.specifiers.find(\n (s) =>\n s.type === 'ExportSpecifier' &&\n s.exported?.name === 'default' &&\n s.local?.type === 'Identifier' &&\n s.local?.name,\n )\n\n if (exportSpecifier) {\n warning('Could not automatically wrap next.config.js with withPayload.')\n warning('Automatic wrapping of named exports as default not supported yet.')\n\n warnUserWrapNotSuccessful(configType)\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n }\n } catch (error: unknown) {\n if (error instanceof Error) {\n warning(`Unable to parse Next config. Error: ${error.message} `)\n warnUserWrapNotSuccessful(configType)\n }\n return {\n modifiedConfigContent: content,\n success: false,\n }\n }\n } else if (configType === 'ts') {\n const { moduleItems, parseOffset } = await compileTypeScriptFileToAST(content)\n\n const exportDefaultDeclaration = moduleItems.find(\n (m) =>\n m.type === 'ExportDefaultExpression' &&\n (m.expression.type === 'Identifier' || m.expression.type === 'CallExpression'),\n ) as ExportDefaultExpression | undefined\n\n if (exportDefaultDeclaration) {\n if (!('span' in exportDefaultDeclaration.expression)) {\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n }\n\n const modifiedConfigContent = insertBeforeAndAfterSWC(\n content,\n exportDefaultDeclaration.expression.span,\n parseOffset,\n )\n return { modifiedConfigContent, success: true }\n }\n }\n\n warning('Could not automatically wrap Next config with withPayload.')\n warnUserWrapNotSuccessful(configType)\n return Promise.resolve({\n modifiedConfigContent: content,\n success: false,\n })\n}\n\nfunction warnUserWrapNotSuccessful(configType: NextConfigType) {\n // Output directions for user to update next.config.js\n const withPayloadMessage = `\n\n ${chalk.bold(`Please manually wrap your existing Next config with the withPayload function. Here is an example:`)}\n\n ${withPayloadStatement[configType]}\n\n const nextConfig = {\n // Your Next.js config here\n }\n\n ${configType === 'cjs' ? 'module.exports = withPayload(nextConfig)' : 'export default withPayload(nextConfig)'}\n\n`\n\n log(withPayloadMessage)\n}\n\ntype Directive = {\n declaration?: {\n loc: Loc\n }\n}\n\ntype ExportNamedDeclaration = {\n declaration: null\n loc: Loc\n specifiers: {\n exported: {\n loc: Loc\n name: string\n type: string\n }\n loc: Loc\n local: {\n loc: Loc\n name: string\n type: string\n }\n type: string\n }[]\n type: string\n}\n\ntype Loc = {\n end: { column: number; line: number }\n start: { column: number; line: number }\n}\n\nfunction insertBeforeAndAfter(content: string, loc: Loc): string {\n const { end, start } = loc\n const lines = content.split('\\n')\n\n const insert = (line: string, column: number, text: string) => {\n return line.slice(0, column) + text + line.slice(column)\n }\n\n // insert ) after end\n lines[end.line - 1] = insert(lines[end.line - 1], end.column, ')')\n // insert withPayload before start\n if (start.line === end.line) {\n lines[end.line - 1] = insert(lines[end.line - 1], start.column, 'withPayload(')\n } else {\n lines[start.line - 1] = insert(lines[start.line - 1], start.column, 'withPayload(')\n }\n\n return lines.join('\\n')\n}\n\nfunction insertBeforeAndAfterSWC(\n content: string,\n span: ModuleItem['span'],\n /**\n * WARNING: This is ONLY for unit tests. Defaults to 0 otherwise.\n *\n * @see compileTypeScriptFileToAST\n */\n parseOffset: number,\n): string {\n const { end: preOffsetEnd, start: preOffsetStart } = span\n\n const start = preOffsetStart - parseOffset\n const end = preOffsetEnd - parseOffset\n\n const insert = (pos: number, text: string): string => {\n return content.slice(0, pos) + text + content.slice(pos)\n }\n\n // insert ) after end\n content = insert(end - 1, ')')\n // insert withPayload before start\n content = insert(start - 1, 'withPayload(')\n\n return content\n}\n\n/**\n * Compile typescript to AST using the swc compiler\n */\nasync function compileTypeScriptFileToAST(\n fileContent: string,\n): Promise<{ moduleItems: ModuleItem[]; parseOffset: number }> {\n let parseOffset = 0\n\n /**\n * WARNING: This is ONLY for unit tests.\n *\n * Multiple instances of swc DO NOT reset the .span.end value.\n * During unit tests, the .spawn.end value is read and accounted for.\n *\n * https://github.com/swc-project/swc/issues/1366\n */\n if (process.env.NODE_ENV === 'test') {\n parseOffset = (await parse('')).span.end\n }\n\n const module = await parse(fileContent, {\n syntax: 'typescript',\n })\n\n return { moduleItems: module.body, parseOffset }\n}\n"],"names":["parse","chalk","Syntax","parseModule","fs","log","warning","withPayloadStatement","cjs","esm","ts","wrapNextConfig","args","nextConfigPath","nextConfigType","configType","configContent","readFileSync","modifiedConfigContent","newConfig","success","parseAndModifyConfigContent","writeFileSync","content","console","ast","loc","moduleExports","body","find","p","type","ExpressionStatement","expression","AssignmentExpression","left","MemberExpression","object","Identifier","name","property","right","insertBeforeAndAfter","Promise","resolve","exportDefaultDeclaration","ExportDefaultDeclaration","exportNamedDeclaration","ExportNamedDeclaration","Error","declaration","exportSpecifier","specifiers","s","exported","local","warnUserWrapNotSuccessful","error","message","moduleItems","parseOffset","compileTypeScriptFileToAST","m","insertBeforeAndAfterSWC","span","withPayloadMessage","bold","end","start","lines","split","insert","line","column","text","slice","join","preOffsetEnd","preOffsetStart","pos","fileContent","process","env","NODE_ENV","module","syntax"],"mappings":"AAEA,SAASA,KAAK,QAAQ,YAAW;AACjC,OAAOC,WAAW,QAAO;AACzB,SAASC,MAAM,EAAEC,WAAW,QAAQ,eAAc;AAClD,OAAOC,QAAQ,KAAI;AAInB,SAASC,GAAG,EAAEC,OAAO,QAAQ,kBAAiB;AAE9C,OAAO,MAAMC,uBAAuB;IAClCC,KAAK,CAAC,gEAAgE,CAAC;IACvEC,KAAK,CAAC,2DAA2D,CAAC;IAClEC,IAAI,CAAC,2DAA2D,CAAC;AACnE,EAAC;AAED,OAAO,MAAMC,iBAAiB,OAAOC;IAInC,MAAM,EAAEC,cAAc,EAAEC,gBAAgBC,UAAU,EAAE,GAAGH;IACvD,MAAMI,gBAAgBZ,GAAGa,YAAY,CAACJ,gBAAgB;IACtD,MAAM,EAAEK,uBAAuBC,SAAS,EAAEC,OAAO,EAAE,GAAG,MAAMC,4BAC1DL,eACAD;IAGF,IAAI,CAACK,SAAS;QACZ;IACF;IAEAhB,GAAGkB,aAAa,CAACT,gBAAgBM;AACnC,EAAC;AAED;;CAEC,GACD,OAAO,eAAeE,4BACpBE,OAAe,EACfR,UAA0B;IAE1BQ,UAAUhB,oBAAoB,CAACQ,WAAW,GAAG,OAAOQ;IAEpDC,QAAQnB,GAAG,CAAC;QAAEU;QAAYQ;IAAQ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AM,EAAE;IAClE,OAAO;QACLJ,KAAK,CAACD,MAAMI,IAAI,GAAG,EAAE,GAAGD,OAAOF,KAAK,CAACD,MAAMI,IAAI,GAAG,EAAE,EAAEJ,MAAMK,MAAM,EAAE;IACtE;IAEA,OAAOJ,MAAMO,IAAI,CAAC;AACpB;AAEA,SAASb,wBACPxC,OAAe,EACfyC,IAAwB,EACxB;;;;GAIC,GACDJ,WAAmB;IAEnB,MAAM,EAAEO,KAAKU,YAAY,EAAET,OAAOU,cAAc,EAAE,GAAGd;IAErD,MAAMI,QAAQU,iBAAiBlB;IAC/B,MAAMO,MAAMU,eAAejB;IAE3B,MAAMW,SAAS,CAACQ,KAAaL;QAC3B,OAAOnD,QAAQoD,KAAK,CAAC,GAAGI,OAAOL,OAAOnD,QAAQoD,KAAK,CAACI;IACtD;IAEA,qBAAqB;IACrBxD,UAAUgD,OAAOJ,MAAM,GAAG;IAC1B,kCAAkC;IAClC5C,UAAUgD,OAAOH,QAAQ,GAAG;IAE5B,OAAO7C;AACT;AAEA;;CAEC,GACD,eAAesC,2BACbmB,WAAmB;IAEnB,IAAIpB,cAAc;IAElB;;;;;;;GAOC,GACD,IAAIqB,QAAQC,GAAG,CAACC,QAAQ,KAAK,QAAQ;QACnCvB,cAAc,AAAC,CAAA,MAAM5D,MAAM,GAAE,EAAGgE,IAAI,CAACG,GAAG;IAC1C;IAEA,MAAMiB,SAAS,MAAMpF,MAAMgF,aAAa;QACtCK,QAAQ;IACV;IAEA,OAAO;QAAE1B,aAAayB,OAAOxD,IAAI;QAAEgC;IAAY;AACjD"}
@@ -1,10 +1,41 @@
1
- import { parseAndModifyConfigContent, withPayloadStatement } from './wrap-next-config.js';
2
1
  import * as p from '@clack/prompts';
3
2
  import { jest } from '@jest/globals';
3
+ import { parseAndModifyConfigContent, withPayloadStatement } from './wrap-next-config.js';
4
+ const tsConfigs = {
5
+ defaultNextConfig: `import type { NextConfig } from "next";
6
+
7
+ const nextConfig: NextConfig = {};
8
+ export default nextConfig;`,
9
+ nextConfigExportNamedDefault: `import type { NextConfig } from "next";
10
+ const nextConfig: NextConfig = {};
11
+ const wrapped = someFunc(asdf);
12
+ export { wrapped as default };
13
+ `,
14
+ nextConfigWithFunc: `import type { NextConfig } from "next";
15
+ const nextConfig: NextConfig = {};
16
+ export default someFunc(nextConfig);
17
+ `,
18
+ nextConfigWithFuncMultiline: `import type { NextConfig } from "next";
19
+ const nextConfig: NextConfig = {};
20
+ export default someFunc(
21
+ nextConfig
22
+ );
23
+ `,
24
+ nextConfigWithSpread: `import type { NextConfig } from "next";
25
+ const nextConfig: NextConfig = {
26
+ ...someConfig,
27
+ };
28
+ export default nextConfig;
29
+ `
30
+ };
4
31
  const esmConfigs = {
5
32
  defaultNextConfig: `/** @type {import('next').NextConfig} */
6
33
  const nextConfig = {};
7
34
  export default nextConfig;
35
+ `,
36
+ nextConfigExportNamedDefault: `const nextConfig = {};
37
+ const wrapped = someFunc(asdf);
38
+ export { wrapped as default };
8
39
  `,
9
40
  nextConfigWithFunc: `const nextConfig = {};
10
41
  export default someFunc(nextConfig);
@@ -13,10 +44,6 @@ export default someFunc(nextConfig);
13
44
  export default someFunc(
14
45
  nextConfig
15
46
  );
16
- `,
17
- nextConfigExportNamedDefault: `const nextConfig = {};
18
- const wrapped = someFunc(asdf);
19
- export { wrapped as default };
20
47
  `,
21
48
  nextConfigWithSpread: `const nextConfig = {
22
49
  ...someConfig,
@@ -25,12 +52,16 @@ export default nextConfig;
25
52
  `
26
53
  };
27
54
  const cjsConfigs = {
55
+ anonConfig: `module.exports = {};`,
28
56
  defaultNextConfig: `
29
57
  /** @type {import('next').NextConfig} */
30
58
  const nextConfig = {};
31
59
  module.exports = nextConfig;
32
60
  `,
33
- anonConfig: `module.exports = {};`,
61
+ nextConfigExportNamedDefault: `const nextConfig = {};
62
+ const wrapped = someFunc(asdf);
63
+ module.exports = wrapped;
64
+ `,
34
65
  nextConfigWithFunc: `const nextConfig = {};
35
66
  module.exports = someFunc(nextConfig);
36
67
  `,
@@ -38,42 +69,61 @@ module.exports = someFunc(nextConfig);
38
69
  module.exports = someFunc(
39
70
  nextConfig
40
71
  );
41
- `,
42
- nextConfigExportNamedDefault: `const nextConfig = {};
43
- const wrapped = someFunc(asdf);
44
- module.exports = wrapped;
45
72
  `,
46
73
  nextConfigWithSpread: `const nextConfig = { ...someConfig };
47
74
  module.exports = nextConfig;
48
75
  `
49
76
  };
50
77
  describe('parseAndInsertWithPayload', ()=>{
78
+ describe('ts', ()=>{
79
+ const configType = 'ts';
80
+ const importStatement = withPayloadStatement[configType];
81
+ it('should parse the default next config', async ()=>{
82
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(tsConfigs.defaultNextConfig, configType);
83
+ expect(modifiedConfigContent).toContain(importStatement);
84
+ expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
85
+ });
86
+ it('should parse the config with a function', async ()=>{
87
+ const { modifiedConfigContent: modifiedConfigContent2 } = await parseAndModifyConfigContent(tsConfigs.nextConfigWithFunc, configType);
88
+ expect(modifiedConfigContent2).toContain('withPayload(someFunc(nextConfig))');
89
+ });
90
+ it('should parse the config with a multi-lined function', async ()=>{
91
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(tsConfigs.nextConfigWithFuncMultiline, configType);
92
+ expect(modifiedConfigContent).toContain(importStatement);
93
+ expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n {2}nextConfig\n\)\)/);
94
+ });
95
+ it('should parse the config with a spread', async ()=>{
96
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(tsConfigs.nextConfigWithSpread, configType);
97
+ expect(modifiedConfigContent).toContain(importStatement);
98
+ expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
99
+ });
100
+ });
51
101
  describe('esm', ()=>{
52
102
  const configType = 'esm';
53
103
  const importStatement = withPayloadStatement[configType];
54
- it('should parse the default next config', ()=>{
55
- const { modifiedConfigContent } = parseAndModifyConfigContent(esmConfigs.defaultNextConfig, configType);
104
+ it('should parse the default next config', async ()=>{
105
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(esmConfigs.defaultNextConfig, configType);
56
106
  expect(modifiedConfigContent).toContain(importStatement);
57
107
  expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
58
108
  });
59
- it('should parse the config with a function', ()=>{
60
- const { modifiedConfigContent } = parseAndModifyConfigContent(esmConfigs.nextConfigWithFunc, configType);
109
+ it('should parse the config with a function', async ()=>{
110
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(esmConfigs.nextConfigWithFunc, configType);
61
111
  expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))');
62
112
  });
63
- it('should parse the config with a function on a new line', ()=>{
64
- const { modifiedConfigContent } = parseAndModifyConfigContent(esmConfigs.nextConfigWithFuncMultiline, configType);
113
+ it('should parse the config with a multi-lined function', async ()=>{
114
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(esmConfigs.nextConfigWithFuncMultiline, configType);
65
115
  expect(modifiedConfigContent).toContain(importStatement);
66
- expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n nextConfig\n\)\)/);
116
+ expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n {2}nextConfig\n\)\)/);
67
117
  });
68
- it('should parse the config with a spread', ()=>{
69
- const { modifiedConfigContent } = parseAndModifyConfigContent(esmConfigs.nextConfigWithSpread, configType);
118
+ it('should parse the config with a spread', async ()=>{
119
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(esmConfigs.nextConfigWithSpread, configType);
70
120
  expect(modifiedConfigContent).toContain(importStatement);
71
121
  expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
72
122
  });
73
123
  // Unsupported: export { wrapped as default }
74
- it('should give warning with a named export as default', ()=>{
124
+ it('should give warning with a named export as default', async ()=>{
75
125
  const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(()=>{});
76
- const { modifiedConfigContent, success } = parseAndModifyConfigContent(esmConfigs.nextConfigExportNamedDefault, configType);
126
+ const { modifiedConfigContent, success } = await parseAndModifyConfigContent(esmConfigs.nextConfigExportNamedDefault, configType);
77
127
  expect(modifiedConfigContent).toContain(importStatement);
78
128
  expect(success).toBe(false);
79
129
  expect(warnLogSpy).toHaveBeenCalledWith(expect.stringContaining('Could not automatically wrap'));
@@ -82,32 +132,32 @@ describe('parseAndInsertWithPayload', ()=>{
82
132
  describe('cjs', ()=>{
83
133
  const configType = 'cjs';
84
134
  const requireStatement = withPayloadStatement[configType];
85
- it('should parse the default next config', ()=>{
86
- const { modifiedConfigContent } = parseAndModifyConfigContent(cjsConfigs.defaultNextConfig, configType);
135
+ it('should parse the default next config', async ()=>{
136
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(cjsConfigs.defaultNextConfig, configType);
87
137
  expect(modifiedConfigContent).toContain(requireStatement);
88
138
  expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
89
139
  });
90
- it('should parse anonymous default config', ()=>{
91
- const { modifiedConfigContent } = parseAndModifyConfigContent(cjsConfigs.anonConfig, configType);
140
+ it('should parse anonymous default config', async ()=>{
141
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(cjsConfigs.anonConfig, configType);
92
142
  expect(modifiedConfigContent).toContain(requireStatement);
93
143
  expect(modifiedConfigContent).toContain('withPayload({})');
94
144
  });
95
- it('should parse the config with a function', ()=>{
96
- const { modifiedConfigContent } = parseAndModifyConfigContent(cjsConfigs.nextConfigWithFunc, configType);
145
+ it('should parse the config with a function', async ()=>{
146
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(cjsConfigs.nextConfigWithFunc, configType);
97
147
  expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))');
98
148
  });
99
- it('should parse the config with a function on a new line', ()=>{
100
- const { modifiedConfigContent } = parseAndModifyConfigContent(cjsConfigs.nextConfigWithFuncMultiline, configType);
149
+ it('should parse the config with a multi-lined function', async ()=>{
150
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(cjsConfigs.nextConfigWithFuncMultiline, configType);
101
151
  expect(modifiedConfigContent).toContain(requireStatement);
102
- expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n nextConfig\n\)\)/);
152
+ expect(modifiedConfigContent).toMatch(/withPayload\(someFunc\(\n {2}nextConfig\n\)\)/);
103
153
  });
104
- it('should parse the config with a named export as default', ()=>{
105
- const { modifiedConfigContent } = parseAndModifyConfigContent(cjsConfigs.nextConfigExportNamedDefault, configType);
154
+ it('should parse the config with a named export as default', async ()=>{
155
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(cjsConfigs.nextConfigExportNamedDefault, configType);
106
156
  expect(modifiedConfigContent).toContain(requireStatement);
107
157
  expect(modifiedConfigContent).toContain('withPayload(wrapped)');
108
158
  });
109
- it('should parse the config with a spread', ()=>{
110
- const { modifiedConfigContent } = parseAndModifyConfigContent(cjsConfigs.nextConfigWithSpread, configType);
159
+ it('should parse the config with a spread', async ()=>{
160
+ const { modifiedConfigContent } = await parseAndModifyConfigContent(cjsConfigs.nextConfigWithSpread, configType);
111
161
  expect(modifiedConfigContent).toContain(requireStatement);
112
162
  expect(modifiedConfigContent).toContain('withPayload(nextConfig)');
113
163
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lib/wrap-next-config.spec.ts"],"sourcesContent":["import { parseAndModifyConfigContent, withPayloadStatement } from './wrap-next-config.js'\nimport * as p from '@clack/prompts'\nimport { jest } from '@jest/globals'\n\nconst esmConfigs = {\n defaultNextConfig: `/** @type {import('next').NextConfig} */\nconst nextConfig = {};\nexport default nextConfig;\n`,\n nextConfigWithFunc: `const nextConfig = {};\nexport default someFunc(nextConfig);\n`,\n nextConfigWithFuncMultiline: `const nextConfig = {};;\nexport default someFunc(\n nextConfig\n);\n`,\n nextConfigExportNamedDefault: `const nextConfig = {};\nconst wrapped = someFunc(asdf);\nexport { wrapped as default };\n`,\n nextConfigWithSpread: `const nextConfig = {\n ...someConfig,\n};\nexport default nextConfig;\n`,\n}\n\nconst cjsConfigs = {\n defaultNextConfig: `\n /** @type {import('next').NextConfig} */\nconst nextConfig = {};\nmodule.exports = nextConfig;\n`,\n anonConfig: `module.exports = {};`,\n nextConfigWithFunc: `const nextConfig = {};\nmodule.exports = someFunc(nextConfig);\n`,\n nextConfigWithFuncMultiline: `const nextConfig = {};\nmodule.exports = someFunc(\n nextConfig\n);\n`,\n nextConfigExportNamedDefault: `const nextConfig = {};\nconst wrapped = someFunc(asdf);\nmodule.exports = wrapped;\n`,\n nextConfigWithSpread: `const nextConfig = { ...someConfig };\nmodule.exports = nextConfig;\n`,\n}\n\ndescribe('parseAndInsertWithPayload', () => {\n describe('esm', () => {\n const configType = 'esm'\n const importStatement = withPayloadStatement[configType]\n it('should parse the default next config', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n esmConfigs.defaultNextConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n it('should parse the config with a function', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n esmConfigs.nextConfigWithFunc,\n configType,\n )\n expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')\n })\n\n it('should parse the config with a function on a new line', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n esmConfigs.nextConfigWithFuncMultiline,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n nextConfig\\n\\)\\)/)\n })\n\n it('should parse the config with a spread', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n esmConfigs.nextConfigWithSpread,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n\n // Unsupported: export { wrapped as default }\n it('should give warning with a named export as default', () => {\n const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})\n\n const { modifiedConfigContent, success } = parseAndModifyConfigContent(\n esmConfigs.nextConfigExportNamedDefault,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(success).toBe(false)\n\n expect(warnLogSpy).toHaveBeenCalledWith(\n expect.stringContaining('Could not automatically wrap'),\n )\n })\n })\n\n describe('cjs', () => {\n const configType = 'cjs'\n const requireStatement = withPayloadStatement[configType]\n it('should parse the default next config', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n cjsConfigs.defaultNextConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n it('should parse anonymous default config', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n cjsConfigs.anonConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload({})')\n })\n it('should parse the config with a function', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n cjsConfigs.nextConfigWithFunc,\n configType,\n )\n expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')\n })\n it('should parse the config with a function on a new line', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n cjsConfigs.nextConfigWithFuncMultiline,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n nextConfig\\n\\)\\)/)\n })\n it('should parse the config with a named export as default', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n cjsConfigs.nextConfigExportNamedDefault,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload(wrapped)')\n })\n\n it('should parse the config with a spread', () => {\n const { modifiedConfigContent } = parseAndModifyConfigContent(\n cjsConfigs.nextConfigWithSpread,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n })\n})\n"],"names":["parseAndModifyConfigContent","withPayloadStatement","p","jest","esmConfigs","defaultNextConfig","nextConfigWithFunc","nextConfigWithFuncMultiline","nextConfigExportNamedDefault","nextConfigWithSpread","cjsConfigs","anonConfig","describe","configType","importStatement","it","modifiedConfigContent","expect","toContain","toMatch","warnLogSpy","spyOn","log","mockImplementation","success","toBe","toHaveBeenCalledWith","stringContaining","requireStatement"],"mappings":"AAAA,SAASA,2BAA2B,EAAEC,oBAAoB,QAAQ,wBAAuB;AACzF,YAAYC,OAAO,iBAAgB;AACnC,SAASC,IAAI,QAAQ,gBAAe;AAEpC,MAAMC,aAAa;IACjBC,mBAAmB,CAAC;;;AAGtB,CAAC;IACCC,oBAAoB,CAAC;;AAEvB,CAAC;IACCC,6BAA6B,CAAC;;;;AAIhC,CAAC;IACCC,8BAA8B,CAAC;;;AAGjC,CAAC;IACCC,sBAAsB,CAAC;;;;AAIzB,CAAC;AACD;AAEA,MAAMC,aAAa;IACjBL,mBAAmB,CAAC;;;;AAItB,CAAC;IACCM,YAAY,CAAC,oBAAoB,CAAC;IAClCL,oBAAoB,CAAC;;AAEvB,CAAC;IACCC,6BAA6B,CAAC;;;;AAIhC,CAAC;IACCC,8BAA8B,CAAC;;;AAGjC,CAAC;IACCC,sBAAsB,CAAC;;AAEzB,CAAC;AACD;AAEAG,SAAS,6BAA6B;IACpCA,SAAS,OAAO;QACd,MAAMC,aAAa;QACnB,MAAMC,kBAAkBb,oBAAoB,CAACY,WAAW;QACxDE,GAAG,wCAAwC;YACzC,MAAM,EAAEC,qBAAqB,EAAE,GAAGhB,4BAChCI,WAAWC,iBAAiB,EAC5BQ;YAEFI,OAAOD,uBAAuBE,SAAS,CAACJ;YACxCG,OAAOD,uBAAuBE,SAAS,CAAC;QAC1C;QACAH,GAAG,2CAA2C;YAC5C,MAAM,EAAEC,qBAAqB,EAAE,GAAGhB,4BAChCI,WAAWE,kBAAkB,EAC7BO;YAEFI,OAAOD,uBAAuBE,SAAS,CAAC;QAC1C;QAEAH,GAAG,yDAAyD;YAC1D,MAAM,EAAEC,qBAAqB,EAAE,GAAGhB,4BAChCI,WAAWG,2BAA2B,EACtCM;YAEFI,OAAO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AAGhB,4BAChCU,WAAWH,2BAA2B,EACtCM;YAEFI,OAAOD,uBAAuBE,SAAS,CAACU;YACxCX,OAAOD,uBAAuBG,OAAO,CAAC;QACxC;QACAJ,GAAG,0DAA0D;YAC3D,MAAM,EAAEC,qBAAqB,EAAE,GAAGhB,4BAChCU,WAAWF,4BAA4B,EACvCK;YAEFI,OAAOD,uBAAuBE,SAAS,CAACU;YACxCX,OAAOD,uBAAuBE,SAAS,CAAC;QAC1C;QAEAH,GAAG,yCAAyC;YAC1C,MAAM,EAAEC,qBAAqB,EAAE,GAAGhB,4BAChCU,WAAWD,oBAAoB,EAC/BI;YAEFI,OAAOD,uBAAuBE,SAAS,CAACU;YACxCX,OAAOD,uBAAuBE,SAAS,CAAC;QAC1C;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/lib/wrap-next-config.spec.ts"],"sourcesContent":["import * as p from '@clack/prompts'\nimport { jest } from '@jest/globals'\n\nimport { parseAndModifyConfigContent, withPayloadStatement } from './wrap-next-config.js'\n\nconst tsConfigs = {\n defaultNextConfig: `import type { NextConfig } from \"next\";\n\nconst nextConfig: NextConfig = {};\nexport default nextConfig;`,\n\n nextConfigExportNamedDefault: `import type { NextConfig } from \"next\";\nconst nextConfig: NextConfig = {};\nconst wrapped = someFunc(asdf);\nexport { wrapped as default };\n`,\n nextConfigWithFunc: `import type { NextConfig } from \"next\";\nconst nextConfig: NextConfig = {};\nexport default someFunc(nextConfig);\n`,\n nextConfigWithFuncMultiline: `import type { NextConfig } from \"next\";\nconst nextConfig: NextConfig = {};\nexport default someFunc(\n nextConfig\n);\n`,\n nextConfigWithSpread: `import type { NextConfig } from \"next\";\nconst nextConfig: NextConfig = {\n ...someConfig,\n};\nexport default nextConfig;\n`,\n}\n\nconst esmConfigs = {\n defaultNextConfig: `/** @type {import('next').NextConfig} */\nconst nextConfig = {};\nexport default nextConfig;\n`,\n nextConfigExportNamedDefault: `const nextConfig = {};\nconst wrapped = someFunc(asdf);\nexport { wrapped as default };\n`,\n nextConfigWithFunc: `const nextConfig = {};\nexport default someFunc(nextConfig);\n`,\n nextConfigWithFuncMultiline: `const nextConfig = {};;\nexport default someFunc(\n nextConfig\n);\n`,\n nextConfigWithSpread: `const nextConfig = {\n ...someConfig,\n};\nexport default nextConfig;\n`,\n}\n\nconst cjsConfigs = {\n anonConfig: `module.exports = {};`,\n defaultNextConfig: `\n /** @type {import('next').NextConfig} */\nconst nextConfig = {};\nmodule.exports = nextConfig;\n`,\n nextConfigExportNamedDefault: `const nextConfig = {};\nconst wrapped = someFunc(asdf);\nmodule.exports = wrapped;\n`,\n nextConfigWithFunc: `const nextConfig = {};\nmodule.exports = someFunc(nextConfig);\n`,\n nextConfigWithFuncMultiline: `const nextConfig = {};\nmodule.exports = someFunc(\n nextConfig\n);\n`,\n nextConfigWithSpread: `const nextConfig = { ...someConfig };\nmodule.exports = nextConfig;\n`,\n}\n\ndescribe('parseAndInsertWithPayload', () => {\n describe('ts', () => {\n const configType = 'ts'\n const importStatement = withPayloadStatement[configType]\n\n it('should parse the default next config', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n tsConfigs.defaultNextConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n\n it('should parse the config with a function', async () => {\n const { modifiedConfigContent: modifiedConfigContent2 } = await parseAndModifyConfigContent(\n tsConfigs.nextConfigWithFunc,\n configType,\n )\n expect(modifiedConfigContent2).toContain('withPayload(someFunc(nextConfig))')\n })\n\n it('should parse the config with a multi-lined function', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n tsConfigs.nextConfigWithFuncMultiline,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n {2}nextConfig\\n\\)\\)/)\n })\n\n it('should parse the config with a spread', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n tsConfigs.nextConfigWithSpread,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n })\n describe('esm', () => {\n const configType = 'esm'\n const importStatement = withPayloadStatement[configType]\n it('should parse the default next config', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n esmConfigs.defaultNextConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n it('should parse the config with a function', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n esmConfigs.nextConfigWithFunc,\n configType,\n )\n expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')\n })\n\n it('should parse the config with a multi-lined function', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n esmConfigs.nextConfigWithFuncMultiline,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n {2}nextConfig\\n\\)\\)/)\n })\n\n it('should parse the config with a spread', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n esmConfigs.nextConfigWithSpread,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n\n // Unsupported: export { wrapped as default }\n it('should give warning with a named export as default', async () => {\n const warnLogSpy = jest.spyOn(p.log, 'warn').mockImplementation(() => {})\n\n const { modifiedConfigContent, success } = await parseAndModifyConfigContent(\n esmConfigs.nextConfigExportNamedDefault,\n configType,\n )\n expect(modifiedConfigContent).toContain(importStatement)\n expect(success).toBe(false)\n\n expect(warnLogSpy).toHaveBeenCalledWith(\n expect.stringContaining('Could not automatically wrap'),\n )\n })\n })\n\n describe('cjs', () => {\n const configType = 'cjs'\n const requireStatement = withPayloadStatement[configType]\n it('should parse the default next config', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n cjsConfigs.defaultNextConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n it('should parse anonymous default config', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n cjsConfigs.anonConfig,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload({})')\n })\n it('should parse the config with a function', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n cjsConfigs.nextConfigWithFunc,\n configType,\n )\n expect(modifiedConfigContent).toContain('withPayload(someFunc(nextConfig))')\n })\n it('should parse the config with a multi-lined function', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n cjsConfigs.nextConfigWithFuncMultiline,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toMatch(/withPayload\\(someFunc\\(\\n {2}nextConfig\\n\\)\\)/)\n })\n it('should parse the config with a named export as default', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n cjsConfigs.nextConfigExportNamedDefault,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload(wrapped)')\n })\n\n it('should parse the config with a spread', async () => {\n const { modifiedConfigContent } = await parseAndModifyConfigContent(\n cjsConfigs.nextConfigWithSpread,\n configType,\n )\n expect(modifiedConfigContent).toContain(requireStatement)\n expect(modifiedConfigContent).toContain('withPayload(nextConfig)')\n })\n })\n})\n"],"names":["p","jest","parseAndModifyConfigContent","withPayloadStatement","tsConfigs","defaultNextConfig","nextConfigExportNamedDefault","nextConfigWithFunc","nextConfigWithFuncMultiline","nextConfigWithSpread","esmConfigs","cjsConfigs","anonConfig","describe","configType","importStatement","it","modifiedConfigContent","expect","toContain","modifiedConfigContent2","toMatch","warnLogSpy","spyOn","log","mockImplementation","success","toBe","toHaveBeenCalledWith","stringContaining","requireStatement"],"mappings":"AAAA,YAAYA,OAAO,iBAAgB;AACnC,SAASC,IAAI,QAAQ,gBAAe;AAEpC,SAAS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C/BK;YAEFI,OAAOD,uBAAuBE,SAAS,CAACW;YACxCZ,OAAOD,uBAAuBE,SAAS,CAAC;QAC1C;IACF;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAsBzC,qBAAa,IAAI;IACf,IAAI,EAAE,OAAO,CAAA;;IA2CP,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAiK5B"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAsBzC,qBAAa,IAAI;IACf,IAAI,EAAE,OAAO,CAAA;;IA2CP,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAgL5B"}
package/dist/main.js CHANGED
@@ -65,7 +65,12 @@ export class Main {
65
65
  p.note("Welcome to Payload. Let's create a project!");
66
66
  // Detect if inside Next.js project
67
67
  const nextAppDetails = await getNextAppDetails(process.cwd());
68
- const { hasTopLevelLayout, isPayloadInstalled, nextAppDir, nextConfigPath } = nextAppDetails;
68
+ const { hasTopLevelLayout, isPayloadInstalled, isSupportedNextVersion, nextAppDir, nextConfigPath, nextVersion } = nextAppDetails;
69
+ if (nextConfigPath && !isSupportedNextVersion) {
70
+ p.log.warn(`Next.js v${nextVersion} is unsupported. Next.js >= 15 is required to use Payload.`);
71
+ p.outro(feedbackOutro());
72
+ process.exit(0);
73
+ }
69
74
  // Upgrade Payload in existing project
70
75
  if (isPayloadInstalled && nextConfigPath) {
71
76
  p.log.warn(`Payload installation detected in current project.`);