迁移到 Pug 2(Migrating to Pug 2)

¥Migrating to Pug 2

Pug 2 于 2016 年 8 月发布。为了使新版本的改进成为可能,我们必须决定弃用或删除一些 API 和未记录的语言功能。我们努力使这些更改尽可能不具有干扰性,其中许多更改以前在控制台警告中是不鼓励的。

¥Pug 2 was released in August 2016. To make improvements in the new release possible, we had to make the decision of deprecating or removing some APIs and undocumented language features. We made an effort to make these changes as unintrusive as possible, and many of these changes were previously discouraged in console warnings.

本文详细介绍了如何将应用从 Jade 转换为 Pug v2。

¥This article details how you can convert your application to Pug v2 from Jade.

项目重命名(Project Rename)

¥Project Rename

由于商标问题,随着 Pug 2 的发布,项目名称已从 “Jade” 更改为 “Pug”。这也意味着我们已经将官方支持的文件扩展名从 .jade 更改为 .pug。尽管 .jade 仍然受支持,但已被弃用。我们鼓励所有用户立即过渡到 .pug

¥Due to a trademark issue, the project name has been changed from “Jade” to “Pug” in conjunction with the release of Pug 2. This also means that we have changed the official supported file extension from .jade to .pug. Although .jade is still supported, it is deprecated. All users are encouraged to transition to .pug immediately.

删除的语言功能(Removed Language Features)

¥Removed Language Features

大多数删除可以被我们的官方 linter pug-lint 自动检测到。

¥Most of these removals can be automatically detected by pug-lint, our official linter.

旧版 Mixin 调用(Legacy Mixin Call)

¥Legacy Mixin Call

//- old

mixin foo('whatever')
//- new

+foo('whatever')

我们删除了调用 mixin 的旧语法,以便更容易区分声明和调用。(所有旧语法的使用都会在 Jade v1 中引起警告。)

¥We removed the legacy syntax for calling a mixin to make it easier to differentiate between declarations and calls. (All uses of the old syntax caused warnings in Jade v1.)

属性插值(Attribute Interpolation)

¥Attribute Interpolation

//- old

a(href='#{link}')

a(href='before#{link}after')
//- new

a(href=link)

//- (on Node.js/io.js ≥ 1.0.0)
a(href=`before${link}after`) 
//- (everywhere)
a(href='before' + link + 'after')

我们取消了对属性插值的支持,因为实现过于复杂,并且该功能往往会阻止用户了解他们可以使用任何 JavaScript 值来代替属性。有关属性语法的更多信息,请查看我们的 属性文档

¥We removed support for interpolation in attributes since the implementation was unnecessarily complex, and the feature tended to discourage users from learning that they can just use any JavaScript value in place of attributes. Check our attribute documentation for more information on attribute syntax.

each 前缀的语法(Prefixed each Syntax)

¥Prefixed each Syntax

//- old

- each a in b
  = a

- for a in b
  = a
//- new

each a in b
  = a

for a in b
  = a

each 不是 JavaScript 语法的一部分,因此在 JavaScript 行中使用 each “keyword” 既令人困惑,又很黑客(就实现而言)。这同样适用于无括号的 for 关键字。

¥each is not part of the JavaScript syntax, so the use of each “keyword” in a JavaScript line is confusing as well as hackish (in terms of implementation). The same applies to parentheses-less for keyword.

只需删除 -,你的代码就可以再次运行。

¥Simply remove - and your code should work again.

删除了 API(Removed API)

¥Removed API

这些导出的属性和编译选项已被删除。在你的应用中,请确保你没有使用这些 API。

¥These exported properties and compilation options have been removed. In your application, make sure you are not using these APIs.

属性(Properties)

¥Properties

doctype(doctype)

此前,未记录的 jade.doctype 对象包含 文档类型快捷方式 的哈希值。通过扩展此对象,用户可以创建其他或修改现有的文档类型快捷方式。

¥Previously, the undocumented jade.doctype object contained a hash of doctype shortcuts. By extending this object, users could create additional or modify existing doctype shortcuts.

在 Pug v2 中,该对象已从 Pug 中拆分到 doctypes 包中。要扩展文档类型快捷方式,你可以编写一个 codeGen 插件。

¥In Pug v2, this object has been split from Pug into the doctypes package. To extend doctype shortcuts, you could write a codeGen plugin.

nodes(nodes)

以前,未记录的 jade.nodes 对象持有类的哈希值,这些类充当(也未记录的)Jade 抽象语法树的节点的构造函数。在 Pug v2 中,我们放弃了这种方法,转而使用 AST 节点中的 type 属性进行动态类型。

¥Previously, the undocumented jade.nodes object held a hash of classes that serve as the constructor of the nodes of the (also undocumented) Jade abstract syntax tree. In Pug v2, we have abandoned this approach in favor of duck typing using the type property in AST nodes.

selfClosing(selfClosing)

以前,未记录的 jade.selfClosing 数组可用于扩展或修改 自闭合标签 的行为。

¥Previously, the undocumented jade.selfClosing array could used to extend or modify the behavior of self-closing tags.

在 Pug v2 中,该数组已从 Pug 中拆分到 void-elements 包中。要修改这个数组,你可以编写一个 codeGen 插件。

¥In Pug v2, this array has been split from Pug into the void-elements package. To modify this array, you could write a codeGen plugin.

utils(utils)

以前,未记录的 jade.utils 对象包含三个对模板引擎内部有用的函数。

¥Previously, the undocumented jade.utils object contained three functions that are useful for template engine internals.

utils.merge 已从 Pug 中删除,因为它不再使用。可以使用 ES2015 Object.assign 方法以及其他变体大致复制其功能。

¥utils.merge has been removed from Pug, as it is not used anymore. Its functionality can be roughly replicated using the ES2015 Object.assign method, among other variants.

utils.stringify 已从 Pug 中拆分到 js-stringify 包中,并针对可能的 XSS 攻击提供了额外的保护。建议所有用户使用该包。

¥utils.stringify has been split from Pug into the js-stringify package, with additional protection against possible XSS attacks. All users are advised to use that package.

utils.walkAST 已被拆分到 pug-walk 包中。

¥utils.walkAST has been split into the pug-walk package.

CompilerLexerParser(Compiler, Lexer, Parser)

以前,未记录的 Jade compilerlexerparser 类是通过这些属性导出的。用户可以创建自己的从这些类派生的编译器、词法分析器和解析器,以便自定义编译行为。

¥Previously, the undocumented Jade compiler, lexer, and parser classes were exported through these properties. Users were allowed to create their own compilers, lexers, and parsers that derive from these classes, in order to customize compilation behaviors.

Pug v2 允许通过插件自定义编译过程,并且这些导出的属性现在已被删除。

¥Pug v2 allows customization of the compilation process through plugins, and these exported properties are now removed.

Pug v2 等价类现在是 pug-code-genpug-lexerpug-parser 包的一部分,但有各种不兼容的更改。

¥The Pug v2 equivalent of classes are now part of the pug-code-gen, pug-lexer, and pug-parser packages, with various incompatible changes.

选项(Options)

¥Options

compilerlexerparser(compiler, lexer, parser)

这些选项与删除的 CompilerLexerParser 结合使用。

¥These options were used in conjunction with the removed Compiler, Lexer, and Parser classes.

client(client)

client 选项用于客户端函数编译。它在 2013 年被弃用,取而代之的是 compileClient 函数,从那时起它的使用就受到警告。

¥The client option was used for client function compilation. It was deprecated in favor of the compileClient function in 2013 year, and its use has been warned against since then.

Pug 中文网 - 粤ICP备13048890号