Skip to content

Commit

Permalink
Support 'deno' runtime for server functions
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed May 22, 2024
1 parent 65f7a60 commit 6d24162
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,16 @@ async function createCacheAssets(monorepoRoot: string) {
/* Server Helper Functions */
/***************************/

function compileCache() {
const outfile = path.join(options.outputDir, ".build", "cache.cjs");
export function compileCache(format: "cjs" | "esm" = "cjs") {
const ext = format === "cjs" ? "cjs" : "mjs";
const outfile = path.join(options.outputDir, ".build", `cache.${ext}`);
esbuildSync(
{
external: ["next", "styled-jsx", "react", "@aws-sdk/*"],
entryPoints: [path.join(__dirname, "adapters", "cache.js")],
outfile,
target: ["node18"],
format: "cjs",
format,
banner: {
js: [
`globalThis.disableIncrementalCache = ${
Expand Down
32 changes: 30 additions & 2 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "types/open-next";
import url from "url";

import { compileCache } from "../build.js";
import logger from "../logger.js";
import { minifyAll } from "../minimize-js.js";
import { openNextReplacementPlugin } from "../plugins/replacement.js";
Expand Down Expand Up @@ -37,6 +38,13 @@ export async function createServerBundle(
const defaultFn = config.default;
const functions = Object.entries(config.functions ?? {});

if (
defaultFn.runtime === "deno" ||
functions.some(([, fn]) => fn.runtime === "deno")
) {
compileCache("esm");
}

const promises = functions.map(async ([name, fnOptions]) => {
const routes = fnOptions.routes;
routes.forEach((route) => foundRoutes.add(route));
Expand Down Expand Up @@ -134,11 +142,16 @@ async function generateBundle(
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });

const ext = fnOptions.runtime === "deno" ? "mjs" : "cjs";
fs.copyFileSync(
path.join(outputDir, ".build", "cache.cjs"),
path.join(outputDir, ".build", `cache.${ext}`),
path.join(outputPath, packagePath, "cache.cjs"),
);

if (fnOptions.runtime === "deno") {
addDenoJson(outputPath, packagePath);
}

// Bundle next server if necessary
const isBundled = fnOptions.experimentalBundledNextServer ?? false;
if (isBundled) {
Expand Down Expand Up @@ -227,14 +240,18 @@ async function generateBundle(
.join(",")}] for Next version: ${options.nextVersion}`,
);
}

const outfileExt = fnOptions.runtime === "deno" ? "ts" : "mjs";
await esbuildAsync(
{
entryPoints: [path.join(__dirname, "../adapters", "server-adapter.js")],
external: ["next", "./middleware.mjs", "./next-server.runtime.prod.js"],
outfile: path.join(outputPath, packagePath, "index.mjs"),
outfile: path.join(outputPath, packagePath, `index.${outfileExt}`),
banner: {
js: [
`globalThis.monorepoPackagePath = "${packagePath}";`,
"import process from 'node:process';",
"import { Buffer } from 'node:buffer';",
"import { createRequire as topLevelCreateRequire } from 'module';",
"const require = topLevelCreateRequire(import.meta.url);",
"import bannerUrl from 'url';",
Expand Down Expand Up @@ -280,6 +297,17 @@ function shouldGenerateDockerfile(options: FunctionOptions) {
return options.override?.generateDockerfile ?? false;
}

function addDenoJson(outputPath: string, packagePath: string) {
const config = {
// Enable "bring your own node_modules" mode
unstable: ["byonm", "fs"],
};
fs.writeFileSync(
path.join(outputPath, packagePath, "deno.json"),
JSON.stringify(config, null, 2),
);
}

//TODO: check if this PR is still necessary https://github.com/sst/open-next/pull/341
function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
// Note: in the monorepo case, the handler file is output to
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/types/open-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export interface FunctionOptions extends DefaultFunctionOptions {
* Runtime used
* @default "node"
*/
runtime?: "node" | "edge";
runtime?: "node" | "edge" | "deno";
/**
* @default "regional"
*/
Expand Down

0 comments on commit 6d24162

Please sign in to comment.