-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
New component root
property may throw errors
#6584
Comments
It's strange that |
This also occurs when sharing components with webpacks module federation. (In the current version of svelte, older versions work). |
I've been racking my brain for the last 3 hours trying to figure out why my app suddenly started exploding... Eventually managed to find this issue. Is there a simple fix to reverting this bug so I can continue development until it's patched? I'm using esbuild-svelte which is pulling in a broken version I guess, so I can't just define an older version of svelte in my package.json as far as I'm aware? For others trying to Google this issue, the console output for Chrome is:
And for Firefox:
|
Try downgrading svelte version until it works again. Version `3.39.0` worked fine for me.
…On 30. 7. 2021 12:01, nullbio wrote:
I've been racking my brain for the last 3 hours trying to figure out
why my app suddenly started exploding... Is there a simple fix to
reverting this bug so I can continue development until it's patched?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6584 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGCU5FABFDMDYYYU3OTM6PTT2JZ7BANCNFSM5BCL6OBQ>.
|
Thanks. Clearing my package-lock.json, node_modules, and adding |
This breaks on Snowpack's streaming imports where you don't have the Sticking to Until #6646 gets merged CDN users are just blocked. Thank you! |
does this issue solved? the same error occurs to me. |
Why is this code ever getting called with neither a If it's one Svelte component trying to use another pre-compiled pre-bundled Svelte component, there are already other known issues with that (problems with the transitions scheduler, What's the use case here? Would the change in #6646 prevent this immediate crash, but still leave the other less-visible issues with apps being bundled with multiple copies of Svelte's internals? |
Microfrontends are the use case :)
The change in #6646 would prevent this, yes. |
A possibly related post on Reddit: https://www.reddit.com/r/sveltejs/comments/pjo902/svelte_microfrontend_module_federation_bug/?utm_medium=android_app&utm_source=share |
I'm getting same error and I've tried downgrading to My use case is I have a ui component library I'm attempting to prepare for publishing -- I've npm linked to a ui library which I've built out a <script>
import { Button } from 'agnosticui-svelte';
</script>
<main>
<Button mode="primary" isBordered>Testing 1234</Button>
</main>
<style></style> I've used https://github.com/joeattardi/svelte-tabs and built it out a similar way and it doesn't have the issue but it's locked back at 3.7.1:
I tried rolling all the way back to UPDATE: Then, I backed down my package.json for the library and for the example app the was This seems like a blocker for anyone trying to develop a component library. |
Hello, I do have the same issue in my app while trying to test a component which contains an "if" statement and a mock module. I did not have the issue back when I was using Svelte 3.35.0. I tried to upgrade yesterday to 3.43.0, but did not succeed. I had to rollback to 3.39.0. To ease a bit the debug, here's a simplified repo to reproduce the issue: https://github.com/gcruchon/tests/tree/main/svelte-null-parent-component I'm using last version of all libraries (svelte, jest, svelte-jester, babel, testing-library, ...). I used the svelte template and just added the bug (i.e. testing a mocked "Link" within an "if" statement) Feel free to comment if I missed something. |
Fixes a bug in compiled components, probably related to sveltejs/svelte#6584
Re @mskocik I tested your example in a REPL and I don't see any errors. Re streaming imports @pateketrueke I'm not familiar with it, but usually using CDN or pre-bundled code for Svelte components, they should always be instantiated with a Re jest mocks @gcruchon that seems like a bug of how Svelte components are mocked and interacting with the component initialization phase. I'm not familiar with how it works under-the-hood, but probably somewhere multiple svelte instances are created when being bundled (?). I agree with Conduitry here. While the PR that fixes this works, I don't see any reason why the error would happen in the first place. Either |
@bluwy Cannot duplicate in the REPL neither. But it was happening. It was quite hard to replicate back then. But since when I tried |
Thanks for acknowledging this bug. If I’m not mocking svelte component in the right way, how would you recommend to mock a svelte component? Using an I’m not a specialist of the svelte compiler, so I need more info to investigate. Can someone help me understand and improve the maturity of Svelte to embrace such software craftsmanship practices? Do you have all what is necessary to reproduce the bug? (I see a label “need repro”) |
The way you mock it now is correct, but I'm guessing there's a bug when transforming svelte files in jest. Might be an issue in https://github.com/mihar-22/svelte-jester.
I'd say a no since the repros given so far are Svelte + [third-party integration] repros. Maybe it's fair as it only happens in these very specific scenarios, but it makes it hard to nail down what actually is the bug in Svelte. Or the real bug resides in that third-party integration. |
@nullbio I was also getting this error with Esbuild and it seemed to happen when I linked other libraries that also included svelte. Esbuild wasn't de-duping the svelte import, so I was getting multiple copies inside my bundle. I couldn't find a plugin to de-dupe svelte so added this inline one. // part of esbuild config
plugins: [
{
name: 'dedupe-svelte',
setup({ onResolve }) {
const svelte = require.resolve('svelte')
onResolve({ filter: /^svelte/ }, args => {
let path = svelte.replace(/svelte\/[^\.]+.js$/,args.path+"/index.mjs")// .mjs for browser path
return { path }
})
}
},
sveltePlugin({compileOptions: {css: true}})
], Hope that helps. |
It's an amazing rule, and i can't find it in the docs. So i must provide all of the source files to avoid this problem? If it is, |
yes, definitely
not really, because you can still consume compiled components manually, using the it is very useful to have, i.e. a well-crafted component that can operate standalone |
I finally have a solution.. building svelte/internal as a separate module and treating |
@pateketrueke What if someone uses a svelte component written in typescript, but the project is using pure JS? Then one would have to add typescript just because of the component used? Feels like an overhead definetely. |
That's why there exists the Library authors should ideally use this feature to create the package. Authors can use SCSS, TypeScript and other language variants and in the end normal JavaScript and CSS components will be generated that can be consumed by any other Svelte project. Here is an example of a TypesScript demo package: https://github.com/ivanhofer/svienna-meetup-package-demo. If you are using a library that doesn't get published in the right way, you could open an issue and let the authors know that this feature exists. |
Actually, I could resolve this issue with a correct rollup config in my library project. I defined all svelte related stuff as external and excluded from resolve, so it is fetched from within the project that is using my components fro the library. My rollup config looks as follows:
Apparently works fine, since all svelte stuff gets provided by the build system of the application using the component. |
Thanks @atomcat1978, set svelte (and svelte/internal) as a external librairie fix the build issue on client side :) |
Having this same issue:
In this setup, I could instruct Rollup (second project) to use it's own version of Svelte (including for the components inside the library) by using the // rollup.config.js
import { visualizer } from "rollup-plugin-visualizer";
import commonjs from "@rollup/plugin-commonjs";
import esbuild from "rollup-plugin-esbuild";
import json from "@rollup/plugin-json";
import polyfills from "rollup-plugin-polyfill-node";
import resolve from "@rollup/plugin-node-resolve";
import svelte from "rollup-plugin-svelte";
import sveltePreprocess from "svelte-preprocess";
export default {
input: "./src/index.ts",
output: [
{
name: "TPScript",
file: "dist/script.js",
format: "umd",
sourcemap: true,
},
],
plugins: [
svelte({
preprocess: sveltePreprocess({
sourceMap: true,
}),
emitCss: false,
compilerOptions: {
sourcemap: true,
},
}),
json(),
commonjs(),
polyfills(),
resolve({
browser: true,
dedupe: ["svelte"],
}),
esbuild({
minify: process.env.NODE_ENV === "production",
}),
visualizer(),
],
}; Posted the whole thing in case it helps anyone, but the relevant bit is just the |
I am still getting this error, even though I made sure the components were precompiled with the same version of svelte as the one the app uses ( For context, I am trying to develop a desktop application that has a plugin system that can extend the application's functionality, and one part of it is that plugins can export svelte components that can then be used in the application itself. The application imports the compiled svelte files via the |
@kennethnym It's not that much about the svelte version as it is about having the same context of I'm not familiar with |
I suspected that was the issue. Thanks for the pointer, I have managed to fix the issue by making sure the plugin and the application is loading the same instance of svelte. |
@kennethnym How did you manage to do that? I'm having the same use case with svelte components as plugins, and trying to make the plugins load I'm wondering what was your solution. Any hint will be much appreciated 🙏🏻 |
Thanks for this! This is my working RemoteButton component (should add some error handling):
|
@vitmsrk In my case I am using Svelte in a Tauri app, so the way I make it work will probably not be applicable to you. Anyways, this is how I made it work. Hopefully it will be helpful to you. Basically, you need to make sure that both the internal code and the plugin code uses the same import URL to import svelte. I use Vite to bundle the internal code. Since it doesn't support import rewriting OOTB, I wrote this simple Vite plugin to do exactly that: /**
* This vite plugin prevents vite from rewriting import paths,
* because some libraries are imported through the lib:// protocol
* instead of being bundled directly into the application.
*
* https://github.com/vitejs/vite/issues/6393
*/
function libProtocol() {
return {
name: "lib-protocol",
enforce: "pre",
configResolved(resolvedConfig) {
const VALID_ID_PREFIX = "/@id/"
const reg = new RegExp(`${VALID_ID_PREFIX}(lib:\/.+)`, "g")
resolvedConfig.plugins.push({
name: "vite-plugin-ignore-static-import-replace-idprefix",
transform: (code) => reg.test(code)
? code.replace(reg, (m, s1) => s1)
: code
})
},
resolveId(id, importer) {
if (id === "svelte" || id === "svelte/internal") {
return {id: `lib://localhost/svelte_internal.js`, external: true}
}
if (id === "@twind/core") {
return {id: "lib://localhost/twind_core.js", external: true}
}
if (id.startsWith("svelte/")) {
// `svelte/store` gets flattened to `svelte_store.js`
return {id: `lib://localhost/${
id.replace("/", "_")
}.js`, external: true}
}
}
}
}
module.exports = libProtocol The For the plugins, I use esbuild to compile the plugins. When compiling the plugins, I used esbuild-plugin-import-map] to re-write all svelte imports to the same URL that the internal code of the app uses to import svelte: function rewriteSvelteImport() {
importMap.load({
imports: {
"@powermacro/api": "lib:/localhost/powermacro-api.js",
"@powermacro/block-api": "lib:/localhost/powermacro-block-api.js",
"@twind/core": "lib:/localhost/twind_core.js",
svelte: "lib:/localhost/svelte_internal.js",
"svelte/internal": "lib:/localhost/svelte_internal.js",
"svelte/action": "lib:/localhost/svelte_action.js",
"svelte/animate": "lib:/localhost/svelte_animate.js",
"svelte/easing": "lib:/localhost/svelte_easing.js",
"svelte/motion": "lib:/localhost/svelte_motion.js",
"svelte/store": "lib:/localhost/svelte_store.js",
"svelte/transition": "lib:/localhost/svelte_transition.js"
}
}, ["lib:/"])
return importMap.plugin()
} You can safely ignore the single slash Since both the internal code and the plugin code use the same URL to import svelte libraries, the browser will use the same instance of svelte for the import. I am assuming that your app is run in a browser instead of Tauri. I can see a potential solution here, which is to import svelte through a CDN, then use the same technique that I used to rewrite imports for internal and plugin code. Edit: note that the esbuild plugin I linked only supports https protocol and nothing else. I forked the plugin to allow for |
@kennethnym Thanks very much for the detailed answer. It looks like I need to make the plugins know where or how they will be used, what I'm actually try to avoid. I'll try the CDN option with your custom vite plugin. Feels like I'm close to it, but missing some final pieces of the puzzle. |
For us the bug was that we needed to wrap our Sapper instance with |
You may also encounter this error if your index.html is missing |
Closing as outdated - Svelte 5 has a new compiler output/runtime. The likely reason for such an error is still the same though: Make sure you're not using precompiled Svelte components within your app that were compiled with a different version than the one you use. |
Read this comment first
The reason why this occurs is that you are likely trying to use a pre-compiled (to JS) component that was compiled with a different Svelte version than the one you use, which is not supported. See this comment for more info and solutions:
#6584 (comment)
Describe the bug
I recently experienced issues due to new
root
property in several svelte plugins.It seems that this line causes issues: 5cfefeb#diff-da9bae4e28c441de5ba3a074e30775fe69109100b3d921ad8f2592d93cd67b7f
It seems that a null check for
parent_component
variable is missing at that point.Reproduction
This suddenly occured on certain svelte plugins.
Logs
No response
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: