Skip to content

Commit ca440d2

Browse files
committed
Improve attribute replacement and make it work even if app devs have custom preprocessing config.
Auto-reloading doesn't work though, so that needs to be fixed, along with regex specificity.
1 parent ee56dc7 commit ca440d2

File tree

5 files changed

+23
-67
lines changed

5 files changed

+23
-67
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ function process_config(config, { cwd = process.cwd() } = {}) {
6666

6767
const replaceSveltkitAttributes = preprocess({
6868
// 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']]
69+
// TODO: Ensure the replacement happens for prerendered HTML too
7070
replace: [
71-
[/sveltekit:attribute/g, 'data-sveltekit-attribute'],
72-
[/sveltekit\:prefetch/g, 'data-sveltekit-prefetch']
71+
[/sveltekit\:prefetch/g, 'data-sveltekit-prefetch'],
72+
[/sveltekit\:reload/g, 'data-sveltekit-reload'],
73+
[/sveltekit\:noscroll/g, 'data-sveltekit-noscroll']
7374
]
7475
});
7576

7677
// TODO: Update types of `config.preprocess` to get type safety here. Use correct typing and validation from vite-plugin-svelte
7778
// 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.
7979
if (Array.isArray(validated.preprocess)) {
8080
validated.preprocess.push(replaceSveltkitAttributes);
8181
} else {

packages/kit/src/runtime/client/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ export function create_client({ target, base, trailing_slash }) {
11221122
if (
11231123
a.hasAttribute('download') ||
11241124
rel.includes('external') ||
1125-
a.hasAttribute('sveltekit:reload')
1125+
a.hasAttribute('data-sveltekit-reload')
11261126
) {
11271127
return;
11281128
}
@@ -1149,7 +1149,7 @@ export function create_client({ target, base, trailing_slash }) {
11491149

11501150
navigate({
11511151
url,
1152-
scroll: a.hasAttribute('sveltekit:noscroll') ? scroll_state() : null,
1152+
scroll: a.hasAttribute('data-sveltekit-noscroll') ? scroll_state() : null,
11531153
keepfocus: false,
11541154
redirect_chain: [],
11551155
details: {

packages/kit/src/vite/index.js

+4-51
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,14 @@ const enforced_config = {
5858
root: true
5959
};
6060

61+
// TODO: Figure out how to reload the svelte.config.js when changes happen.
62+
const svelte_config = await load_config();
63+
6164
/**
6265
* @return {import('vite').Plugin[]}
6366
*/
6467
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-
75-
return [...svelte(), kit()];
68+
return [...svelte({ configFile: false, ...svelte_config }), kit()];
7669
}
7770

7871
/**
@@ -337,46 +330,6 @@ function kit() {
337330
}
338331
},
339332

340-
// IDEA: Maybe the attribute replacement could be done when rendering the chunks to avoid looping an extra time (as is the case with generateBundle).
341-
// /** @type {import('rollup').RenderChunkHook} */
342-
// renderChunk(code, chunk) {
343-
// for (const [attribute, replacement] of [['sveltekit:prefetch', 'data-sveltekit-prefetch']]) {
344-
// if (code.includes(attribute)) {
345-
// console.log(`[${chunk.name}]: Replacing "${attribute}" with "${replacement}"`);
346-
347-
// return code.replace(attribute, replacement);
348-
// }
349-
// }
350-
351-
// return code;
352-
// },
353-
354-
// TODO: Ensure the replacement only happens for attributes
355-
// TODO: Ensure the replacement happens for prerendered HTML too
356-
// This might be read from the svelte source file which means we need to replace it when server side rendering the HTML response too.
357-
/**
358-
* @param {import('rollup').OutputOptions} _options
359-
* @param {{ [fileName: string]: any }} bundle
360-
*/
361-
generateBundle(_options, bundle) {
362-
for (const entry of Object.values(bundle)) {
363-
if (entry.type !== 'chunk') continue;
364-
365-
for (const [attribute, replacement] of [
366-
['sveltekit:prefetch', 'data-sveltekit-prefetch']
367-
]) {
368-
if (entry.code.includes(attribute)) {
369-
console.log(`[${entry.fileName}]: Replacing "${attribute}" with "${replacement}"`);
370-
371-
bundle[entry.fileName] = {
372-
...entry,
373-
code: entry.code.replaceAll(attribute, replacement)
374-
};
375-
}
376-
}
377-
}
378-
},
379-
380333
/**
381334
* Vite builds a single bundle. We need three bundles: client, server, and service worker.
382335
* The user's package.json scripts will invoke the Vite CLI to execute the client build. We
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<a sveltekit:prefetch href="/path-base/prefetching/prefetched">click me for sveltekit:prefetch</a>
22
<br />
33
<a sveltekit:attribute href="/path-base/prefetching/prefetched">click me for sveltekit:attribute</a>
4+
<br />
5+
<a sveltekit:noscroll href="/path-base/prefetching/prefetched">click me for sveltekit:noscroll</a>
6+
<br />
7+
<a sveltekit:reload href="/path-base/prefetching/prefetched">click me for sveltekit:reload</a>
8+
<br />
9+
<a sveltekit:something href="/path-base/prefetching/prefetched">click me for sveltekit:something</a>

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ const config = {
2727
}
2828
},
2929
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-
// })
30+
// Test how combined user preprocessing works together with internal sveltekit preprocessing.
31+
preprocess({
32+
replace: [
33+
[/sveltekit\:attribute/g, 'data-sveltekit-attribute'],
34+
[/sveltekit\:something/g, 'data-sveltekit-something']
35+
]
36+
})
4037
]
4138
};
4239

0 commit comments

Comments
 (0)