Symfony 4 - Webpack Encore bootstrap css 不在输出中
Symfony 4 - Webpack Encore bootstrap css not in output
我正在尝试将 Webpack Encore 引入我的 Symfony 4 应用程序以管理 JS 和 CSS 资产。
我安装了 yarn
和 nodejs
。
然后 composer require encore
然后 yarn install
.
我在 assets/js/
中有 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('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');
已安装 bootstrap 和其他与 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
.
中没有任何 bootstrap css
在 webpack.config.js
中我还需要 require('bootstrap');
吗?
如果您转到 node_modules
目录,查找 bootstrap
并打开它的 package.json
您会找到条目:
"main": "dist/js/bootstrap",
当你这样做时:
require("bootstrap");
在您的文件中,您需要图书馆 package.json
的 main
部分中指定的文件。
但是,您可以通过指定其(相对)路径来从库中获取任何文件:
require("bootstrap/dist/css/bootstrap.css");
现在你可以运行:
yarn encore dev
和 Bootstrap CSS 应该在你的 app.css
.
编辑(2019-03-05):
这是 relevant block code of the screencast 7. Require CSS!?, part of the SymfonyCasts Course Webpack Encore: A Party for your Assets Tutorial.
更新 (2019-11-25):
此答案解释了如何使用 Node.js.[=29 提供的 require
函数绑定(导入)与 yarn
(或 npm
)一起安装的 CSS 库=]
ECMAScript 2015(也称为 ES6)引入了新功能 import
。 from WhiteRabbit.
中解释了它的用法
除了这两个选项之外,还有第三种方法可以导入 Bootstrap,即直接在您自己的 CSS 文件中使用 @import
:
@import 'bootstrap';
这仅在使用 Webpack 和 Yarn(或 NPM)时适用。 Webpack Encore 能够解析路径并导入相应的 CSS 文件。
基本上是 cezar 说的,但我把它放在一个答案中,因为评论会使它变得不可读:
import 'bootstrap'; // js-file
import 'bootstrap/dist/css/bootstrap.css'; // css file
不是 100% 确定,但我认为您还需要手动导入 Popper 和 Jquery:
import Popper from 'popper.js';
import $ from 'jquery';
根据文档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
我正在尝试将 Webpack Encore 引入我的 Symfony 4 应用程序以管理 JS 和 CSS 资产。
我安装了 yarn
和 nodejs
。
然后 composer require encore
然后 yarn install
.
我在 assets/js/
中有 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('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');
已安装 bootstrap 和其他与 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
.
在 webpack.config.js
中我还需要 require('bootstrap');
吗?
如果您转到 node_modules
目录,查找 bootstrap
并打开它的 package.json
您会找到条目:
"main": "dist/js/bootstrap",
当你这样做时:
require("bootstrap");
在您的文件中,您需要图书馆 package.json
的 main
部分中指定的文件。
但是,您可以通过指定其(相对)路径来从库中获取任何文件:
require("bootstrap/dist/css/bootstrap.css");
现在你可以运行:
yarn encore dev
和 Bootstrap CSS 应该在你的 app.css
.
编辑(2019-03-05):
这是 relevant block code of the screencast 7. Require CSS!?, part of the SymfonyCasts Course Webpack Encore: A Party for your Assets Tutorial.
更新 (2019-11-25):
此答案解释了如何使用 Node.js.[=29 提供的 require
函数绑定(导入)与 yarn
(或 npm
)一起安装的 CSS 库=]
ECMAScript 2015(也称为 ES6)引入了新功能 import
。
除了这两个选项之外,还有第三种方法可以导入 Bootstrap,即直接在您自己的 CSS 文件中使用 @import
:
@import 'bootstrap';
这仅在使用 Webpack 和 Yarn(或 NPM)时适用。 Webpack Encore 能够解析路径并导入相应的 CSS 文件。
基本上是 cezar 说的,但我把它放在一个答案中,因为评论会使它变得不可读:
import 'bootstrap'; // js-file
import 'bootstrap/dist/css/bootstrap.css'; // css file
不是 100% 确定,但我认为您还需要手动导入 Popper 和 Jquery:
import Popper from 'popper.js';
import $ from 'jquery';
根据文档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