I use fos_js_routing bundle on symfony4 . I need to get the Routing object reachable in my twig view. I defined Routing in assets/js/app.js, a transpiled js file with Webpack Encore. Because my Routing object is correctly built in this file, I want to access it in a Twig view.
// assets/js/app.js
const routes = require('../../web/js/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
Routing.setRoutingData(routes);
I got this line in my webpack config:
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
.addEntry('js/app', './assets/js/app.js')
I get my twig view, where the transpiled app.js is reachable and succesfully transpiled by webpack Encore. But the variable Routing is not reachable ( I got client error : Routing is not defined),
// in my twig view, inside a script block
var redirectionUrl = Routing.generate('my_route', {arg: arg}); // Routing is not defined
probably because is defined as let in the transpilation process, and I want it as a var, to be reachable in each twig view where I include transpiled app.js ( public/build/js/app.js ). My Twig view is including the transpiled file in this line, and succesfully imported in my twig sources
<script src="{{ asset('build/js/app.js') }}"></script>
How can I get Routing in my twig view using Webpack encore ?
'../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js'contains something likeexports.default = somethingor that you are using an ES module interop aware loader or bundler like Webpack or SystemJS and or a properly configured transpiler such as bable or typescript - Aluan Haddad