跳到内容

安装 Encore

编辑此页

首先,请确保您已安装 Node.js。然后,按照以下说明操作,这些说明取决于您是否在 Symfony 应用中安装 Encore。

在 Symfony 应用中安装 Encore

运行这些命令以在您的项目中安装 PHP 和 JavaScript 依赖

1
2
$ composer require symfony/webpack-encore-bundle
$ npm install

如果您正在使用 Symfony Flex,这将安装并启用 WebpackEncoreBundle,创建 assets/ 目录,添加 webpack.config.js 文件,并将 node_modules/ 添加到 .gitignore。您可以跳过本文的其余部分,通过阅读 Encore:设置您的项目,开始编写您的第一个 JavaScript 和 CSS!

如果您未使用 Symfony Flex,您需要按照下一节中显示的说明自行创建所有这些目录和文件。

在非 Symfony 应用中安装 Encore

通过 npm 将 Encore 安装到您的项目中

1
$ npm install @symfony/webpack-encore --save-dev

此命令创建(或修改)一个 package.json 文件并将依赖项下载到 node_modules/ 目录中。

提示

您应该将 package.jsonpackage-lock.json 提交到版本控制,但忽略 node_modules/

创建 webpack.config.js 文件

接下来,在您的项目根目录中创建一个新的 webpack.config.js 文件。这是 Webpack 和 Webpack Encore 的主要配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const Encore = require('@symfony/webpack-encore');

// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    // only needed for CDN's or sub-directory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
     */
    .addEntry('app', './assets/app.js')

    // enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
    .enableStimulusBridge('./assets/controllers.json')

    // When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
    .splitEntryChunks()

    // will require an extra script tag for runtime.js
    // but, you probably want this, unless you're building a single-page app
    .enableSingleRuntimeChunk()

    /*
     * FEATURE CONFIG
     *
     * Enable & configure other features below. For a full
     * list of features, see:
     * https://symfony.ac.cn/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())

    .configureBabel((config) => {
        config.plugins.push('@babel/plugin-transform-class-properties');
    })

    // enables @babel/preset-env polyfills
    .configureBabelPresetEnv((config) => {
        config.useBuiltIns = 'usage';
        config.corejs = 3;
    })

    // enables Sass/SCSS support
    //.enableSassLoader()

    // uncomment if you use TypeScript
    //.enableTypeScriptLoader()

    // uncomment if you use React
    //.enableReactPreset()

    // uncomment to get attributes on your script & link tags
    // requires WebpackEncoreBundle 1.4 or higher
    //.enableIntegrityHashes(Encore.isProduction())

    // uncomment if you're having problems with a jQuery plugin
    //.autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

创建其他支持文件

接下来,打开新的 assets/app.js 文件,其中包含一些 JavaScript 代码并导入一些 CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
// assets/app.js
/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you import will output into a single css file (app.css in this case)
import './styles/app.css';

// start the Stimulus application
import './bootstrap';

以及新的 assets/styles/app.css 文件

1
2
3
4
/* assets/styles/app.css */
body {
    background-color: lightgray;
}

您还应该添加一个 assets/bootstrap.js 文件,它初始化 Stimulus:一个您很快将了解的系统

1
2
3
4
5
6
7
8
9
10
11
12
// assets/bootstrap.js
import { startStimulusApp } from '@symfony/stimulus-bridge';

// Registers Stimulus controllers from controllers.json and in the controllers/ directory
export const app = startStimulusApp(require.context(
    '@symfony/stimulus-bridge/lazy-controller-loader!./controllers',
    true,
    /\.(j|t)sx?$/
));

// register any custom, 3rd party controllers here
// app.register('some_controller_name', SomeImportedController);

然后创建一个 assets/controllers.json 文件,它也适用于 Stimulus 系统

1
2
3
4
{
    "controllers": [],
    "entrypoints": []
}

最后,虽然是可选的,但将以下 scripts 添加到您的 package.json 文件中,以便您可以在文档的其余部分运行相同的命令

1
2
3
4
5
6
"scripts": {
    "dev-server": "encore dev-server",
    "dev": "encore dev",
    "watch": "encore dev --watch",
    "build": "encore production --progress"
}

您将在 Encore:设置您的项目中自定义并了解更多关于这些文件的信息。当您执行 Encore 时,它会要求您根据您启用的 Encore 功能安装更多依赖项。

警告

部分文档将使用特定于 Symfony 或 Symfony 的 WebpackEncoreBundle 的功能。这些是可选的,并且是指向 Encore 生成的资源路径的特殊方式,这些路径启用了诸如版本控制代码分割之类的功能。

这项工作,包括代码示例,均根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本