Skip to content

Commit fd60cb2

Browse files
committed
feat: throw error if API package.json does not exist
1 parent b729d3b commit fd60cb2

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
};
2424
```
2525

26-
You will need to create an `api/` folder in your project root containing a [`host.json`](https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json) (see sample below). The adapter will output the `render` Azure function for SSR in that folder. The `api` folder needs to be in your repo so that Azure can recognize the API at build time. However, you can add `api/render` to your .gitignore so that the generated function is not in source control.
26+
You will need to create an `api/` folder in your project root containing a [`host.json`](https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json) and a `package.json` (see samples below). The adapter will output the `render` Azure function for SSR in that folder. The `api` folder needs to be in your repo so that Azure can recognize the API at build time. However, you can add `api/render` to your .gitignore so that the generated function is not in source control.
2727

2828
### Sample `host.json`
2929

@@ -37,6 +37,14 @@ You will need to create an `api/` folder in your project root containing a [`hos
3737
}
3838
```
3939

40+
### Sample `package.json`
41+
42+
It's okay for this to be empty. Not including it causes the Azure Function build to fail.
43+
44+
```json
45+
{}
46+
```
47+
4048
## Azure configuration
4149

4250
When deploying to Azure, you will need to properly [configure your build](https://docs.microsoft.com/en-us/azure/static-web-apps/build-configuration?tabs=github-actions) so that both the static files and API are deployed.

files/api/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { writeFileSync } from 'fs';
1+
import { writeFileSync, existsSync } from 'fs';
22
import { join, posix } from 'path';
33
import { fileURLToPath } from 'url';
44
import esbuild from 'esbuild';
@@ -61,6 +61,12 @@ export default function ({ debug = false, customStaticWebAppConfig = {} } = {})
6161
}
6262
};
6363

64+
if (!existsSync(join('api', 'package.json'))) {
65+
throw new Error(
66+
'You need to create a package.json in your `api` directory. See the adapter README for details.'
67+
);
68+
}
69+
6470
const tmp = builder.getBuildDirectory('azure-tmp');
6571
const publish = 'build';
6672
const staticDir = join(publish, 'static');

package-lock.json

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

0 commit comments

Comments
 (0)