Symfony Encore 多个资产清单

Symfony Encore multiple asset manifests

我有一个关于 Symfony 3.4 中的 Encore 和资产版本控制的问题。

在我的 webpack.config.js 我有两种配置。 第一个用于 JS 文件,另一个用于编译 .less。

每个配置由 Encore.reset()

重置

输出包正在通过 .enableVersioning 生成带有版本控制的清单,所以我在

中有两个 manifest.json
web/js/manifest.json
web/stylesheets/manifest.json

根据文档,要通过清单加载我的资产,我需要在 config.yml

中声明它
   assets:
        base_path: "%myapp.http.site_assets_suffix%" 
        stylesheets:
            json_manifest_path: "%kernel.project_dir%/web/assets/stylesheets/manifest.json"

如果我想link到style.css由webpack生成,我使用

asset("stylesheets/style.css")

但在我的应用程序中,我有两个清单,我认为由于有两个 Encore 配置,这无法更改。

我试过添加一些东西,例如

packages:
     stylesheets:
                json_manifest_path: "%kernel.project_dir%/web/assets/stylesheets/manifest.json"
     js:
                json_manifest_path: "%kernel.project_dir%/web/assets/js/manifest.json"

因为我在某处看到过,但不幸的是这根本行不通。

我考虑过在上一个 webpack 入口点将两个清单合并为一个,但这可能很耗时。

除了合并manfiests或者合并js + less编译成一个大的Encore任务,还有其他解决方案吗?

我找到了解决方案

assets:
    base_path: 'path%'
    packages:
        noversion:
            version: false
            version_format: "%%1$s"
            base_path: "path%"
        stylesheets:
            json_manifest_path: "%kernel.project_dir%/web/assets/stylesheets/manifest.json"
        js:
            json_manifest_path: "%kernel.project_dir%/web/assets/js/manifest.json"
        admin:
            json_manifest_path: "%kernel.project_dir%/web/assets/js/admin/manifest.json"

然后在.twig个文件中,你需要将其命名为

    <script src="{{ asset('assets/DIRNAME/WEBPACK_ENTRY_NAME_HERE', ASSET_PACKAGE_NAME_HERE) }}"></script>

就我而言

<script src="{{ asset('assets/js/backend.js', 'js') }}"></script>

其中 WEBPACK_ENTRY_NAME 是来自 webpack.config.js 的 Webpack/Encore 包的名称,在我的例子中是

.setOutputPath('./web/assets/js')
    .setPublicPath('/assets/js')
    .addEntry('backend',

很抱歉延迟回答,但我忘记了。

Webpack Encore 使用 webpack-manifest-plugin 生成 manifest.json 文件。

根据文档,您可以在设置配置时指定 options.seed

A cache of key/value pairs to used to seed the manifest. This may include a set of custom key/value pairs to include in your manifest, or may be used to combine manifests across compilations in multi-compiler mode. To combine manifests, pass a shared seed object to each compiler's ManifestPlugin instance.

 Encore.configureManifestPlugin(options => {
            let seed;

            try {
                // require your existing manifest content if exists
                seed = require(path.join(outputPath, 'manifest.json'));
            }

            catch (e) {
                // fallback if manifest.json is missing
                seed = {};
            }

            // inject your latest config as seed.
            // The plugin will update it and rewrite manifest.json with correct values (overwrite existing keys, append news)
            options.seed = seed;

           // Also i add a trick to avoid "License.txt" entries
            options.generate = function(seed, files, entrypoints) {
                // trick to avoid generate useless versionned entries like License
                const filesWithoutLicense = files.filter(file => {
                    return file.path.match(/.*LICENSE.*/) === null;
                });

                const newManifestContent = filesWithoutLicense.reduce(
                    (newManifestContent, file) => {
                        newManifestContent[file.name] = file.path;
                        return newManifestContent;
                    },

                    seed
                );


                return newManifestContent;
            }