Skip to content

Commit

Permalink
add new normalizePathForInlineCode utility
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Dec 27, 2024
1 parent 12a1f75 commit acd0370
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-mirrors-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

TODO: add a proper changeset
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from "node:path";

import type { BuildOptions } from "@opennextjs/aws/build/helper.js";

import { normalizePathForInlineCode } from "../../utils/normalize-path-for-inline-code";

/**
* Sets up the OpenNext cache handler in a Next.js build.
*
Expand All @@ -19,12 +21,12 @@ export async function patchCache(code: string, openNextOptions: BuildOptions): P
// TODO: switch to cache.mjs
const outputPath = path.join(outputDir, "server-functions", "default");
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
const cacheFile = path.join(outputPath, packagePath, "cache.cjs");
const cacheFilePath = path.join(outputPath, packagePath, "cache.cjs");

return code.replace(
"const { cacheHandler } = this.nextConfig;",
`const cacheHandler = null;
CacheHandler = require('${cacheFile}').default;
CacheHandler = require('${normalizePathForInlineCode(cacheFilePath)}').default;
`
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from "vitest";

import { normalizePathForInlineCode } from "./normalize-path-for-inline-code";

describe("normalizePathForInlineCode", () => {
it("should extract production env vars", () => {
const result = normalizePathForInlineCode("TODO");
expect(result).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { normalizePath } from "./normalize-path";

/**
* TODO: add proper comment
*
* @param path
*/
export function normalizePathForInlineCode(path: string): string {
// let's normalize the path for good measure
const normalizedPath = normalizePath(path);

// we need to escape
const doublyEscaped = normalizedPath.replaceAll("\\", "\\\\");

return doublyEscaped;
}

0 comments on commit acd0370

Please sign in to comment.