Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(edgio): support v7 edgio application #2067

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 5 additions & 4 deletions docs/content/2.deploy/20.providers/edgio.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ You can use Nitropack to test your app's developement experience locally:
NITRO_PRESET=edgio npx nitropack build
```

To simulate on local how your app would run in production with Edgio, run the following command:
To simulate on local how your app would run in production with Edgio, locate the output directory and run the following command:

```bash
edgio build && edgio run --production
# cd `.output` dir and install dependencies before then
npm run preview
```

## Deploying from your local machine

Once you have tested your application locally, you may deploy using:

```bash
edgio deploy
npm run deploy
```

## Deploying using CI/CD

If you are deploying from a non-interactive environment, you will need to create an account on [Edgio Developer Console](https://app.layer0.co) first and setup a [deploy token](https://docs.edg.io/guides/basics/deployments#deploy-from-ci). Once the deploy token is created, save it as a secret to your environment. You can start the deploy by running:

```bash
edgio deploy --token=XXX
npm run deploy --token=XXX
```
121 changes: 101 additions & 20 deletions src/presets/edgio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export const edgio = defineNitroPreset({
{
connector: "./edgio",
routes: "./routes.js",
backends: {},
includeFiles: {
"server/**": true,
},
},
null,
2
Expand All @@ -43,32 +39,117 @@ export const edgio = defineNitroPreset({
await writeFile(
resolve(nitro.options.output.dir, "routes.js"),
`
import { Router } from '@edgio/core/router'
import { isProductionBuild } from '@edgio/core/environment'

const router = new Router()
import { edgioRoutes } from "@edgio/core";
import { isProductionBuild } from "@edgio/core/environment";
import { Router } from "@edgio/core/router";

const router = new Router();
router.match("/:path*", ({ renderWithApp }) => {
renderWithApp();
});
if (isProductionBuild()) {
router.static('public')
router.static("public");
}
router.use(edgioRoutes);

export default router;
`.trim()
);

// Write edgio/build.js
await writeFile(
resolve(nitro.options.output.dir, "edgio/build.js"),
`
const path = require("path");
const deploy = require("@edgio/core/deploy");
const FrameworkBuildError = require("@edgio/core/errors/FrameworkBuildError");

function _interopDefaultCompat(e) {
return e && typeof e === "object" && "default" in e ? e.default : e;
}

const FrameworkBuildError__default =
/*#__PURE__*/ _interopDefaultCompat(FrameworkBuildError);

const appDir = process.cwd();
async function build(options) {
const builder = new deploy.DeploymentBuilder();
builder.clearPreviousBuildOutput();
if (!options.skipFramework) {
const command = "node ./server/index.mjs";
try {
await builder.exec(command);
} catch (e) {
throw new FrameworkBuildError__default("Nitropack", command, e);
}
}
builder.addJSAsset(path.join(appDir, "server"));
await builder.build();
}

module.exports = build;
`.trim()
);

// Write edgio/dev.js
await writeFile(
resolve(nitro.options.output.dir, "edgio/dev.js"),
`
const dev$1 = require("@edgio/core/dev");

function dev() {
const isWin = process.platform === "win32";
return dev$1.createDevServer({
label: "Nitropack",
command: (port) =>
isWin
? ${"`set PORT=${port} && node ./server/index.mjs`"}
: ${"`PORT=${port} node ./server/index.mjs`"},
ready: [/localhost:/i],
});
}

router.fallback(({ renderWithApp }) => { renderWithApp() })
module.exports = dev;
`.trim()
);

// Write edgio/init.js
await writeFile(
resolve(nitro.options.output.dir, "edgio/init.js"),
`
const path = require("path");
const deploy = require("@edgio/core/deploy");

export default router
`.trim()
function init() {
new deploy.DeploymentBuilder(process.cwd())
.addDefaultAppResources(path.join(__dirname, "default-app"))
.addDefaultEdgioScripts();
}

module.exports = init;
`.trim()
);

// Write edgio/prod.js
await writeFile(
resolve(nitro.options.output.dir, "edgio/prod.js"),
`
module.exports = async function entry (port) {
process.env.PORT = process.env.NITRO_PORT = port.toString()
console.log('Starting Edgio server on port', port)
await import('../server/index.mjs')
console.log('Edgio server started')
const path = require("path");
const fs = require("fs");

async function prod(port) {
const appFilePath = path.resolve("server", "index.mjs");
if (fs.existsSync(appFilePath)) {
process.env.NITRO_PORT = port.toString();
return await import(
/* webpackIgnore: true */
appFilePath
);
}
}
`.trim()

module.exports = prod;
`.trim()
);

// Write and prepare package.json for deployment
Expand All @@ -86,8 +167,8 @@ module.exports = async function entry (port) {
preview: "npm i && edgio build && edgio run --production",
},
devDependencies: {
"@edgio/cli": "^6",
"@edgio/core": "^6",
"@edgio/cli": "^7",
"@edgio/core": "^7",
},
},
null,
Expand Down