Svelte 4 移行ガイド
この移行ガイドでは、Svelte バージョン 3 から 4 への移行方法の概要を説明します。各変更の詳細については、リンクされた PR を参照してください。移行スクリプトを使用して、これらのいくつかを自動的に移行します: npx svelte-migrate@latest svelte-4
ライブラリの作者である場合は、Svelte 4 のみをサポートするか、Svelte 3 もサポートできるかどうかを検討してください。破壊的変更のほとんどは多くの人に影響を与えないため、これは簡単に可能かもしれません。また、peerDependencies
のバージョン範囲を更新することを忘れないでください。
最小バージョン要件
- Node 16 以降にアップグレードしてください。以前のバージョンはサポートされなくなりました。(#8566)
- SvelteKit を使用している場合は、1.20.4 以降にアップグレードしてください(sveltejs/kit#10172)
- SvelteKit なしで Vite を使用している場合は、
vite-plugin-svelte
2.4.1 以降にアップグレードしてください(#8516) - webpack を使用している場合は、webpack 5 以降と
svelte-loader
3.1.8 以降にアップグレードしてください。以前のバージョンはサポートされなくなりました。(#8515, 198dbcf) - Rollup を使用している場合は、
rollup-plugin-svelte
7.1.5 以降にアップグレードしてください(198dbcf) - TypeScript を使用している場合は、TypeScript 5 以降にアップグレードしてください。低いバージョンでも動作する可能性がありますが、保証はありません。(#8488)
バンドラーのブラウザ条件
バンドラーは、ブラウザのフロントエンドバンドルをビルドするときに、browser
条件を指定する必要があります。SvelteKit と Vite はこれを自動的に処理します。その他を使用している場合は、onMount
などのライフサイクルフックが呼び出されない場合があり、モジュール解決設定を更新する必要があります。
- Rollup の場合、これは
@rollup/plugin-node-resolve
プラグイン内で、そのオプションでbrowser: true
を設定することによって行われます。詳細については、rollup-plugin-svelte
ドキュメントを参照してください - webpack の場合、これは
conditionNames
配列に"browser"
を追加することによって行われます。設定している場合は、alias
設定も更新する必要がある場合があります。詳細については、svelte-loader
ドキュメントを参照してください
(#8516)
CJS 関連出力の削除
Svelte は、コンパイラ出力の CommonJS (CJS) 形式をサポートしなくなり、svelte/register
フックと CJS ランタイムバージョンも削除しました。CJS 出力形式を維持する必要がある場合は、バンドラーを使用して Svelte の ESM 出力をビルド後のステップで CJS に変換することを検討してください。(#8613)
Svelte 関数のより厳密な型
createEventDispatcher
、Action
、ActionReturn
、および onMount
の型がより厳密になりました
createEventDispatcher
は、ペイロードがオプション、必須、または存在しないことを指定できるようになり、呼び出しサイトはそれに応じてチェックされます(#7224)
import { function createEventDispatcher<EventMap extends Record<string, any> = any>(): EventDispatcher<EventMap>
Creates an event dispatcher that can be used to dispatch component events.
Event dispatchers are functions that can take two arguments: name
and detail
.
Component events created with createEventDispatcher
create a
CustomEvent.
These events do not bubble.
The detail
argument corresponds to the CustomEvent.detail
property and can contain any type of data.
The event dispatcher can be typed to narrow the allowed event names and the type of the detail
argument:
const dispatch = createEventDispatcher<{
loaded: never; // does not take a detail argument
change: string; // takes a detail argument of type string, which is required
optional: number | null; // takes an optional detail argument of type number
}>();
createEventDispatcher } from 'svelte';
const const dispatch: EventDispatcher<{
optional: number | null;
required: string;
noArgument: null;
}>
dispatch = createEventDispatcher<{
optional: number | null;
required: string;
noArgument: null;
}>(): EventDispatcher<{
optional: number | null;
required: string;
noArgument: null;
}>
Creates an event dispatcher that can be used to dispatch component events.
Event dispatchers are functions that can take two arguments: name
and detail
.
Component events created with createEventDispatcher
create a
CustomEvent.
These events do not bubble.
The detail
argument corresponds to the CustomEvent.detail
property and can contain any type of data.
The event dispatcher can be typed to narrow the allowed event names and the type of the detail
argument:
const dispatch = createEventDispatcher<{
loaded: never; // does not take a detail argument
change: string; // takes a detail argument of type string, which is required
optional: number | null; // takes an optional detail argument of type number
}>();
createEventDispatcher<{
optional: number | null
optional: number | null;
required: string
required: string;
noArgument: null
noArgument: null;
}>();
// Svelte version 3:
const dispatch: EventDispatcher
<"optional">(type: "optional", parameter?: number | null | undefined, options?: DispatchOptions | undefined) => boolean
dispatch('optional');
const dispatch: EventDispatcher
<"required">(type: "required", parameter: string, options?: DispatchOptions | undefined) => boolean
dispatch('required'); // I can still omit the detail argument
const dispatch: EventDispatcher
<"noArgument">(type: "noArgument", parameter?: null | undefined, options?: DispatchOptions | undefined) => boolean
dispatch('noArgument', 'surprise'); // I can still add a detail argument
// Svelte version 4 using TypeScript strict mode:
const dispatch: EventDispatcher
<"optional">(type: "optional", parameter?: number | null | undefined, options?: DispatchOptions | undefined) => boolean
dispatch('optional');
const dispatch: EventDispatcher
<"required">(type: "required", parameter: string, options?: DispatchOptions | undefined) => boolean
dispatch('required'); // error, missing argument
const dispatch: EventDispatcher
<"noArgument">(type: "noArgument", parameter?: null | undefined, options?: DispatchOptions | undefined) => boolean
dispatch('noArgument', 'surprise'); // error, cannot pass an argument
Action
とActionReturn
のデフォルトのパラメータ型はundefined
になりました。つまり、このアクションがパラメータを受け取ることを指定する場合は、ジェネリックを入力する必要があります。移行スクリプトはこれを自動的に移行します(#7442)
const action: Action = (node, params) => { ... } // this is now an error if you use params in any way
const const action: Action<HTMLElement, string>
action: type Action = /*unresolved*/ any
Action<HTMLElement, string> = (node: any
node, params: any
params) => { ... } // params is of type string
onMount
は、非同期的に関数を返すと型エラーを表示するようになりました。これは、コールバックが破棄時に呼び出されることを期待しているコードのバグである可能性が高いためです。これは、同期的に返された関数のみに対して行われます(#8136)
// Example where this change reveals an actual bug
onMount(
// someCleanup() not called because function handed to onMount is async
async () => {
const something = await foo();
// someCleanup() is called because function handed to onMount is sync
() => {
foo().then(something: any
something => {...});
// ...
return () => someCleanup();
}
);
Svelte を使用したカスタム要素
Svelte を使用したカスタム要素の作成は、見直され、大幅に改善されました。tag
オプションは非推奨となり、新しい customElement
オプションが推奨されます
<svelte:options tag="my-component" />
<svelte:options customElement="my-component" />
この変更は、高度なユースケースでより多くの設定を可能にするために行われました。移行スクリプトはコードを自動的に調整します。プロパティの更新タイミングもわずかに変更されました。(#8457)
SvelteComponentTyped は非推奨です
SvelteComponentTyped
は非推奨です。SvelteComponent
にはすべてのタイピング機能が備わっているためです。SvelteComponentTyped
のすべてのインスタンスを SvelteComponent
に置き換えてください。
import { SvelteComponentTyped } from 'svelte';
import { class SvelteComponent<Props extends Record<string, any> = Record<string, any>, Events extends Record<string, any> = any, Slots extends Record<string, any> = any>
This was the base class for Svelte components in Svelte 4. Svelte 5+ components
are completely different under the hood. For typing, use Component
instead.
To instantiate components, use mount
instead`.
See migration guide for more info.
SvelteComponent } from 'svelte';
export class Foo extends SvelteComponentTyped<{ aProp: string }> {}
export class class Foo
Foo extends class SvelteComponent<Props extends Record<string, any> = Record<string, any>, Events extends Record<string, any> = any, Slots extends Record<string, any> = any>
This was the base class for Svelte components in Svelte 4. Svelte 5+ components
are completely different under the hood. For typing, use Component
instead.
To instantiate components, use mount
instead`.
See migration guide for more info.
SvelteComponent<{ aProp: string
aProp: string }> {}
以前にコンポーネントインスタンスタイプとして SvelteComponent
を使用していた場合、多少不透明な型エラーが表示される場合があります。これは、: typeof SvelteComponent
を : typeof SvelteComponent<any>
に変更することで解決されます。
<script>
import ComponentA from './ComponentA.svelte';
import ComponentB from './ComponentB.svelte';
import { SvelteComponent } from 'svelte';
let component: typeof SvelteComponent<any>;
function choseRandomly() {
component = Math.random() > 0.5 ? ComponentA : ComponentB;
}
</script>
<button on:click={choseRandomly}>random</button>
<svelte:element this={component} />
移行スクリプトは両方とも自動的に行います。(#8512)
トランジションはデフォルトでローカルです
トランジションは、ページナビゲーションに関する混乱を防ぐため、デフォルトでローカルになりました。「ローカル」とは、トランジションがネストされた制御フローブロック (each/if/await/key
) 内にあり、直接の親ブロックではなく、その上のブロックが作成/破棄された場合に再生されないことを意味します。次の例では、slide
イントロアニメーションは success
が false
から true
に変わったときにのみ再生されますが、show
が false
から true
に変わったときには再生され*ません*
{#if show}
...
{#if success}
<p in:slide>Success</p>
{/each}
{/if}
トランジションをグローバルにするには、|global
修飾子を追加します。これにより、*任意*の制御フローブロックが作成/破棄されたときに再生されます。移行スクリプトはこれを自動的に行います。(#6686)
デフォルトのスロットバインディング
デフォルトのスロットバインディングは、名前付きスロットに公開されなくなりました。逆も同様です
<script>
import Nested from './Nested.svelte';
</script>
<Nested let:count>
<p>
count in default slot - is available: {count}
</p>
<p slot="bar">
count in bar slot - is not available: {count}
</p>
</Nested>
これにより、たとえばデフォルトスロットがリストからのもので、名前付きスロットがそうでない場合の動作が未定義であるため、スロットバインディングの一貫性が向上します。(#6049)
プリプロセッサ
プリプロセッサが適用される順序が変更されました。現在、プリプロセッサは順番に実行され、1つのグループ内では、順序はマークアップ、スクリプト、スタイルです。
import { function preprocess(source: string, preprocessor: PreprocessorGroup | PreprocessorGroup[], options?: {
filename?: string;
} | undefined): Promise<Processed>
The preprocess function provides convenient hooks for arbitrarily transforming component source code.
For example, it can be used to convert a <style lang="sass">
block into vanilla CSS.
preprocess } from 'svelte/compiler';
const { const code: string
The new code
code } = await function preprocess(source: string, preprocessor: PreprocessorGroup | PreprocessorGroup[], options?: {
filename?: string;
} | undefined): Promise<Processed>
The preprocess function provides convenient hooks for arbitrarily transforming component source code.
For example, it can be used to convert a <style lang="sass">
block into vanilla CSS.
preprocess(
source,
[
{
PreprocessorGroup.markup?: MarkupPreprocessor | undefined
markup: () => {
var console: Console
The console
module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console
class with methods such as console.log()
, console.error()
and console.warn()
that can be used to write to any Node.js stream.
- A global
console
instance configured to write to process.stdout
and
process.stderr
. The global console
can be used without calling require('console')
.
Warning: The global console object’s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O
for
more information.
Example using the global console
:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console
class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to stdout
with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()
).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format()
for more information.
log('markup-1');
},
PreprocessorGroup.script?: Preprocessor | undefined
script: () => {
var console: Console
The console
module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console
class with methods such as console.log()
, console.error()
and console.warn()
that can be used to write to any Node.js stream.
- A global
console
instance configured to write to process.stdout
and
process.stderr
. The global console
can be used without calling require('console')
.
Warning: The global console object’s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O
for
more information.
Example using the global console
:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console
class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to stdout
with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()
).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format()
for more information.
log('script-1');
},
PreprocessorGroup.style?: Preprocessor | undefined
style: () => {
var console: Console
The console
module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console
class with methods such as console.log()
, console.error()
and console.warn()
that can be used to write to any Node.js stream.
- A global
console
instance configured to write to process.stdout
and
process.stderr
. The global console
can be used without calling require('console')
.
Warning: The global console object’s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O
for
more information.
Example using the global console
:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console
class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to stdout
with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()
).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format()
for more information.
log('style-1');
}
},
{
PreprocessorGroup.markup?: MarkupPreprocessor | undefined
markup: () => {
var console: Console
The console
module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console
class with methods such as console.log()
, console.error()
and console.warn()
that can be used to write to any Node.js stream.
- A global
console
instance configured to write to process.stdout
and
process.stderr
. The global console
can be used without calling require('console')
.
Warning: The global console object’s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O
for
more information.
Example using the global console
:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console
class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to stdout
with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()
).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format()
for more information.
log('markup-2');
},
PreprocessorGroup.script?: Preprocessor | undefined
script: () => {
var console: Console
The console
module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console
class with methods such as console.log()
, console.error()
and console.warn()
that can be used to write to any Node.js stream.
- A global
console
instance configured to write to process.stdout
and
process.stderr
. The global console
can be used without calling require('console')
.
Warning: The global console object’s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O
for
more information.
Example using the global console
:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console
class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to stdout
with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()
).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format()
for more information.
log('script-2');
},
PreprocessorGroup.style?: Preprocessor | undefined
style: () => {
var console: Console
The console
module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console
class with methods such as console.log()
, console.error()
and console.warn()
that can be used to write to any Node.js stream.
- A global
console
instance configured to write to process.stdout
and
process.stderr
. The global console
can be used without calling require('console')
.
Warning: The global console object’s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O
for
more information.
Example using the global console
:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console
class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to stdout
with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()
).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format()
for more information.
log('style-2');
}
}
],
{
filename?: string | undefined
filename: 'App.svelte'
}
);
// Svelte 3 logs:
// markup-1
// markup-2
// script-1
// script-2
// style-1
// style-2
// Svelte 4 logs:
// markup-1
// script-1
// style-1
// markup-2
// script-2
// style-2
これは、たとえば MDsveX
を使用している場合に影響を与える可能性があります。この場合は、スクリプトまたはスタイルのプリプロセッサの前に配置する必要があります。
preprocess: [
vitePreprocess(),
mdsvex(mdsvexConfig)
mdsvex(mdsvexConfig),
vitePreprocess()
]
各プリプロセッサには名前も必要です。(#8618)
新しい eslint パッケージ
eslint-plugin-svelte3
は非推奨です。Svelte 4 でも動作する可能性がありますが、保証はありません。新しいパッケージ eslint-plugin-svelte に切り替えることをお勧めします。移行方法については、この Github の投稿 を参照してください。または、npm create svelte@latest
を使用して新しいプロジェクトを作成し、eslint (および場合によっては TypeScript) オプションを選択してから、関連するファイルを既存のプロジェクトにコピーすることもできます。
その他の破壊的変更
- 支援技術から見えなくなり、インタラクションを防ぐために、
inert
属性がアウトロイング要素に適用されるようになりました。(#8628) - ランタイムは
classList.toggle(name, boolean)
を使用するようになりました。これは非常に古いブラウザでは動作しない場合があります。これらのブラウザをサポートする必要がある場合は、ポリフィルの使用を検討してください。(#8629) - ランタイムは
CustomEvent
コンストラクタを使用するようになりました。これは非常に古いブラウザでは動作しない場合があります。これらのブラウザをサポートする必要がある場合は、ポリフィルの使用を検討してください。(#8775) svelte/store
からStartStopNotifier
インターフェース (writable
などの create 関数に渡されます) を使用して独自のストアを最初から実装している人は、set 関数に加えて update 関数を渡す必要があります。これは、ストアを使用している人や既存の Svelte ストアを使用してストアを作成している人には影響ありません。(#6750)derived
は、渡されたストアではなく、偽の値でエラーをスローするようになりました。(#7947)- パブリック API ではない内部メソッドの使用をさらに抑制するために、
svelte/internal
の型定義が削除されました。これらのほとんどは Svelte 5 で変更される可能性が高いためです - DOM ノードの削除がバッチ処理されるようになり、順序がわずかに変更されました。これにより、これらの要素で
MutationObserver
を使用している場合に、発生するイベントの順序に影響を与える可能性があります(#8763) - 以前に
svelte.JSX
名前空間を通じてグローバルタイピングを拡張した場合、svelteHTML
名前空間を使用するように移行する必要があります。同様に、svelte.JSX
名前空間を使用して型定義を使用していた場合は、svelte/elements
から型を使用するように移行する必要があります。対処方法の詳細については、こちら を参照してください