在Symfony 4中将引导程序与Webpack Encore一起使用

时间:2018-09-28 09:00:49

标签: bootstrap-4 symfony4 webpack-encore

我想在Symfony 4.1中将引导程序与Webpack Encore一起使用,但是引导程序不起作用。在本文的template / base.html.twig文件中,我使用了一些引导程序类,但未将其考虑在内,我也不明白为什么。

我安装了用bootstrap引导所需的依赖项:

yarn add bootstrap --dev
yarn add jquery --dev
yarn add popper.js --dev

template / base.html.twig

在此文件中,我使用资产功能来考虑以下文件: build / app.scss build / app.js

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}
        <link rel="stylesheet" href="{{ asset('build/app.scss') }}">
        {% endblock %}
    </head>
    <body>
        {% block body %}
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <a class="navbar-brand" href="#">Navbar</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" ar$
            <span class="navbar-toggler-icon"></span>
          </button>
        </nav>

        {% endblock %}
        {% block javascripts %}
            <script src="{{ asset('build/app.js') }}"></script>
        {% endblock %}
    </body>
</html>

在以下两个文件中,我需要并导入了引导所需的内容。

资产/js/app.js

require('../css/app.scss');
var $ = require('jquery');
require('bootstrap');

资产/css/app.scss

@import "~bootstrap/scss/bootstrap";

webpack.config.js

在此文件中,我使用 enableSassLoader()来激活Sass,并使用 autoProvidejQuery()来访问jQuery作为全局变量。

var Encore = require('@symfony/webpack-encore');

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
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
     */
    .addEntry('app', './assets/js/app.js')

    /*
     * 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())

    // enables Sass/SCSS support
    .enableSassLoader()

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

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

module.exports = Encore.getWebpackConfig();

命令 yarn encore dev 正确构建了所有内容。但是,我在屏幕上看不到引导主题。

预先感谢

2 个答案:

答案 0 :(得分:1)

头部的 template / base.html.twig 文件存在错误。 public / build目录中的全局 asset / js / app.scss 的结果是 app.css 文件,而不是.scss扩展名文件。这是我们可以在 assets / js /目录中找到的所有javascript文件生成的结果。

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>{% block title %}Welcome!{% endblock %}</title>
    {% block stylesheets %}
    <link rel="stylesheet" href="{{ asset('build/app.css') }}">
    {% endblock %}
</head>

答案 1 :(得分:0)

WebpackEncoreBundle的两个Twig帮助者可以为您完成大部分工作:

https://symfony.com/doc/current/frontend/encore/simple-example.html#the-import-and-export-statements

{# templates/base.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        <!-- ... -->

        {% block stylesheets %}
            {# 'app' must match the first argument to addEntry() in webpack.config.js #}
            {{ encore_entry_link_tags('app') }}

            <!-- Renders a link tag (if your module requires any CSS)
                 <link rel="stylesheet" href="/build/app.css"> -->
        {% endblock %}
    </head>
    <body>
        <!-- ... -->

        {% block javascripts %}
            {{ encore_entry_script_tags('app') }}

            <!-- Renders app.js & a webpack runtime.js file
                <script src="/build/runtime.js"></script>
                <script src="/build/app.js"></script> -->
        {% endblock %}
    </body>
</html>