fix: dont require config file
Fixes a couple of issues with config files in websnacks projects. First, config files are no longer required and the dev and build commands will no longer error out if a websnacks.ts/js file doesn't exist. Second, all optional user config params are now actually optional - before some parameters were assumed to exist and would error out if not present (e.g. the "watch" parameter). Finally, e2e tests were added to prevent regressions on these issues and test helpers were extracted to a separate file.
This commit is contained in:
parent
9a37a4d03d
commit
15683cb1d1
5 changed files with 410 additions and 120 deletions
|
|
@ -58,13 +58,31 @@ const noop = () => {};
|
|||
* @return Fully-realized configuration.
|
||||
*/
|
||||
export const loadConfig = async (rootDir: string): Promise<Config> => {
|
||||
const configPath = require.resolve(path.resolve(rootDir, "websnacks"));
|
||||
purgeModuleAndDepsFromCache(configPath);
|
||||
// TODO: validate user config.
|
||||
const userConfig = await import(configPath);
|
||||
let configPath;
|
||||
let userConfig: UserConfig = {};
|
||||
// Attempt to load a websnacks.ts/js file in rootDir.
|
||||
try {
|
||||
configPath = require.resolve(path.resolve(rootDir, "websnacks"));
|
||||
purgeModuleAndDepsFromCache(configPath);
|
||||
// TODO: validate user config.
|
||||
userConfig = await import(configPath);
|
||||
} catch (error) {
|
||||
// Use default config;
|
||||
}
|
||||
const outDir = path.join(rootDir, "public");
|
||||
const pagesDir = path.join(rootDir, "pages");
|
||||
const staticAssetsDir = path.join(rootDir, "static");
|
||||
|
||||
const watch = [pagesDir, staticAssetsDir];
|
||||
if (configPath != null) {
|
||||
watch.push(path.relative(rootDir, configPath));
|
||||
}
|
||||
if (userConfig.watch != null) {
|
||||
for (const userWatch of userConfig.watch) {
|
||||
watch.push(path.relative(rootDir, userWatch));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
paths: {
|
||||
rootDir,
|
||||
|
|
@ -76,11 +94,6 @@ export const loadConfig = async (rootDir: string): Promise<Config> => {
|
|||
afterSiteRender: noop,
|
||||
...userConfig.hooks,
|
||||
},
|
||||
watch: [
|
||||
...userConfig.watch.map((p: string) => path.relative(rootDir, p)),
|
||||
path.relative(rootDir, configPath),
|
||||
pagesDir,
|
||||
staticAssetsDir,
|
||||
],
|
||||
watch,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue