首页 > 解决方案 > Symfony - 使用 Encore 在开发模式下构建时 JS 忽略

问题描述

我对 PHP、Symfony 或 Encore 真的很陌生,并且在我的配置中找不到问题(尽管在谷歌和这个网站上查看)。

这是问题(我正在研究 symfony 项目的基本配置):

我的“资产”文件夹中有几个 .js 文件。

如果我在 prod 模式下使用 : 构建这些资产yarn run encore prod,它们会在 public/build 中使用它们的版本控制标签生成。当我运行“symfony serve”在本地查看我的站点时,一切正常。

但是,如果我尝试在开发模式 ( yarn run encore dev) 下生成这些资产,那么应用程序会完全忽略它们(即使我清除缓存并重新运行服务器)。

我的 .env.local 文件确实指向“dev”环境(我的 .env 文件也是如此)。我试图在我的 webpack.config.js 文件中将“.enableVersioning(Encore.isProduction())”更改为“.enableVersioning()”,这样资产将生成带有版本标签,甚至是 dev,但即使这样,它们也会被忽略我的代码。

这是我的 webpack.config 文件:

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')
    .addEntry('projet', './assets/projet.js')
    .addEntry('categorie', './assets/categorie.js')
    .addEntry('utilisateur', './assets/utilisateur.js')
    .addEntry('saisie', './assets/saisie.js')
    .addEntry('recherche', './assets/recherche.js')
    .addEntry('pagination', './assets/pagination.js')
    .addEntry('home', './assets/home.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())

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

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

    // enables Sass/SCSS support
    .enableSassLoader()
    .enablePostCssLoader()
  
    .copyFiles({
         from: './assets/images',
       // if versioning is enabled, add the file hash too
        to: 'images/[path][name].[hash:8].[ext]',
        // only copy files matching this pattern
        pattern: /\.(png|jpg|jpeg|svg)$/
    })

    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

以及我如何在我的树枝文件中调用这些脚本:

{% block user_javascripts %}
    {{parent()}}

    <script src="{{ asset('build/recherche.js') }}"></script>
{% endblock %}

在我的 base.html.twig 文件的正文部分的末尾有这个:

{% block user_javascripts %}
            {{ encore_entry_script_tags('app') }}
{% endblock %}

你知道我如何在开发模式下使用我的资产吗?

谢谢您的帮助 !

标签: javascriptsymfonyassetswebpack-encore

解决方案


推荐阅读