From 6299c077c421f3afb9bb9ebb7907c5e66c3e72b4 Mon Sep 17 00:00:00 2001 From: Davie Date: Thu, 3 Aug 2023 13:01:11 +0100 Subject: [PATCH] fix: `SharedConfigPlugin` uses closest shared config ### Before If a page did not specify any shared config metadata then it would simply create a ref to the parent directory shared-config.json file. However, this assumes that the parent directory *has* a shared config file. ### After All generated shared config files are identified and then the closest 1 to the page is used. --- packages/plugins/src/SharedConfigPlugin.ts | 47 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/plugins/src/SharedConfigPlugin.ts b/packages/plugins/src/SharedConfigPlugin.ts index 0f3e68d7..ccc75e25 100644 --- a/packages/plugins/src/SharedConfigPlugin.ts +++ b/packages/plugins/src/SharedConfigPlugin.ts @@ -9,6 +9,16 @@ function createFileGlob(url, pageExtensions) { return `${url}{${pageExtensions.join(',')}}`; } +/** + * https://stackoverflow.com/a/50549047 + * compute inner relative to outer + * If it's not contained, the first component of the resulting path will be .., so that's what we check for + */ +function isWithin(outer, inner) { + const rel = path.posix.relative(outer, inner); + return !rel.startsWith('../') && rel !== '..'; +} + interface SharedConfigPluginPage extends Page { sharedConfig?: string; } @@ -35,8 +45,14 @@ const SharedConfigPlugin: PluginType