Skip to content

Commit

Permalink
fix: SharedConfigPlugin uses closest shared config
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
DavieReid committed Aug 3, 2023
1 parent 70d123b commit 6299c07
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions packages/plugins/src/SharedConfigPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -35,8 +45,14 @@ const SharedConfigPlugin: PluginType<SharedConfigPluginPage, SharedConfigPluginO
}
);

const sharedConfigFiles = [];
const pagesWithoutConfig: string[] = [];

for (const pagePath of pagePaths) {
const sharedConfigFile = path.posix.join(path.dirname(String(pagePath)), options.filename);
const sharedConfigFile = path.posix.join(
path.posix.dirname(String(pagePath)),
options.filename
);

const page = await serialiser.deserialise(
String(pagePath),
Expand All @@ -45,11 +61,36 @@ const SharedConfigPlugin: PluginType<SharedConfigPluginPage, SharedConfigPluginO
if (page.sharedConfig) {
config.setRef(sharedConfigFile, ['config', '$ref'], `${String(pagePath)}#/sharedConfig`);
await mutableFilesystem.promises.writeFile(sharedConfigFile, '{}');
sharedConfigFiles.push(sharedConfigFile);
} else {
const baseDir = path.posix.resolve(path.dirname(String(pagePath)), '../');
pagesWithoutConfig.push(page.fullPath);
}
}

let closestSharedConfigIndex = 0;
for (const pagePath of pagesWithoutConfig) {
for (let i = 0; i < sharedConfigFiles.length; i++) {
const sharedConfigBaseDir = path.posix.resolve(
path.posix.dirname(sharedConfigFiles[i]),
'../'
);
const pageBaseDir = path.posix.resolve(path.posix.dirname(String(pagePath)), '../');

config.setAliases(path.join(baseDir, options.filename), [sharedConfigFile]);
if (isWithin(sharedConfigBaseDir, pageBaseDir)) {
closestSharedConfigIndex = i;
}
}

const sharedConfigFile = path.posix.join(
path.posix.dirname(String(pagePath)),
options.filename
);

const closestSharedConfig = path.posix.resolve(
path.dirname(String(pagePath)),
sharedConfigFiles[closestSharedConfigIndex]
);
config.setAliases(closestSharedConfig, [sharedConfigFile]);
}
}
};
Expand Down

1 comment on commit 6299c07

@vercel
Copy link

@vercel vercel bot commented on 6299c07 Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mosaic – ./

mosaic-mosaic-dev-team.vercel.app
mosaic-git-main-mosaic-dev-team.vercel.app

Please sign in to comment.