@@ -13,27 +13,32 @@ const lambdaHandlerWithCompatLayer = `
13
13
};
14
14
` ;
15
15
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
+ }
22
28
} else {
23
- return p . replace ( path . normalize ( "../" ) , "" ) ;
29
+ relativePathToCompatLayer = "./" ;
24
30
}
31
+
32
+ relativePathToCompatLayer += "compatLayer" ;
33
+ return relativePathToCompatLayer ;
25
34
} ;
26
35
27
36
module . exports = jsHandlerPath => {
28
37
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
- ) ;
35
38
36
- relativePathToCompatLayer = fixCompatLayerPath ( relativePathToCompatLayer ) ;
39
+ const relativePathToCompatLayer = relativePathToCompatLayerFile (
40
+ jsHandlerPath
41
+ ) ;
37
42
38
43
return lambdaHandlerWithCompatLayer
39
44
. replace ( PAGE_BUNDLE_PATH , `./${ basename } .original.js` )
0 commit comments