-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
create-next-config-file.ts
73 lines (67 loc) · 2.16 KB
/
create-next-config-file.ts
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
import type { ExecutorContext } from '@nrwl/devkit';
import {
applyChangesToString,
ChangeType,
workspaceLayout,
workspaceRoot,
stripIndents,
} from '@nrwl/devkit';
import * as ts from 'typescript';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import type { NextBuildBuilderOptions } from '../../../utils/types';
import { findNodes } from 'nx/src/utils/typescript';
export function createNextConfigFile(
options: NextBuildBuilderOptions,
context: ExecutorContext
) {
const nextConfigPath = options.nextConfig
? join(context.root, options.nextConfig)
: join(context.root, options.root, 'next.config.js');
// Copy config file and our `with-nx.js` file to remove dependency on @nrwl/next for production build.
if (existsSync(nextConfigPath)) {
writeFileSync(join(options.outputPath, 'with-nx.js'), getWithNxContent());
writeFileSync(
join(options.outputPath, 'next.config.js'),
readFileSync(nextConfigPath)
.toString()
.replace('@nrwl/next/plugins/with-nx', './with-nx.js')
);
}
}
function getWithNxContent() {
const withNxFile = join(__dirname, '../../../../plugins/with-nx.js');
let withNxContent = readFileSync(withNxFile).toString();
const withNxSource = ts.createSourceFile(
withNxFile,
withNxContent,
ts.ScriptTarget.Latest,
true
);
const getWithNxContextDeclaration = findNodes(
withNxSource,
ts.SyntaxKind.FunctionDeclaration
)?.find(
(node: ts.FunctionDeclaration) => node.name?.text === 'getWithNxContext'
);
if (getWithNxContextDeclaration) {
withNxContent = applyChangesToString(withNxContent, [
{
type: ChangeType.Delete,
start: getWithNxContextDeclaration.getStart(withNxSource),
length: getWithNxContextDeclaration.getWidth(withNxSource),
},
{
type: ChangeType.Insert,
index: getWithNxContextDeclaration.getStart(withNxSource),
text: stripIndents`function getWithNxContext() {
return {
workspaceRoot: '${workspaceRoot}',
libsDir: '${workspaceLayout().libsDir}'
}
}`,
},
]);
}
return withNxContent;
}