gpt4 book ai didi

Symfony 4 - Webpack Encore bootstrap css 不在输出中

转载 作者:行者123 更新时间:2023-12-03 15:36:10 28 4
gpt4 key购买 nike

我正在尝试将 Webpack Encore 引入我的 Symfony 4 应用程序以管理 JS 和 CSS Assets 。

我安装了 yarnnodejs .

然后composer require encore然后 yarn 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 安装 bootstrap 和其他依赖项:
{
"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 ?

最佳答案

根据文档 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

关于Symfony 4 - Webpack Encore bootstrap css 不在输出中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54966110/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com