Skip to content

Commit ee56dc7

Browse files
committed
WIP: Use svelte-preprocess to replace values. Significant progress!
PoC works great if run from the user's svelte.config.js, but we need to extend the user config and still make it work. Currently it only works when run with the default svelte config loading.
1 parent 545286d commit ee56dc7

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

packages/kit/src/core/config/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import path from 'path';
33
import * as url from 'url';
44
import options from './options.js';
5+
import preprocess from 'svelte-preprocess';
56

67
/**
78
* Loads the template (src/app.html by default) and validates that it has the
@@ -63,6 +64,24 @@ function process_config(config, { cwd = process.cwd() } = {}) {
6364

6465
validated.kit.outDir = path.resolve(cwd, validated.kit.outDir);
6566

67+
const replaceSveltkitAttributes = preprocess({
68+
// TODO: Update regex to only match attributes and not all string values, to prevent it from replacing text content.
69+
// replace: [[/sveltekit\:prefetch/g, 'data-sveltekit-prefetch']]
70+
replace: [
71+
[/sveltekit:attribute/g, 'data-sveltekit-attribute'],
72+
[/sveltekit\:prefetch/g, 'data-sveltekit-prefetch']
73+
]
74+
});
75+
76+
// TODO: Update types of `config.preprocess` to get type safety here. Use correct typing and validation from vite-plugin-svelte
77+
// TODO: This needs to support all valid configs for preprocess, possibly including objects and not just arrays.
78+
// TODO: Ensure the svelte.preprocess API can run multiple instances of `svelte-preprocess` if the user has their own config too.
79+
if (Array.isArray(validated.preprocess)) {
80+
validated.preprocess.push(replaceSveltkitAttributes);
81+
} else {
82+
validated.preprocess = [replaceSveltkitAttributes];
83+
}
84+
6685
for (const key in validated.kit.files) {
6786
// @ts-expect-error this is typescript at its stupidest
6887
validated.kit.files[key] = path.resolve(cwd, validated.kit.files[key]);

packages/kit/src/vite/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ const enforced_config = {
6262
* @return {import('vite').Plugin[]}
6363
*/
6464
export function sveltekit() {
65+
// TODO: find out if this is the right place to ensure we add preprocessing for replacing sveltekit specific attributes
66+
// NOTE: OR maybe it is autimatically passed in, since it uses the svelte.config.js file where we might have additional preprocessing
67+
// TODO: Ensure preprocessing works with multiple calls to `svelte-preprocess`.
68+
// Seems like it should be find though, as it likely is using an internal closure to keep state, and returning the callback to actually execute the transform.
69+
70+
// IDEA: We could use svelte-preprocess to create the needed config for preprocessing `sveltekit:*`
71+
// TODO: Update regex to only match attributes and not all string values, to prevent it from replacing text content.
72+
// import preprocess from 'svelte-preprocess'
73+
// preprocess({ replace: [/sveltekit\:prefetch/g, 'data-sveltekit-prefetch'] })
74+
6575
return [...svelte(), kit()];
6676
}
6777

@@ -197,6 +207,9 @@ function kit() {
197207
async config(config, config_env) {
198208
vite_config_env = config_env;
199209
svelte_config = await load_config();
210+
211+
console.log('vite svelte.config.js preprocess', svelte_config.preprocess);
212+
200213
env = get_env(vite_config_env.mode, svelte_config.kit.env.publicPrefix);
201214

202215
// The config is created in build_server for SSR mode and passed inline
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<a sveltekit:prefetch href="/path-base/prefetching/prefetched">click me for sveltekit:prefetch</a>
2+
<br />
3+
<a sveltekit:attribute href="/path-base/prefetching/prefetched">click me for sveltekit:attribute</a>

packages/kit/test/apps/options/svelte.config.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import preprocess from 'svelte-preprocess';
2+
13
/** @type {import('@sveltejs/kit').Config} */
24
const config = {
35
extensions: ['.jesuslivesineveryone', '.whokilledthemuffinman', '.svelte.md', '.svelte'],
@@ -23,7 +25,19 @@ const config = {
2325
base: '/path-base',
2426
assets: 'https://cdn.example.com/stuff'
2527
}
26-
}
28+
},
29+
preprocess: [
30+
// IDEA: We could use svelte-preprocess to create the needed config for preprocessing `sveltekit:*`
31+
// TODO: Update regex to only match attributes and not all string values, to prevent it from replacing text content.
32+
// preprocess({
33+
// // TODO: Find a way to create a combined preprocess() inside of sveltekit.
34+
// // This works perfectly, but we shouldn't have to expose this detail to app developers. There must be a way to combine it.
35+
// replace: [
36+
// [/sveltekit\:attribute/g, 'data-sveltekit-attribute'],
37+
// [/sveltekit\:prefetch/g, 'data-sveltekit-prefetch']
38+
// ]
39+
// })
40+
]
2741
};
2842

2943
export default config;

0 commit comments

Comments
 (0)