template icon indicating copy to clipboard operation
template copied to clipboard

Development builds should warn developer of compile errors in the browser

Open rlaferla opened this issue 6 years ago • 5 comments

A common problem during Svelte development is when you reload your app in the browser and don't see your changes. After a few minutes, you realize that it's because your code failed to compile.

For dev builds only, Svelte bundle.js should include another Javascript file. e.g. error.js When you compile your app, the compiler will write a function to that file:

// SUCCESSFUL COMPILE
function compileErrors() {
   return "";
}

or

// FAILED TO COMPILE
function compileErrors() {
   return "(!) Plugin svelte: A11y: <img> element should have an alt attribute
src/HUD.svelte
67:   <div id="hud-overlay">
68:     <div class="hud">
69:       <img src="/image.png" />
          ^
70:       <p>Added</p>
71:     </div>
";
}

When your Svelte bundle.js is loaded, it loads error.js (only during development) and calls compileErrors(). If it returns "", the app runs normally. Otherwise, it displays the compile errors in the browser:

image

It's a small change that will boost our productivity. Please consider it.

rlaferla avatar Oct 20 '19 17:10 rlaferla

After a few minutes, you realize that it's because your code failed to compile.

I am always doing this! I now try to position the terminal window in a location behind everything else but with enough sticking out to see my next incoming failures:-

buildFail

MintyMods avatar Oct 20 '19 18:10 MintyMods

You can have it with webpack-dev-server (used by official Svelte template for Webpack) by adding devServer.overlay: true to your webpack.config.js (example here).

This can also be done with HMR over Nollup, but using a fork of Nollup for now.

Finally, this is also supported in the very experimental HMR plugin for Rollup.

rixo avatar Oct 21 '19 09:10 rixo

I'd like to see this added as a default config for Svelte. It's these little everyday things that make frameworks a joy to work with. Please consider adding this, Svelte team...

rlaferla avatar Oct 21 '19 15:10 rlaferla

Isn't this a job for the Svelte compiler rather than this template?

antony avatar Jul 30 '20 10:07 antony

@antony it's about displaying compile errors in the browser. It's a feature of the dev server (that needs support from the bundler).

rixo avatar Jul 30 '20 11:07 rixo