Why are AST transforms required for the sdk-js in sveltekit? #837
-
@benjaminpreiss @ivanhofer Why are AST transforms required in the sdk-js? I found #615 that introduced AST transforms. However, I can't find the reasoning. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
Hey, thanks for the Q. We need to transform some source code in the vite plugin. One might think this could just be done with regexes, but it's much more reliable to parse the JS code as an AST to target specific elements when transforming. Does that answer your question? |
Beta Was this translation helpful? Give feedback.
-
Dear all, I'd like to give my thoughts to this discussion, by going through all the necessary steps outlined in the Refactoring master plan and sharing my evaluation regarding feasibility. In general, we transform:
Regular JS files / regular svelte filesIn these files, we run
import {i} from "@inlang/sdk-js"
console.log(i('hello'))
[1, 2, 3, 4].forEach((v, i) => {
console.log(i)
}) Here, we would need to write an AST transform that understands that the second For regular svelte files, the functionality is similar: Special JS filesHere, we need to wrap some function declarations or create them from scratch and rewrite some parameters (which is the tricky part).
The complexity of changing the parameters of a certain function lies in identifying where to rewrite these parameters: function fn2(someparameters) {
console.log(i('tricky'))
}
const fn1 = fn2
export const load = fn1 should become function fn2(someparameters, {i}) {
console.log(i('tricky'))
}
const fn1 = fn2
export const load = inlangWrapper(fn1) Here, we need to rewrite the function parameters of import {fn3} from 'somewhere'
function fn2(someparameters) {
console.log(i('tricky'))
}
const fn1 = fn3(fn2)
export const load = fn1 Here we can not trace back what That said, I am confident that we can cover most of the cases quickly / have them covered. Special svelte filesHere we just need to replace the SDK imports with some lines (all at the top of the script tag), wrap some svelte markup
... and then run the regular svelte file transforms. Summary
"import { initHandleWrapper } from "@inlang/sdk-js/adapter-sveltekit/server";
import { sequence } from "@sveltejs/kit/hooks";
import { i } from "@inlang/sdk-js";
const seq1 = async ({ event, resolve }) => {
console.log(getRuntimeFromLocals(event.locals).i("welcome"));
return resolve(event);
};
export const handle = initHandleWrapper({
inlangConfigModule: import("../inlang.config.js"),
getLanguage: () => undefined
}).wrap(sequence(seq1));" How can we improve this so that we don't need to change function parameters?
Final wordsRegardless of how we decide here, I am very confident that we can get the AST transforms to be very stable - we are actually pretty close to covering most cases. |
Beta Was this translation helpful? Give feedback.
Then you would need to use it like this:
And it would probably introduce more problems than it solves, because people will always use the function in a wrong way ^^
This alone should be reason enough to not do it.
But the real reason is:
the whole easy to use solution relies on the AST transformation we make. There are 10+ different ways how
i('welcome')
can be used (different files, different contexts). A user would need to set this up himself and needs to know where he needs to use which import. At that point we wouln't need yet another i18n library and everyone should just usetypesafe-i18n
.