Symfony 4-Webpack Encore引导CSS不在输出中

时间:2019-03-03 06:15:27

标签: symfony webpack yarnpkg webpack-4 webpack-encore

我正试图将Webpack Encore引入我的Symfony 4应用程序中,以管理JS和CSS资产。

我安装了yarnnodejs

然后依次composer require encoreyarn install

我在app.js中有assets/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 require will output into a single css file (app.css in this case)
require('bootstrap');
require('../css/app.css');

// Need jQuery? Install it with "yarn add jquery", then uncomment to require it.
const $ = require('jquery');

console.log('Hello Webpack Encore! Edit me in assets/js/app.js');

使用yarn安装了引导程序和其他依赖项:

{
    "devDependencies": {
        "@symfony/webpack-encore": "^0.22.0",
        "bootstrap": "^4.3.1",
        "jquery": "^3.3.1",
        "popper.js": "^1.14.7",
        "webpack-notifier": "^1.6.0"
    },
    "license": "UNLICENSED",
    "private": true,
    "scripts": {
        "dev-server": "encore dev-server",
        "dev": "encore dev",
        "watch": "encore dev --watch",
        "build": "encore production --progress"
    }
}

然后运行yarn encore dev --watch

问题

我的public/build/app.css中没有任何引导CSS。

除了require('bootstrap');中的webpack.config.js以外,我还需要什么吗?

3 个答案:

答案 0 :(得分:2)

如果转到node_modules目录,请查找bootstrap并打开其package.json,您将找到以下条目:

"main": "dist/js/bootstrap",

当您这样做:

require("bootstrap");

在您的文件中,需要库main的{​​{1}}部分中指定的文件。
但是,您可以通过指定其(相对)路径来从库中获取任何文件:

package.json

现在您可以运行:

require("bootstrap/dist/css/bootstrap.css");

,Bootstrap CSS应该在您的yarn encore dev 中。

编辑(2019-03-05):
这是截屏视频 7的relevant block code。需要CSS!?,这是SymfonyCasts课程Webpack Encore: A Party for your Assets Tutorial的一部分。

答案 1 :(得分:2)

根据文档bootstrap in webpack encore

在webpack.config.js中,您可以拥有

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')

    .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())

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

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

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

    // uncomment if you use API Platform Admin (composer req api-admin)
    //.enableReactPreset()
    //.addEntry('admin', './assets/js/admin.js')
;

module.exports = Encore.getWebpackConfig();

然后使用以下命令安装jquery,popper.js,bootstrap,font-awesome(如果您使用的是npm并且需要font-awesome):

npm install jquery --save-dev
npm install popper.js --save-dev
npm install bootstrap@4 --save-dev
npm install font-awesome --save-dev

在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 require will output into a single css file (app.css in this case)
require('../css/app.css');

// Need jQuery? Install it with "yarn add jquery", then uncomment to require it.
const $ = require('jquery');
import 'popper.js';
import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.css';
import 'font-awesome/css/font-awesome.css';

$(document).ready(function(){
    // Put your jquery code here.
});

在模板中,您可以执行以下操作:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>title</title>
        {{ encore_entry_link_tags('app') }}  
    </head>
    <body>
         <div id="content_container"></div>

         {% block lib_javascripts %}
              {{ encore_entry_script_tags('app') }}
         {% endblock %}
         {% block javascripts %}{% endblock %}
    </body>
</html>                

您可以构建并观看。在您的项目目录中,您可以执行以下操作:

./node_modules/.bin/encore dev --watch

答案 2 :(得分:1)

塞萨尔所说的基本,但是我把它放在了答案中,因为评论会使它变得不可读:

import 'bootstrap';  // js-file
import 'bootstrap/dist/css/bootstrap.css'; // css file

不确定100%,但是我认为您还需要手动导入Popper和Jquery:

import Popper from 'popper.js';
import $ from 'jquery';