Skip to content

Commit 19d861a

Browse files
authored
fix dynamic import of amplify config (#3)
1 parent 0c33735 commit 19d861a

File tree

7 files changed

+60
-29
lines changed

7 files changed

+60
-29
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1-
# template-project
2-
Template for new project repositories
1+
# project-volunteer-platform
2+
3+
Platform concept to connect volunteers with projects and opportunities within the Helpful community.
4+
5+
## Getting started
6+
7+
```shell
8+
npm ci
9+
npm run build
10+
npm preview
11+
```
12+
13+
### Amplify considerations
14+
15+
By default, this project will generate a mock `amplify_outputs.json` file for local development.
16+
17+
If you would like to use a real Amplify configuration file, simply download from
18+
the AWS console or generate one with the following command:
19+
20+
```shell
21+
npm i @amplify/backend-cli
22+
npx apx generate
23+
```
24+
25+
Then you can run the standard build and preview commands as directed above.

mock_amplify_outputs.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build": "tsc && vite build",
8+
"prebuild": "node scripts/generate-config.cjs",
9+
"build": "npm run prebuild && vite build",
910
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1011
"preview": "vite preview",
1112
"test": "vitest run --passWithNoTests"

scripts/generate-config.cjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
// Path to amplify_outputs.json
5+
const amplifyOutputsPath = path.resolve(__dirname, "../amplify_outputs.json");
6+
7+
// Check if amplify_outputs.json exists
8+
if (!fs.existsSync(amplifyOutputsPath)) {
9+
console.log("amplify_outputs.json not found. Generating default configuration...");
10+
11+
// Default configuration
12+
const defaultConfig = {
13+
data: {
14+
url: "https://example.com/graphql",
15+
aws_region: "us-east-1",
16+
default_authorization_type: "AMAZON_COGNITO_USER_POOLS",
17+
authorization_types: ["AMAZON_COGNITO_USER_POOLS", "AWS_IAM"],
18+
},
19+
};
20+
21+
// Write the default configuration to amplify_outputs.json
22+
fs.writeFileSync(amplifyOutputsPath, JSON.stringify(defaultConfig, null, 2));
23+
console.log("Default amplify_outputs.json has been generated.");
24+
} else {
25+
console.log("amplify_outputs.json already exists. Skipping generation.");
26+
}

src/main.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,12 @@ import Dashboard from "./pages/Dashboard.tsx";
55
import "./index.css";
66
import { Amplify } from "aws-amplify";
77

8-
// Import the Amplify configuration
9-
// This is a workaround to avoid the error: "Cannot find module '../amplify_outputs.json' or its corresponding type declarations."
10-
// as we would like to avoid adding sensitive information to the repository and would like to
11-
// avoid maintaining long-running infrastructure for this project.
12-
let outputs;
13-
try {
14-
// If you would like to generate the amplify_outputs.json file, run something like:
15-
// npm i @amplify/backend-cli
16-
// npx apx generate
17-
outputs = require(`../amplify_outputs.json`);
18-
} catch (error) {
19-
outputs = require("../mock_amplify_outputs.json");
20-
}
8+
import outputs from "../amplify_outputs.json";
219

2210
Amplify.configure(outputs);
2311

2412
ReactDOM.createRoot(document.getElementById("root")!).render(
2513
<React.StrictMode>
26-
{/* <App /> */}
2714
<Dashboard />
2815
</React.StrictMode>
29-
);
16+
);

src/types/global.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.json" {
2+
const value: any;
3+
export default value;
4+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"isolatedModules": true,
1414
"noEmit": true,
1515
"jsx": "react-jsx",
16+
"esModuleInterop": true,
1617

1718
/* Linting */
1819
"strict": true,

0 commit comments

Comments
 (0)