parcel-plugin-svelte: Class constructor SvelteComponent cannot be invoked without 'new'
š Bug Report
Hi, got some strange error with a basic āHello worldā app.
Class constructor SvelteComponent cannot be invoked without 'new', pointing my app.js
š Configuration (.svelterc, package.json, cli command)
Nothing special.
š» Code Sample
Everythingās in the repo, but the code is quite simple :
import App from "./components/app.svelte";
const root = new App({
target: document.querySelector("#root"),
props: {},
});
export default root;
and the app.svelte is:
<script>
let name = 'world';
</script>
<h1>Hello {name.toUpperCase()}!</h1>
š Your Environment
Svelte version ^3.1.0, Parcel version ^1.12.3, plugin version 3.0.0
Thanks in advance!
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 41
- Comments: 30 (8 by maintainers)
This is not an issue specific to this plugin, this is an issue that is caused due to some design decisions made in Parcel. Specifically(as mentioned above) the decision not to transpile packages in the
node_modulesfolder. You can see the issue in their repository here: https://github.com/parcel-bundler/parcel/issues/1655Iām subscribed to that issue, and to other issues regarding HMR, and will update this plugin when possible. Until then there is nothing we can do⦠EDIT: Updated the link to the issue, accidentally linked a different one.
I have the same issue.
Adding the following to
package.json(as recommended by this comment) get things compiling and displaying on the page:However, HMR throws an error (browser console):
Manually refreshing the page works fine, so we can provide the
--no-hmrCLI option to Parcel and get rid of that issue, but obviously this isnāt ideal as we no longer have HMR.This might be caused by the way babel transforms the svelte output.
For example one of my components outputs this text in the class definition.
Babel and webpack turn it into this gobbledegook where this function
_classCallCheckthrows that error.So, I made a babel config containing some customizations for
@babel/preset-env.npm install core-jsanduseBuiltInsare probably not necessary. Thatās just how my setup goes.It needs a little more investigation to find out if the svelte output or the babel transform are to blame. Based on my work here I donāt think itās an error with
DeMoorJasper/parcel-plugin-svelte.Was this helpful?
Do we need to talk to anyone from the svelte team or do you think itās just babel junk?
@edm00se I thought you might need this for your template.
Related links: https://babeljs.io/docs/en/babel-plugin-transform-classes https://stackoverflow.com/questions/36577683/babel-error-class-constructor-foo-cannot-be-invoked-without-new
If you donāt want to use the browserslist solution, I wrote a Parcel plugin that forces a Babel transform on certain JS assets:
ForceBabel.jsbuild.jsIf you want to use the Parcel CLI instead of the Node API, you can put
ForceBabel.jsin a local directory with apackage.json, call itparcel-plugin-force-babeland runnpm i file:./parcel-plugin-force-babel.Well⦠after some comments on the issue I opened, it seems that
browserslistshould only be used on applications and not library.So I spent some more hours of debugging and it seems that the problem is coming from Parcel itself: when an external JS asset is transformed to AST, the minimal amount of babel plugins is computed. But when external module contains no information (browserslist, babelrc, etc.), the target env serves as reference (https://github.com/parcel-bundler/parcel/blob/master/packages/core/parcel-bundler/src/transforms/babel/env.js#L21), which leads to no transpilation at all.
Guys, this is a marathon⦠š
This problem seems to be related to parcel-bundler/parcel#1037 and the source from
node_modulespackages not being transpiled. Svelte v3 (tested with 3.3.0) contains some ES6 syntax (mostly class declarations). If I understand correctly all the explanations (which I doubt), the correction has to be made on svelte itself š¢.Or as @Mouvedia mentioned, use browserslist in order to get only ES6 code and not mix it with transpiled code.
I using this temprorary workaround (thx @icopp for comment) for solve the bug. It just trigger a transpilation
sveltemodule via Babel (I hope it will be fixed in next releases of Parcel)package.json:.browserslistrc.packages:Maybe this has been mentioned before, but babel and typescript both have options to allow transpiling of certain node_modules. If parcel would respect what the app has defined in their babel or typescript config files, then apps would have a better workaround.
example within a tsconfig.json file:
I really wish there was an option to transpile node_modules. Can Parcel read read node_modules only once and utilize the cache to check if anything has changed? I donāt like the idea of assuming all node_modules are already āstandard jsā.
@blairn you can use the workaround by @orlov-vo
Sent with GitHawk
@madbrain there has actually been various discussions around libraries needing to explicitly mention their target env as otherwise bundlers have no way of knowing which entrypoint to pick/env to start from. There is unfortunately no standard to this as package.json is not really standardised at all.
There have been some proposals and parcel just tries to figure it out based on all semi-standardised fields like browserslist. Not sure why they donāt wanna mention it. But they should.