-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[fix] resolve $lib alias when packaging #2453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Fixes the 90% case of #1950
🦋 Changeset detectedLatest commit: b99df98 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit concerned about using string replacement for resolving module aliases, I know bundlers like rollup or esbuild handle these stuff differently, there must be a reason a separate plugin for alias and replace exists in rollup, or separate options in esbuild. One of the case I can think of is when a user have a code block with an import path that uses $lib
, it will match the pattern and replace the path when it's not supposed to do so.
With that being said, I agree this should be sufficient for now while we try to figure out more ways to tackle this, and possibly in an asynchronous way as the "pause" is getting longer as the lib folder gets bigger. I know @dominikg has a prototype with using esbuild, but I think it's only for transpiling TS files and not Svelte.
|
||
const full_path = path.join(config.kit.files.lib, file); | ||
const full_import_path = path.join(config.kit.files.lib, import_path.slice('$lib/'.length)); | ||
let resolved = path.relative(path.dirname(full_path), full_import_path).replace(/\\/g, '/'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when full_import_path
doesn't actually exists, for whatever reasons it may be (most likely a bad copy-paste or refactor)?
path.relative
won't throw as long as it's a string, should we throw an error, leave it with the original alias in place, or use the non-existent resolved path (currently the case)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's on the user then, the also things like svelte-check
should be able find out. We don't check other file paths for correctness, so in my mind it would be inconsistent to check the aliases.
const full_path = path.join(config.kit.files.lib, file); | ||
const full_import_path = path.join(config.kit.files.lib, import_path.slice('$lib/'.length)); | ||
let resolved = path.relative(path.dirname(full_path), full_import_path).replace(/\\/g, '/'); | ||
resolved = resolved.startsWith('.') ? resolved : './' + resolved; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe inline this ternary below to keep resolved
as const
?
Fully agree that this is a hacky solution and I'd very much would like to leverage vite instead. There's a library mode but it works too different from what we want/need, as it bundles everything and will transform Svelte to JS, which we actively want to avoid. |
not entirely sure if it's helpful in this case but vite-plugin-svelte has 2 different features that may be combined to solve it.
So in theory you could build a vite plugin that searches for the other vite plugin it wants to run during svelte-preprocess, save that as local state in configResolved hook and then offer api.sveltePreprocess which uses the other vite-plugin and is picked up by vite-plugin-svelte. I agree that a simple string replace of aliases is too dangerous. For the time being it may be best to recommend not using aliases for libraries build with sveltekit package command |
I think we'd probably want to handle kit/packages/kit/src/core/build/index.js Line 172 in 6bd21c7
It probably wouldn't be too hard to refactor out the code to set the config defaults then look at the actual value of I agree this is a bit dangerous, but it also seems better than not supporting it, so I'm a bit torn I wonder if we need to do something as advanced as what @dominikg is suggesting. Could we just have a svelte-preprocessor that does a replacement using the AST? I don't think it would necessarily need to invoke Vite, which would make things quite a bit easier |
If we are able to use Vite to preprocess the components, we could take advantage of useVitePreprocess which removes the need for |
Ohhh. I did not understand from Domenik's comment or the API names that these methods were not calling |
I'm a little confused by the different solutions outlined in this PR. What we need is something that resolves any alias for
|
I think it's fine to merge the PR as is with the current approach, we could look into other long-term solutions so we don't block others who need this feature. A documentation update noting the caveat would be nice too. Unless we want to ship a more robust experience to the end-users. |
Fixes the 90% case of #1950
Shortcomings of this solution:
These shortcomings should be adressed later
Before submitting the PR, please make sure you do the following
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0