-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1184 from evidence-dev/fix/1182/pluggable-svelte-…
…config feat: enable custom sveltekit configs
- Loading branch information
Showing
5 changed files
with
115 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@evidence-dev/evidence': patch | ||
--- | ||
|
||
User projects can now extend svelte.config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import evidencePreprocess from '@evidence-dev/preprocess'; | ||
import preprocess from 'svelte-preprocess'; | ||
import adapter from '@sveltejs/adapter-static'; | ||
import { evidencePlugins } from '@evidence-dev/plugin-connector'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
/** | ||
* @param {Object} a | ||
* @param {Object} b | ||
* @returns {Object} | ||
*/ | ||
const deepMerge = (a, b) => { | ||
for (const key in b) { | ||
if (typeof b[key] === 'object' && !Array.isArray(b[key])) { | ||
if (!a[key]) a[key] = {}; | ||
deepMerge(a[key], b[key]); | ||
} else { | ||
Object.assign(a, { [key]: b[key] }); | ||
} | ||
} | ||
return a; | ||
}; | ||
|
||
/** @type {import('@sveltejs/kit').Config} */ | ||
const config = { | ||
extensions: ['.svelte', '.md'], | ||
preprocess: [ | ||
...evidencePreprocess(true), | ||
evidencePlugins(), | ||
preprocess({ | ||
postcss: true | ||
}) | ||
], | ||
kit: { | ||
adapter: adapter({ | ||
strict: false | ||
}), | ||
files: { | ||
routes: 'src/pages', | ||
lib: 'src/components' | ||
} | ||
} | ||
}; | ||
|
||
async function loadUserConfiguration() { | ||
if (!process.cwd().includes('.evidence')) return; | ||
const rootDir = process.cwd().split('.evidence')[0]; | ||
const rootDirContents = fs.readdirSync(rootDir); | ||
|
||
if (!rootDirContents.includes('svelte.config.js')) return; | ||
|
||
const configFileLocation = path.join(rootDir, 'svelte.config.js'); | ||
const configURL = new URL(`file:///${configFileLocation}`).href; | ||
/** @type {import("@sveltejs/kit").Config} */ | ||
const userConfig = await import(configURL).then((r) => r.default); | ||
|
||
if ('preprocess' in userConfig) { | ||
if ('preprocess' in config) { | ||
config.preprocess.push(...userConfig.preprocess); | ||
} else { | ||
// This case shouldn't ever be reached. | ||
config.preprocess = userConfig.preprocess; | ||
} | ||
delete userConfig.preprocess; | ||
} | ||
if ('extensions' in userConfig) { | ||
console.warn('Configuring extensions is disabled for Evidence projects.'); | ||
delete userConfig.extensions; | ||
} | ||
if ('kit' in userConfig && 'files' in userConfig.kit) { | ||
console.warn('Configuring file locations is disabled for Evidence projects.'); | ||
delete userConfig.kit.files; | ||
} | ||
|
||
deepMerge(config, userConfig); | ||
console.log('Custom svelte.config.js applied'); | ||
} | ||
|
||
await loadUserConfiguration(); | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** @type {import("@sveltejs/kit").Config} */ | ||
export default { | ||
preprocess: [], | ||
extensions: [], | ||
kit: { | ||
files: {} | ||
}, | ||
vite: { | ||
server: { | ||
watch: { | ||
usePolling: true | ||
} | ||
} | ||
} | ||
}; |