У меня проблема с порядком загрузки скриптов в symfony 5 / webpack encore.
Скрипты не загружаются в правильном порядке ...
Например, я сделали этот простой тест: два документа готовы к работе с Jquery версия:
$(document).ready(function() {
alert($().jquery);
});
один в моем приложении. js вызов в моей базе. html .twig (сначала вызовите нормально), а другой на моей странице.
Когда вы запускаете страницу, версия оповещения на моей странице отображается сначала с «неопределенной версией», а затем оповещение в приложении. js работает правильно.
Обычно приложение. js - это вызов первым, поэтому я не понимаю, почему второй документ готов, это вызов первым.
спасибо за вашу помощь!
Приложение. 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 import will output into a single css file (app.css in this case)
import '../css/app.css';
// Need jQuery? Install it with "yarn add jquery", then uncomment to import it.
import $ from 'jquery';
// uncomment if you have legacy code that needs global variables
// global.$ = $;
import 'bootstrap';
require('../css/global.scss');
$(document).ready(function() {
alert($().jquery);
console.log('Version')
});
Webpack config
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
// this is your *true* public path
.setPublicPath('/athena/build')
// this is now needed so that your manifest.json keys are still `build/foo.js`
// (which is a file that's used by Symfony's `asset()` function)
.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')
//Datagrid
.addEntry('jqwidgetsDataGrid', './assets/js/jqwidgetsDataGrid.js')
//.addEntry('page2', './assets/js/page2.js')
// 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()
// .disableSingleRuntimeChunk()
/*
* 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())
.enableVersioning()
// enables @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
// enables Sass/SCSS support
.enableSassLoader()
// Intégration des fichiers et images statiques
// .copyFiles({
// from: './assets/md',
// to: './md/[name].[ext]'
// })
.copyFiles({
from: './assets/images',
to: 'images/[path][name].[hash:8].[ext]'
})
//
// Transformation du css pour l'ensemble des navigateurs
// .enablePostCssLoader((options) => {
// options.config = {
// path: 'config/postcss.config.js'
// };
// })
// 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();
base. html .twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Athena{% endblock %}</title>
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
</head>
<body>
{# Zone menu #}
{% include("partials/navbar.html.twig") %}
{# Corps de la page #}
<div class="mx-3">
{% block body %}{% endblock %}
</div>
{# Javascript #}
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
</body>
</html>
Моя страница:
{% extends 'base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
{{ encore_entry_link_tags('jqwidgetsDataGrid') }}
{% endblock %}
{% block title %}Permissions de l'application{% endblock %}
{# Ajout des Popups et modals #}
{% use 'permissions/modalform.html.twig' with global as addPerm_global %}
{% block body %}
<div class="spacer-orange rounded-top mt-3"></div>
<div class="container-fluid parker-fond-page rounded-bottom ">
<h1>Liste des Permissions</h1>
{{ dump(permissions) }}
{% for permission in permissions %}
<H2>{{ permission.Name }}</H2>
{% endfor %}
</div>
<div class="spacer-orange rounded-top mt-3"></div>
<div class="container-fluid parker-fond-page rounded-bottom ">
<div id="jqxgrid">
</div>
</div>
{# Ajout modals #}
{% set form_id = "formAjoutPerm" %}
{% set modal_id = "modalAjoutPerm" %}
{% set modal_title = "Ajout d'une permission" %}
{% set form_action = path('permissions_ajaxadd') %}
{% set namespace = "addForm" %}
{{ block('addPerm_global') }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('jqwidgetsDataGrid') }}
<script type="text/javascript">
$(document).ready(function () {
alert($().jquery);
// show first but doesn't work ????
}); //fin documentready function
</script>
{% endblock %}