Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

svelte-loader-hot

rixo1.8kMIT0.3.1

Webpack loader for Svelte with HMR support

svelte, sveltejs, webpack-loader, hmr

readme

svelte-loader-hot

This is a copy of official svelte-loader with added HMR support.

HMR is not officially supported by Svelte 3 yet. Progress can be tracked in this issue.

Meanwhile, please report your issues regarding HMR (with Webpack) in this project's issue tracker.

:warning: Experimental :warning:

This HMR implementation relies on Svelte's private & non documented API. This means that it can stop working with any new version of Svelte.

Update 2020-02-24 We're making progress :)

Templates

To quickly bootstrap a new project, or for example purpose, you can use the following templates. They are copies of the official templates, with the bare minimum added to support HMR with this plugin.

Installation

npm install --save-dev svelte-loader-hot

Usage

This plugin can be used as a drop-in replacement for svelte-loader. It aims to remain as close to the official plugin as possible. Please refer to official docs for general usage of the plugin. For HMR specific stuff, see bellow.

Configure inside your webpack.config.js:

module.exports = {
  ...
  module: {
    rules: [
      ...
      {
        test: /\.(html|svelte)$/,
        exclude: /node_modules/,
        use: {
          loader: 'svelte-loader-hot',
          options: {
            // NOTE emitCss: true is currently not supported with HMR
            emitCss: false,
            // Enable HMR
            hotReload: true, // Default: false
            // Extra HMR options
            hotOptions: {
              // Prevent preserving local component state
              noPreserveState: false,

              // If this string appears anywhere in your component's code, then local
              // state won't be preserved, even when noPreserveState is false
              noPreserveStateKey: '@!hmr',

              // Prevent doing a full reload on next HMR update after fatal error
              noReload: false,

              // Try to recover after runtime errors in component init
              optimistic: false

              // --- Advanced ---

              // Prevent adding an HMR accept handler to components with 
              // accessors option to true, or to components with named exports
              // (from <script context="module">). This have the effect of
              // recreating the consumer of those components, instead of the
              // component themselves, on HMR updates. This might be needed to
              // reflect changes to accessors / named exports in the parents,
              // depending on how you use them.
              acceptAccessors: true,
              acceptNamedExports: true,
            }
          }
        }
      }
      ...
    ]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    ...
  ]
}

You also need to add the HotModuleReplacementPlugin. There are multiple ways to achieve this.

If you're using webpack-dev-server, you can just pass it the hot option to add the plugin automatically.

Otherwise, you can add it to your webpack config directly:

const webpack = require('webpack');

module.exports = {
  ...
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    ...
  ]
}

changelog

svelte-loader changelog

2.13.6

  • Check whether the filesystem implements purge before calling it (#81)

2.13.5

  • Fix onwarn with Svelte 3 (#104)

2.13.4

  • Fix webpack 3 bug (#89)

2.13.2

  • Fix compileOptions.filename (#79)

2.13.1

  • v3 fixes (#78)

2.13.0

  • Handle dependencies returned from preprocess (#75)

2.12.0

  • Support Svelte 3

2.11.0

  • Resolve svelte/shared.js before injecting import declaration (#65)

2.10.1

  • Support for older webpack versions (#63)

2.10.0

  • Add options.externalDependencies (#66)

2.9.1

  • Posixify CSS paths on Windows (#64)

2.9.0

  • Fix peer dependency to squelch version mismatch warnings (#57)
  • Fix CSS filenames (#45, #56)
  • Use virtual filesystem to preserve relative paths in CSS (#45

2.8.1

  • Fix HMR on Windows (#52)

2.8.0

  • Deprecate options.markup, options.style and options.script in favour of options.preprocess.* (#41)

2.7.0

  • Use resolved path for hot-api.js, to allowed linked components (#49)

2.6.0

  • Prevent future post-1.60 deprecation warnings (#48)

2.5.1

  • Wrap components with proxies when hot reloading (#44)

2.5.0

  • Emit warnings to webpack (#35)

2.4.0

  • Component-level hot reloading support (#40)

2.3.3

  • Posixify paths to tmp CSS files (#34)

2.3.2

  • Use bare identifier for shared helpers (#33)
  • Use forward-slashes for generated CSS imports (#34)

2.3.1

  • Coerce utimes arguments to dates (#32)

2.3.0

  • Add preprocess support (#31)

2.2.2

  • Deterministic filenames for CSS output (#30)

2.2.1

  • Fix git fail

2.2.0

  • Add emitCss option (#28)

2.1.0

  • Include code frame in error message (#26)

2.0.1

  • Resolve shared helpers path (#19)

2.0.0

  • Pass through all options, overriding format and shared (#17)
  • Auto-generate best configuration for detected webpack version

1.3.1

  • Update loader-utils dependency

1.3.0

  • Pass through options.generate (#10)

1.2.0

  • Add option to specify shared output (svelte#218)

1.1.0

1.0.1

  • Add svelte as peerDependency

1.0.0

  • First release