Skip to navigation Skip to main content
Eleventy
Eleventy Documentation
Stable
3.0.0
Toggle Menu
Eleventy 5.81s
Gatsby 43.36s

在命令行下使用

Prerequisites
  1. Eleventy runs in a Terminal application. Well, wait—what is a Terminal?
  2. Have you already installed Eleventy?

Here’s the first command you can enter in your Terminal application to run Eleventy:

npm pnpm yarn
# Searches the current directory, outputs to ./_site
npx @11ty/eleventy

# `npx @11ty/eleventy` is the same as:
npx @11ty/eleventy --input=. --output=_site
# Searches the current directory, outputs to ./_site
pnpm exec eleventy

# `pnpm exec eleventy` is the same as:
pnpm exec eleventy --input=. --output=_site
# Searches the current directory, outputs to ./_site
yarn exec eleventy

# `yarn exec eleventy` is the same as:
yarn exec eleventy --input=. --output=_site

阅读有关 --input--output 的更多信息。请注意,我们推荐通过 配置 文件设置输入和输出目录。

假定当前目录下有一个名为 template.md 的文件,最终将被渲染并输出为 _site/template/index.html 文件。请参考 永久链接 章节以了解更多信息。

npm pnpm yarn
# Use only a subset of template types
npx @11ty/eleventy --formats=md,html,ejs

# Don’t process any formats
npx @11ty/eleventy --formats=

# Find out the most up-to-date list of commands (there are more)
npx @11ty/eleventy --help
# Use only a subset of template types
pnpm exec eleventy --formats=md,html,ejs

# Don’t process any formats
pnpm exec eleventy --formats=

# Find out the most up-to-date list of commands (there are more)
pnpm exec eleventy --help
# Use only a subset of template types
yarn exec eleventy --formats=md,html,ejs

# Don’t process any formats
yarn exec eleventy --formats=

# Find out the most up-to-date list of commands (there are more)
yarn exec eleventy --help
  • The default for --formats= changed in v3.0.0 from an alias of * to an empty set.

保存文件后重新运行 Eleventy

npm pnpm yarn
# Add a web server to apply changes and
# refresh automatically. We’ll also --watch for you.
npx @11ty/eleventy --serve

# Change the web server’s port—use localhost:8081
npx @11ty/eleventy --serve --port=8081

# Watch and re-run when files change, without the web server.
npx @11ty/eleventy --watch
# Add a web server to apply changes and
# refresh automatically. We’ll also --watch for you.
pnpm exec eleventy --serve

# Change the web server’s port—use localhost:8081
pnpm exec eleventy --serve --port=8081

# Watch and re-run when files change, without the web server.
pnpm exec eleventy --watch
# Add a web server to apply changes and
# refresh automatically. We’ll also --watch for you.
yarn exec eleventy --serve

# Change the web server’s port—use localhost:8081
yarn exec eleventy --serve --port=8081

# Watch and re-run when files change, without the web server.
yarn exec eleventy --watch

--quiet 参数让输出更安静

npm pnpm yarn
# Shhhhh—Don’t log so much to the console
npx @11ty/eleventy --quiet
# Shhhhh—Don’t log so much to the console
pnpm exec eleventy --quiet
# Shhhhh—Don’t log so much to the console
yarn exec eleventy --quiet

--dryrun 参数用于小测试

运行但不向文件系统写入任何文件。在 调试 时很有用。

npm pnpm yarn
# Run Eleventy but don’t write any files
npx @11ty/eleventy --dryrun
# Run Eleventy but don’t write any files
pnpm exec eleventy --dryrun
# Run Eleventy but don’t write any files
yarn exec eleventy --dryrun

--config 参数用于指定配置文件的文件名

npm pnpm yarn
# Override the default eleventy project config filename (.eleventy.js)
npx @11ty/eleventy --config=myeleventyconfig.js
# Override the default eleventy project config filename (.eleventy.js)
pnpm exec eleventy --config=myeleventyconfig.js
# Override the default eleventy project config filename (.eleventy.js)
yarn exec eleventy --config=myeleventyconfig.js

Read more about Configuration files.

Added in v3.0.0If your specified --config file does not exist, Eleventy will throw an error.

--to 参数用于输出 JSON

npm pnpm yarn
# Output a JSON structure (does not write to the file system)
npx @11ty/eleventy --to=json

# 以通过换行符分隔的 JSON 格式输出(不写入文件系统)
npx @11ty/eleventy --to=ndjson

# 默认行为(输出到文件系统)
npx @11ty/eleventy --to=fs
# Output a JSON structure (does not write to the file system)
pnpm exec eleventy --to=json

# 以通过换行符分隔的 JSON 格式输出(不写入文件系统)
pnpm exec eleventy --to=ndjson

# 默认行为(输出到文件系统)
pnpm exec eleventy --to=fs
# Output a JSON structure (does not write to the file system)
yarn exec eleventy --to=json

# 以通过换行符分隔的 JSON 格式输出(不写入文件系统)
yarn exec eleventy --to=ndjson

# 默认行为(输出到文件系统)
yarn exec eleventy --to=fs

更多信息请参阅 ndjson

--incremental for Partial Incremental Builds

npm pnpm yarn
# *Repeat* builds only operate on files that have changed
npx @11ty/eleventy --watch --incremental
npx @11ty/eleventy --serve --incremental

# Skip the initial full build with `--ignore-initial`
npx @11ty/eleventy --serve --incremental --ignore-initial

# Pass in a template path, watch/serve not required
# Added in v3.0.0
npx @11ty/eleventy --incremental=myfile.md
# *Repeat* builds only operate on files that have changed
pnpm exec eleventy --watch --incremental
pnpm exec eleventy --serve --incremental

# Skip the initial full build with `--ignore-initial`
pnpm exec eleventy --serve --incremental --ignore-initial

# Pass in a template path, watch/serve not required
# Added in v3.0.0
pnpm exec eleventy --incremental=myfile.md
# *Repeat* builds only operate on files that have changed
yarn exec eleventy --watch --incremental
yarn exec eleventy --serve --incremental

# Skip the initial full build with `--ignore-initial`
yarn exec eleventy --serve --incremental --ignore-initial

# Pass in a template path, watch/serve not required
# Added in v3.0.0
yarn exec eleventy --incremental=myfile.md

更多信息请参阅 增量构建(incremental builds) 章节。额外信息 GitHub #3324

--ignore-initial to run Eleventy without an Initial Build Added in v2.0.0

Be wary of any file changes that happened while Eleventy wasn’t running!

npm pnpm yarn
# Don’t build when Eleventy starts, only build on file changes
npx @11ty/eleventy --watch --ignore-initial
npx @11ty/eleventy --serve --ignore-initial

# Works great with Incremental
npx @11ty/eleventy --serve --incremental --ignore-initial
# Don’t build when Eleventy starts, only build on file changes
pnpm exec eleventy --watch --ignore-initial
pnpm exec eleventy --serve --ignore-initial

# Works great with Incremental
pnpm exec eleventy --serve --incremental --ignore-initial
# Don’t build when Eleventy starts, only build on file changes
yarn exec eleventy --watch --ignore-initial
yarn exec eleventy --serve --ignore-initial

# Works great with Incremental
yarn exec eleventy --serve --incremental --ignore-initial

Using the Same Input and Output

的确,你可以将 inputoutput 参数设置为同一个目录,如下所示:

npm pnpm yarn
# Parse and write Markdown to HTML, respecting directory structure.
npx @11ty/eleventy --input=. --output=. --formats=md
# Parse and write Markdown to HTML, respecting directory structure.
pnpm exec eleventy --input=. --output=. --formats=md
# Parse and write Markdown to HTML, respecting directory structure.
yarn exec eleventy --input=. --output=. --formats=md
WARNING:
请注意这里的 --formats=html 参数!如果你多次运行 Eleventy,它将尝试将输出的文件作为输入文件处理(这将导致报错)。更多信息请参阅 HTML 模板文档

Other pages in Getting Started: