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,这可以通过将
"browser"
添加到conditionNames
数组来完成。如果您已设置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 版本 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'); // 我仍然可以省略细节参数
const dispatch: EventDispatcher
<"noArgument">(type: "noArgument", parameter?: null | undefined, options?: DispatchOptions | undefined) => boolean
dispatch('noArgument', 'surprise'); // 我仍然可以添加细节参数
// Svelte 版本 4 使用 TypeScript 严格模式:
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'); // 错误,缺少参数
const dispatch: EventDispatcher
<"noArgument">(type: "noArgument", parameter?: null | undefined, options?: DispatchOptions | undefined) => boolean
dispatch('noArgument', 'surprise'); // 错误,无法传递参数
Action
和ActionReturn
现在具有默认参数类型undefined
,这意味着如果您想指定此 action 接收一个参数,则需要指定泛型。迁移脚本将自动迁移此内容 (#7442)
const action: Action = (node, params) => { ... } // 如果您以任何方式使用 params,这现在是一个错误
const const action: Action<HTMLElement, string>
action: type Action = /*unresolved*/ any
Action<HTMLElement, string> = (node: any
node, params: any
params) => { ... } // params 的类型为 string
- 如果您以异步方式从
onMount
返回一个函数,现在会出现类型错误,因为这可能是您代码中的一个错误,您期望回调在销毁时被调用,而这仅适用于同步返回的函数 (#8136)
// 此更改显示了一个实际错误的示例
onMount(
// someCleanup() 未被调用,因为传递给 onMount 的函数是异步的
async () => {
const something = await foo();
// someCleanup() 被调用,因为传递给 onMount 的函数是同步的
() => {
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
现在拥有所有的类型功能。用 SvelteComponent
替换所有 SvelteComponentTyped
实例。
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}>随机</button>
<svelte:element this={component} />
迁移脚本将自动为您完成这两项。 (#8512)
过渡默认是局部的
过渡现在默认是局部的,以防止在页面导航时造成混淆。“局部”意味着如果在嵌套的控制流块(each/if/await/key
)中,且不是父块,而是上面的一个块被创建/销毁,则不会播放过渡。在以下示例中,slide
进入动画仅在 success
从 false
变为 true
时播放,但在 show
从 false
变为 true
时不会播放:
{#if show}
...
{#if success}
<p in:slide>成功</p>
{/each}
{/if}
要使过渡全局,请添加 |global
修饰符 - 那么它们将在任何上层控制流块被创建/销毁时播放。迁移脚本将自动为您完成这项工作。 (#6686)
默认插槽绑定
默认插槽绑定不再暴露给命名插槽,反之亦然:
<script>
import Nested from './Nested.svelte';
</script>
<Nested let:count>
<p>
默认插槽中的计数 - 可用:{count}
</p>
<p slot="bar">
条形插槽中的计数 - 不可用:{count}
</p>
</Nested>
这使插槽绑定更加一致,因为当默认插槽来自列表而命名插槽不是时,行为是未定义的。 (#6049)
预处理器
预处理器应用的顺序已更改。现在,预处理器按顺序执行,在一个组内,顺序为标记、脚本、样式。
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 日志:
// markup-1
// markup-2
// script-1
// script-2
// style-1
// style-2
// Svelte 4 日志:
// 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)
,这在非常老的浏览器中可能无法正常工作。如果您需要支持这些浏览器,请考虑使用 polyfill。 ([#8629](https://github.com/sveltejs/svelte/issues/8629)) - 运行时现在使用
CustomEvent
构造函数,这在非常老的浏览器中可能无法正常工作。如果您需要支持这些浏览器,请考虑使用 polyfill。 (#8775) - 使用
svelte/store
中的StartStopNotifier
接口(传递给writable
的创建函数等)从头开始实现自己的 stores 的人,现在需要在 set 函数之外传递一个更新函数。这对使用现有 Svelte store 的人或创建 store 的人没有影响。 (#6750) derived
现在将在假值上抛出错误,而不是传递给它的store。 (#7947)- 移除了
svelte/internal
的类型定义,以进一步阻止使用那些不是公共 API 的内部方法。大多数这些方法可能会在 Svelte 5 中改变。 - 现在移除 DOM 节点是批量执行的,这稍微改变了顺序,这可能会影响您在这些元素上使用
MutationObserver
的事件触发顺序。 (#8763) - 如果您之前通过
svelte.JSX
命名空间增强全局类型定义,您需要迁移到使用svelteHTML
命名空间。类似地,如果您使用svelte.JSX
命名空间来使用其类型定义,则需要迁移这些以使用svelte/elements
的类型。您可以在 这里 找到更多信息。