学习Symfony 5,在通过Webpack和Encore进行Bootstrap集成时遇到问题,并且在CSS上遇到了奇怪



我在这里,在过去几年离开Symfony之后回到了它。现在它正在使用 Webpack,当我上次触摸它时它曾经使用 Assetic。所以,我对很多与快速发展的JavaScript生态系统有关的东西有点困惑。

我访问了Sensiolabs的网站,按照有关如何将Bootstrap集成到我的项目中的说明进行操作。我在这个过程中走得很远,因为我的网页上有一个指向我的样式表的链接:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Blog index!</title>
<link rel="stylesheet" href="/build/app.css">
</head>
<body class="bg-light">

当我使用 css 的路径作为 url 时:http://localhost/symfony-01/public/build/app.css 我正在显示所有样式(我假设是正确的(。 但是,我在 html 中根本没有任何样式。起初我以为我没有使用 webapp 正确生成构建,但事实并非如此。

所以我在树枝模板中添加了一些愚蠢的元素,并给了它一个愚蠢的css,以确保样式不会过载或其他东西: 在基地.html.twig:

<div class="superfuzz"> HELLO GUYS</div>

在应用程序中.css :

div.superfuzz{
background-color: green !important;
}

当我查看从上面的 url 获得的 css 时,我可以向下发现该样式确实是由 Encore 添加的。然而,检查器中的样式是空的,它没有应用。我不知道为什么,这对我来说完全是一个谜。我唯一的想法是样式表的链接不正确,但同时我对 webpack、Encore 和 Yarn 很陌生。

也可能意味着 css 文件中的样式不正确。我没有做那么多 SCSS 来真正发现是否有问题,但通常 webpack 会将其转换为正确的 css 对吗?

这是我的 html 标头的树枝版本,以防万一:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
</head>
<body class="bg-light">

这是我的webpack.config.js:

var 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
*
* 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 your JavaScript imports CSS.
*/
.addEntry('app', [
'./assets/js/app.js',
'./node_modules/jquery/dist/jquery.slim.js',
'./node_modules/popper.js/dist/popper.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.js'])
// .addStyleEntry('css/app', [
//     './node_modules/bootstrap/dist/css/bootstrap.css',
//    './assets/css/app.css'
// ])
// 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())
// enables @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
.enableSassLoader()
// enables Sass/SCSS support
//.enableSassLoader()
// uncomment if you use TypeScript
//.enableTypeScriptLoader()
// 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()
// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin', './assets/js/admin.js')
;
module.exports = Encore.getWebpackConfig();

让我添加 assets/js/app.js:

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

和 scss 文件:

@import "~bootstrap/scss/bootstrap";

入口点.json 文件的内容:

{
"entrypoints": {
"app": {
"js": [
"/build/runtime.js",
"/build/vendors~app.js",
"/build/app.js"
],
"css": [
"/build/app.css"
]
}
}
}

好的,生成的链接 href 路径不正确。 感谢@Julien B.提供解决方案。

由于我还没有使用虚拟主机,我的 URL 遵循本地主机/项目文件夹/公共格式。我太信任Encore立即处理这个问题,但实际上我必须在webpack.config中编辑setPublicPath方法的参数.js以反映我的情况。所以它必须是.setPublicPath('/projectfolder/public/build'(而不是.setPublicPath('/build'(

更改某些内容后尝试清除缓存。 我对这个文件也有类似的问题。Symfony 找不到以下文件:

http://localhost/build/runtime.js
http://localhost/build/vendors~app.js
http://localhost/build/app.js

在此之后,问题消失了:

bin/console cache:clear; npm run watch

最新更新