Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 60cdf9d

Browse files
fix: simplify how relative path to compatLayer is calculated
1 parent 9073ce1 commit 60cdf9d

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/getCompatLayerCode.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ const lambdaHandlerWithCompatLayer = `
1313
};
1414
`;
1515

16-
const fixCompatLayerPath = p => {
17-
// path.relative doesn't check if is a file or dir
18-
// this makes sure the path is actually correct
19-
// https://stackoverflow.com/questions/31023972/node-path-relative-returns-incorrect-path
20-
if (p.startsWith("../compatLayer")) {
21-
return p.replace(path.normalize("../"), path.normalize("./"));
16+
const relativePathToCompatLayerFile = jsHandlerPath => {
17+
const pathSegments = jsHandlerPath.split(path.sep);
18+
let relativePathToCompatLayer = "";
19+
20+
if (pathSegments.length > 2) {
21+
// this is a nested page in the build directory. e.g. sls-next-build/categories/uno/dos.js
22+
// compatLayer is in sls-next-build/compatLayer.js
23+
const stepsUp = pathSegments.length - 2;
24+
25+
for (let index = 0; index < stepsUp; index++) {
26+
relativePathToCompatLayer += "../";
27+
}
2228
} else {
23-
return p.replace(path.normalize("../"), "");
29+
relativePathToCompatLayer = "./";
2430
}
31+
32+
relativePathToCompatLayer += "compatLayer";
33+
return relativePathToCompatLayer;
2534
};
2635

2736
module.exports = jsHandlerPath => {
2837
const basename = path.basename(jsHandlerPath, ".js");
29-
const [rootDir] = jsHandlerPath.split(path.sep);
30-
const pathToCompatLayer = path.resolve(path.join(rootDir, "compatLayer"));
31-
let relativePathToCompatLayer = path.relative(
32-
jsHandlerPath,
33-
pathToCompatLayer
34-
);
3538

36-
relativePathToCompatLayer = fixCompatLayerPath(relativePathToCompatLayer);
39+
const relativePathToCompatLayer = relativePathToCompatLayerFile(
40+
jsHandlerPath
41+
);
3742

3843
return lambdaHandlerWithCompatLayer
3944
.replace(PAGE_BUNDLE_PATH, `./${basename}.original.js`)

0 commit comments

Comments
 (0)