Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions motoko/cert-var/frontend/src/actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import { createActor } from "./bindings/backend";
// via Set-Cookie header (see vite.config.js).
const canisterEnv = safeGetCanisterEnv();

// Resolve canister ID: cookie (icp-cli + dev server) → env var (dfx build-time)
const canisterId =
canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ??
process.env.CANISTER_ID_BACKEND;
const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"];

if (!canisterId) {
throw new Error(
"Canister ID for 'backend' not found. Run 'icp deploy' or 'dfx deploy' first."
"Canister ID for 'backend' not found. Run 'icp deploy' first."
);
}

Expand Down
99 changes: 42 additions & 57 deletions motoko/cert-var/frontend/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,60 @@
import { defineConfig, loadEnv } from "vite";
import { defineConfig } from "vite";
import { execSync } from "child_process";
import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite";

const CANISTER_NAME = "backend";
const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local";

function getDevServerConfig() {
// Try icp-cli first
let networkStatus;
try {
const canisterId = execSync("icp canister status backend -e local -i", {
encoding: "utf-8",
stdio: "pipe",
}).trim();
const networkStatus = JSON.parse(
execSync("icp network status --json", {
networkStatus = JSON.parse(
execSync(`icp network status -e ${ENVIRONMENT} --json`, {
encoding: "utf-8",
stdio: "pipe",
})
);
return {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": { target: "http://127.0.0.1:8000", changeOrigin: true },
},
};
} catch {}
} catch (error) {
if (String(error.stderr).includes("does not contain an environment named")) {
console.error(
`Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.`
);
} else {
console.error(
`No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` +
` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}`
);
}
process.exit(1);
}

// Try dfx
let canisterId;
try {
const pingResult = JSON.parse(
execSync("dfx ping", { encoding: "utf-8", stdio: "pipe" })
canisterId = execSync(
`icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`,
{ encoding: "utf-8", stdio: "pipe" }
).trim();
} catch {
console.error(
`Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` +
` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}`
);
const rootKeyHex = Buffer.from(pingResult.root_key).toString("hex");
const canisterId = execSync("dfx canister id backend", {
encoding: "utf-8",
stdio: "pipe",
}).trim();
return {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`ic_root_key=${rootKeyHex}&PUBLIC_CANISTER_ID:backend=${canisterId}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": {
target: "http://127.0.0.1:4943",
changeOrigin: true,
},
},
host: "127.0.0.1",
};
} catch {}
process.exit(1);
}

throw new Error(
"No local network running. Start with:\n icp network start -d && icp deploy\nor:\n dfx start --background && dfx deploy"
);
return {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": { target: networkStatus.api_url, changeOrigin: true },
},
};
}

export default defineConfig(({ command, mode }) => {
// dfx generates ../.env with CANISTER_ID_* vars on deploy. Bake them into the
// bundle so actor.js can fall back to them when the ic_env cookie does not
// contain canister IDs (dfx does not inject PUBLIC_CANISTER_ID:* env vars
// into the asset canister, unlike icp-cli).
const env = loadEnv(mode, "..", ["CANISTER_"]);

export default defineConfig(({ command }) => {
return {
base: "./",
plugins: [
Expand All @@ -73,11 +63,6 @@ export default defineConfig(({ command, mode }) => {
outDir: "./src/bindings",
}),
],
define: {
"process.env.CANISTER_ID_BACKEND": JSON.stringify(
env.CANISTER_ID_BACKEND
),
},
optimizeDeps: {
esbuildOptions: { define: { global: "globalThis" } },
},
Expand Down
63 changes: 43 additions & 20 deletions motoko/daily_planner/frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,56 @@ import { execSync } from "child_process";
import react from "@vitejs/plugin-react";
import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite";

const CANISTER_NAME = "backend";
const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local";

function getDevServerConfig() {
let networkStatus;
try {
const canisterId = execSync("icp canister status backend -e local -i", {
encoding: "utf-8",
stdio: "pipe",
}).trim();
const networkStatus = JSON.parse(
execSync("icp network status --json", {
networkStatus = JSON.parse(
execSync(`icp network status -e ${ENVIRONMENT} --json`, {
encoding: "utf-8",
stdio: "pipe",
})
);
return {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:backend=${canisterId}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": { target: "http://127.0.0.1:8000", changeOrigin: true },
},
};
} catch {}
} catch (error) {
if (String(error.stderr).includes("does not contain an environment named")) {
console.error(
`Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.`
);
} else {
console.error(
`No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` +
` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}`
);
}
process.exit(1);
}

let canisterId;
try {
canisterId = execSync(
`icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`,
{ encoding: "utf-8", stdio: "pipe" }
).trim();
} catch {
console.error(
`Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` +
` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}`
);
process.exit(1);
}

throw new Error(
"No local network running. Start with:\n icp network start -d && icp deploy"
);
return {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": { target: networkStatus.api_url, changeOrigin: true },
},
};
}

export default defineConfig(({ command }) => {
Expand Down
89 changes: 53 additions & 36 deletions motoko/evm_block_explorer/frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,58 @@ import { icpBindgen } from "@icp-sdk/bindgen/plugins/vite";
import react from "@vitejs/plugin-react";
import { execSync } from "child_process";

const CANISTER_NAME = "backend";
const ENVIRONMENT = process.env.ICP_ENVIRONMENT || "local";

function getDevServerConfig() {
let networkStatus;
try {
networkStatus = JSON.parse(
execSync(`icp network status -e ${ENVIRONMENT} --json`, {
encoding: "utf-8",
stdio: "pipe",
})
);
} catch (error) {
if (String(error.stderr).includes("does not contain an environment named")) {
console.error(
`Unknown environment "${ENVIRONMENT}". Check ICP_ENVIRONMENT against the environments in icp.yaml.`
);
} else {
console.error(
`No network running for environment "${ENVIRONMENT}". Start it and deploy first:\n` +
` icp network start -d -e ${ENVIRONMENT} && icp deploy -e ${ENVIRONMENT}`
);
}
process.exit(1);
}

let canisterId;
try {
canisterId = execSync(
`icp canister status ${CANISTER_NAME} -e ${ENVIRONMENT} -i`,
{ encoding: "utf-8", stdio: "pipe" }
).trim();
} catch {
console.error(
`Canister "${CANISTER_NAME}" is not deployed in environment "${ENVIRONMENT}". Deploy it first:\n` +
` icp deploy ${CANISTER_NAME} -e ${ENVIRONMENT}`
);
process.exit(1);
}

return {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`ic_root_key=${networkStatus.root_key}&PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": { target: networkStatus.api_url, changeOrigin: true },
},
};
}

export default defineConfig(({ command }) => {
const plugins = [
react(),
Expand All @@ -16,43 +68,8 @@ export default defineConfig(({ command }) => {
return { plugins };
}

const environment = process.env.ICP_ENVIRONMENT || "local";
const CANISTER_NAME = "backend";

const networkStatus = JSON.parse(
execSync(`icp network status -e ${environment} --json`, { encoding: "utf-8" })
);
const rootKey = networkStatus.root_key;
const proxyTarget = networkStatus.api_url;

let canisterId;
try {
canisterId = execSync(
`icp canister status ${CANISTER_NAME} -e ${environment} -i`,
{ encoding: "utf-8" }
).trim();
} catch {
console.error(`
Backend canister "${CANISTER_NAME}" not found in environment "${environment}"

Before running the dev server, deploy the backend canister:

icp deploy ${CANISTER_NAME} -e ${environment}
`);
process.exit(1);
}

return {
plugins,
server: {
headers: {
"Set-Cookie": `ic_env=${encodeURIComponent(
`PUBLIC_CANISTER_ID:${CANISTER_NAME}=${canisterId}&ic_root_key=${rootKey}`
)}; SameSite=Lax;`,
},
proxy: {
"/api": { target: proxyTarget, changeOrigin: true },
},
},
server: getDevServerConfig(),
};
});
4 changes: 1 addition & 3 deletions motoko/filevault/frontend/src/actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { createActor } from "./bindings/backend";

const canisterEnv = safeGetCanisterEnv();

const canisterId =
canisterEnv?.["PUBLIC_CANISTER_ID:backend"] ??
process.env.CANISTER_ID_BACKEND;
const canisterId = canisterEnv?.["PUBLIC_CANISTER_ID:backend"];

if (!canisterId) {
throw new Error(
Expand Down
Loading
Loading