twig 函数 encore_entry_link_tags('app') 和 encore_entry_script_tags('app') 都会导致内部服务器错误

问题描述 投票:0回答:1

我正在本地建立一个新的 Pimcore 网站,但我似乎无法使提到的 twig 功能正常工作。我收到此错误消息Error

当我注释掉

encore_entry_link_tags('app')
时,
 encore_entry_script_tags('app')
会导致同样的错误。

我按照 this 指南将 webpack encore 安装到我的 pimcore 项目中,并且捆绑工作得很好。当我运行

npm run build
时,SCSS 和 javascript 文件都会被捆绑并保存到
public/build
。看来仅仅最后一步是不行的。

这是我当前的 webpack.config.js

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.com/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())
    .enableSassLoader()
    .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

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

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

// uncomment to get integrity="..." 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();

我尝试过查看不同的项目,看看他们是如何实现这一点的,但没有取得太大成功。我的代码和配置中的所有内容看起来都与其他功能项目中的一样。人工智能也没有多大帮助,而且似乎没有其他人遇到过这个特殊问题,这就是我决定在这里提问的原因。

感谢您的任何帮助!

php symfony twig webpack-encore pimcore
1个回答
0
投票

经过更多研究,我发现了一个名为

webpack_encore.yaml
的文件。我在一个旧的私人项目中找到了它,它看起来像这样:

webpack_encore:
    # The path where Encore is building the assets - i.e. Encore.setOutputPath()
    output_path: '%kernel.project_dir%/public/build'
    # If multiple builds are defined (as shown below), you can disable the default build:
    # output_path: false

    # Set attributes that will be rendered on all script and link tags
    script_attributes:
        defer: true
        # Uncomment (also under link_attributes) if using Turbo Drive
        # https://turbo.hotwired.dev/handbook/drive#reloading-when-assets-change
        # 'data-turbo-track': reload
    # link_attributes:
        # Uncomment if using Turbo Drive
        # 'data-turbo-track': reload

    # If using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
    # crossorigin: 'anonymous'

    # Preload all rendered script and link tags automatically via the HTTP/2 Link header
    # preload: true

    # Throw an exception if the entrypoints.json file is missing or an entry is missing from the data
    # strict_mode: false

    # If you have multiple builds:
    # builds:
        # pass "frontend" as the 3rg arg to the Twig functions
        # {{ encore_entry_script_tags('entry1', null, 'frontend') }}

        # frontend: '%kernel.project_dir%/public/frontend/build'

    # Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
    # Put in config/packages/prod/webpack_encore.yaml
    # cache: true

framework:
    assets:
        json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'

#when@prod:
#    webpack_encore:
#        # Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
#        # Available in version 1.2
#        cache: true

#when@test:
#    webpack_encore:
#        strict_mode: false

通常这个文件会通过 symfony flex 自动添加,但由于我使用的是 Pimcore,它不使用 symfony flex,所以我必须自己创建它。

添加此文件后,我没有再遇到任何问题,虽然我仍在测试一般功能,但它可以处理到目前为止我扔给它的所有内容。

希望这对将来的人有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.