Skip to content

Commit 717eded

Browse files
authored
Nextjs use spec (#253)
* add output bundle spec to common module * bump common module version * use new output bundle spec * resolve merge * copy over all resources * add packagejson update * fix lint * swap import to common * add packagejson update * fix e2e test * bump version
1 parent 4534609 commit 717eded

File tree

7 files changed

+137
-187
lines changed

7 files changed

+137
-187
lines changed

package-lock.json

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@apphosting/adapter-nextjs/e2e/run-local.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ await promiseSpawn("node", [buildScript], {
4949

5050
const bundleYaml = parseYaml(readFileSync(join(cwd, ".apphosting/bundle.yaml")).toString());
5151

52-
const runCommand = bundleYaml.runCommand;
52+
const runCommand = bundleYaml.serverConfig.runCommand;
5353

5454
if (typeof runCommand !== "string") {
5555
throw new Error("runCommand must be a string");

packages/@apphosting/adapter-nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apphosting/adapter-nextjs",
3-
"version": "14.0.7",
3+
"version": "14.0.8",
44
"main": "dist/index.js",
55
"description": "Experimental addon to the Firebase CLI to add web framework support",
66
"repository": {

packages/@apphosting/adapter-nextjs/src/bin/build.spec.ts

Lines changed: 70 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ describe("build commands", () => {
1313
beforeEach(() => {
1414
tmpDir = generateTmpDir();
1515
outputBundleOptions = {
16-
bundleYamlPath: path.join(tmpDir, ".apphosting/bundle.yaml"),
16+
bundleYamlPath: path.join(tmpDir, ".apphosting", "bundle.yaml"),
1717
outputDirectoryBasePath: path.join(tmpDir, ".apphosting"),
18-
outputDirectoryAppPath: path.join(tmpDir, ".apphosting"),
19-
outputPublicDirectoryPath: path.join(tmpDir, ".apphosting/public"),
20-
outputStaticDirectoryPath: path.join(tmpDir, ".apphosting/.next/static"),
21-
serverFilePath: path.join(tmpDir, ".apphosting/server.js"),
18+
outputDirectoryAppPath: path.join(tmpDir, ".next", "standalone"),
19+
outputPublicDirectoryPath: path.join(tmpDir, ".next", "standalone", "public"),
20+
outputStaticDirectoryPath: path.join(tmpDir, ".next", "standalone", ".next", "static"),
21+
serverFilePath: path.join(tmpDir, ".next", "standalone", "server.js"),
2222
};
2323
defaultNextVersion = "14.0.3";
2424
});
2525

2626
it("expects all output bundle files to be generated", async () => {
27-
const { generateOutputDirectory, validateOutputDirectory, createMetadata } = await importUtils;
27+
const { generateBuildOutput, validateOutputDirectory, createMetadata } = await importUtils;
2828
const files = {
2929
".next/standalone/server.js": "",
3030
".next/static/staticfile": "",
@@ -36,27 +36,21 @@ describe("build commands", () => {
3636
};
3737
const packageVersion = createMetadata(defaultNextVersion).adapterVersion;
3838
generateTestFiles(tmpDir, files);
39-
await generateOutputDirectory(
39+
await generateBuildOutput(
4040
tmpDir,
4141
tmpDir,
4242
outputBundleOptions,
4343
path.join(tmpDir, ".next"),
4444
defaultNextVersion,
4545
);
46-
await validateOutputDirectory(outputBundleOptions);
46+
await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next"));
4747

4848
const expectedFiles = {
49-
".apphosting/.next/static/staticfile": "",
50-
".apphosting/server.js": "",
51-
".apphosting/bundle.yaml": `headers: []
52-
redirects: []
53-
rewrites: []
54-
runCommand: node .apphosting/server.js
55-
neededDirs:
56-
- .apphosting
57-
staticAssets:
58-
- .apphosting/public
59-
env: []
49+
".next/standalone/.next/static/staticfile": "",
50+
".next/standalone/server.js": "",
51+
".apphosting/bundle.yaml": `version: v1
52+
serverConfig:
53+
runCommand: node .next/standalone/server.js
6054
metadata:
6155
adapterPackageName: "@apphosting/adapter-nextjs"
6256
adapterVersion: ${packageVersion}
@@ -68,155 +62,123 @@ metadata:
6862
});
6963

7064
it("moves files into correct location in a monorepo setup", async () => {
71-
const { generateOutputDirectory } = await importUtils;
65+
const { generateBuildOutput } = await importUtils;
7266
const files = {
7367
".next/standalone/apps/next-app/standalonefile": "",
7468
".next/static/staticfile": "",
7569
"public/publicfile": "",
76-
".next/routes-manifest.json": `{
77-
"headers":[],
78-
"rewrites":[],
79-
"redirects":[]
80-
}`,
8170
};
8271
generateTestFiles(tmpDir, files);
83-
await generateOutputDirectory(
72+
await generateBuildOutput(
8473
tmpDir,
8574
"apps/next-app",
8675
{
87-
bundleYamlPath: path.join(tmpDir, ".apphosting/bundle.yaml"),
76+
bundleYamlPath: path.join(tmpDir, ".apphosting", "bundle.yaml"),
8877
outputDirectoryBasePath: path.join(tmpDir, ".apphosting"),
89-
outputDirectoryAppPath: path.join(tmpDir, ".apphosting/apps/next-app"),
90-
outputPublicDirectoryPath: path.join(tmpDir, ".apphosting/apps/next-app/public"),
91-
outputStaticDirectoryPath: path.join(tmpDir, ".apphosting/apps/next-app/.next/static"),
92-
serverFilePath: path.join(tmpDir, ".apphosting/apps/next-app/server.js"),
78+
outputDirectoryAppPath: path.join(tmpDir, ".next", "standalone", "apps", "next-app"),
79+
outputPublicDirectoryPath: path.join(
80+
tmpDir,
81+
".next",
82+
"standalone",
83+
"apps",
84+
"next-app",
85+
"public",
86+
),
87+
outputStaticDirectoryPath: path.join(
88+
tmpDir,
89+
".next",
90+
"standalone",
91+
"apps",
92+
"next-app",
93+
".next",
94+
"static",
95+
),
96+
serverFilePath: path.join(tmpDir, ".next", "standalone", "apps", "next-app", "server.js"),
9397
},
9498
path.join(tmpDir, ".next"),
9599
defaultNextVersion,
96100
);
97101

98102
const expectedFiles = {
99-
".apphosting/apps/next-app/.next/static/staticfile": "",
100-
".apphosting/apps/next-app/standalonefile": "",
103+
".next/standalone/apps/next-app/.next/static/staticfile": "",
104+
".next/standalone/apps/next-app/standalonefile": "",
101105
};
102106
const expectedPartialYaml = {
103-
headers: [],
104-
rewrites: [],
105-
redirects: [],
106-
runCommand: "node .apphosting/apps/next-app/server.js",
107-
neededDirs: [".apphosting"],
108-
staticAssets: [".apphosting/apps/next-app/public"],
107+
version: "v1",
108+
serverConfig: { runCommand: "node .next/standalone/apps/next-app/server.js" },
109109
};
110110
validateTestFiles(tmpDir, expectedFiles);
111111
validatePartialYamlContents(tmpDir, ".apphosting/bundle.yaml", expectedPartialYaml);
112112
});
113113

114-
it("expects directories and other files to be copied over", async () => {
115-
const { generateOutputDirectory, validateOutputDirectory } = await importUtils;
114+
it("test failed validateOutputDirectory", async () => {
115+
const { generateBuildOutput, validateOutputDirectory } = await importUtils;
116116
const files = {
117-
".next/standalone/server.js": "",
117+
".next/standalone/notserver.js": "",
118118
".next/static/staticfile": "",
119-
"public/publicfile": "",
120-
extrafile: "",
121119
".next/routes-manifest.json": `{
122-
"headers":[],
123-
"rewrites":[],
124-
"redirects":[]
120+
"headers":[{"source":"source", "headers":["header1"]}],
121+
"rewrites":[{"source":"source", "destination":"destination"}],
122+
"redirects":[{"source":"source", "destination":"destination"}]
125123
}`,
126124
};
127125
generateTestFiles(tmpDir, files);
128-
await generateOutputDirectory(
126+
await generateBuildOutput(
129127
tmpDir,
130128
tmpDir,
131129
outputBundleOptions,
132130
path.join(tmpDir, ".next"),
133131
defaultNextVersion,
134132
);
135-
await validateOutputDirectory(outputBundleOptions);
136-
137-
const expectedFiles = {
138-
".apphosting/.next/static/staticfile": "",
139-
".apphosting/server.js": "",
140-
".apphosting/public/publicfile": "",
141-
".apphosting/extrafile": "",
142-
};
143-
const expectedPartialYaml = {
144-
headers: [],
145-
rewrites: [],
146-
redirects: [],
147-
runCommand: "node .apphosting/server.js",
148-
neededDirs: [".apphosting"],
149-
staticAssets: [".apphosting/public"],
150-
};
151-
validateTestFiles(tmpDir, expectedFiles);
152-
validatePartialYamlContents(tmpDir, ".apphosting/bundle.yaml", expectedPartialYaml);
133+
assert.rejects(
134+
async () => await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next")),
135+
);
153136
});
154-
155-
it("expects bundle.yaml headers/rewrites/redirects to be generated", async () => {
156-
const { generateOutputDirectory, validateOutputDirectory } = await importUtils;
137+
it("expects directories and other files to be copied over", async () => {
138+
const { generateBuildOutput, validateOutputDirectory } = await importUtils;
157139
const files = {
158140
".next/standalone/server.js": "",
159141
".next/static/staticfile": "",
142+
"public/publicfile": "",
143+
extrafile: "",
160144
".next/routes-manifest.json": `{
161-
"headers":[{"source":"source", "headers":["header1"]}],
162-
"rewrites":[{"source":"source", "destination":"destination"}],
163-
"redirects":[{"source":"source", "destination":"destination"}]
145+
"headers":[],
146+
"rewrites":[],
147+
"redirects":[]
164148
}`,
165149
};
166150
generateTestFiles(tmpDir, files);
167-
await generateOutputDirectory(
151+
await generateBuildOutput(
168152
tmpDir,
169153
tmpDir,
170154
outputBundleOptions,
171155
path.join(tmpDir, ".next"),
172156
defaultNextVersion,
173157
);
174-
await validateOutputDirectory(outputBundleOptions);
158+
await validateOutputDirectory(outputBundleOptions, path.join(tmpDir, ".next"));
175159

176160
const expectedFiles = {
177-
".apphosting/.next/static/staticfile": "",
178-
".apphosting/server.js": "",
179-
};
180-
const expectedPartialYaml = {
181-
headers: [{ source: "source", headers: ["header1"] }],
182-
rewrites: [{ source: "source", destination: "destination" }],
183-
redirects: [{ source: "source", destination: "destination" }],
161+
".next/standalone/.next/static/staticfile": "",
162+
".next/standalone/server.js": "",
163+
".next/standalone/public/publicfile": "",
164+
".next/standalone/extrafile": "",
184165
};
185166
validateTestFiles(tmpDir, expectedFiles);
186-
validatePartialYamlContents(tmpDir, ".apphosting/bundle.yaml", expectedPartialYaml);
187-
});
188-
it("test failed validateOutputDirectory", async () => {
189-
const { generateOutputDirectory, validateOutputDirectory } = await importUtils;
190-
const files = {
191-
".next/standalone/notserver.js": "",
192-
".next/static/staticfile": "",
193-
".next/routes-manifest.json": `{
194-
"headers":[{"source":"source", "headers":["header1"]}],
195-
"rewrites":[{"source":"source", "destination":"destination"}],
196-
"redirects":[{"source":"source", "destination":"destination"}]
197-
}`,
198-
};
199-
generateTestFiles(tmpDir, files);
200-
await generateOutputDirectory(
201-
tmpDir,
202-
tmpDir,
203-
outputBundleOptions,
204-
path.join(tmpDir, ".next"),
205-
defaultNextVersion,
206-
);
207-
assert.rejects(async () => await validateOutputDirectory(outputBundleOptions));
208167
});
209168
it("test populate output bundle options", async () => {
210169
const { populateOutputBundleOptions } = await importUtils;
211170
const expectedOutputBundleOptions = {
212171
bundleYamlPath: "test/.apphosting/bundle.yaml",
213172
outputDirectoryBasePath: "test/.apphosting",
214-
outputDirectoryAppPath: "test/.apphosting",
215-
outputPublicDirectoryPath: "test/.apphosting/public",
216-
outputStaticDirectoryPath: "test/.apphosting/.next/static",
217-
serverFilePath: "test/.apphosting/server.js",
173+
outputDirectoryAppPath: "test/.next/standalone",
174+
outputPublicDirectoryPath: "test/.next/standalone/public",
175+
outputStaticDirectoryPath: "test/.next/standalone/.next/static",
176+
serverFilePath: "test/.next/standalone/server.js",
218177
};
219-
assert.deepEqual(populateOutputBundleOptions("test", "test"), expectedOutputBundleOptions);
178+
assert.deepEqual(
179+
populateOutputBundleOptions("test", "test", "test/.next"),
180+
expectedOutputBundleOptions,
181+
);
220182
});
221183
afterEach(() => {
222184
fs.rmSync(tmpDir, { recursive: true, force: true });

packages/@apphosting/adapter-nextjs/src/bin/build.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {
33
loadConfig,
44
populateOutputBundleOptions,
5-
generateOutputDirectory,
5+
generateBuildOutput,
66
validateOutputDirectory,
77
} from "../utils.js";
88
import { join } from "path";
@@ -20,15 +20,20 @@ if (!process.env.FRAMEWORK_VERSION) {
2020
}
2121
await runBuild();
2222

23-
const outputBundleOptions = populateOutputBundleOptions(root, opts.projectDirectory);
2423
const { distDir } = await loadConfig(root, opts.projectDirectory);
2524
const nextBuildDirectory = join(opts.projectDirectory, distDir);
2625

27-
await generateOutputDirectory(
26+
const outputBundleOptions = populateOutputBundleOptions(
27+
root,
28+
opts.projectDirectory,
29+
nextBuildDirectory,
30+
);
31+
32+
await generateBuildOutput(
2833
root,
2934
opts.projectDirectory,
3035
outputBundleOptions,
3136
nextBuildDirectory,
3237
process.env.FRAMEWORK_VERSION,
3338
);
34-
await validateOutputDirectory(outputBundleOptions);
39+
await validateOutputDirectory(outputBundleOptions, nextBuildDirectory);

0 commit comments

Comments
 (0)