根据文档webpack encore 中的引导程序 https://symfony.com/doc/current/frontend/encore/bootstrap.html
在 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