-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to use the router.js with webpack? #264
Comments
+1 any idea for this ? |
the router.js file currently does not support AMD and CommonJS, meaning importing it directly won't work. you would have to create a shim exposing the global variable through CommonJS instead |
My solution inspired by this comment
Then I created file const {router} = require('../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js');
require('../../web/js/fos_js_routes');
module.exports = router; Then webpack magic starts: ..... encore config file .....
.autoProvideVariables({
"Routing": "router"
})
.addLoader({
test: /jsrouting-bundle\/Resources\/public\/js\/router.js$/,
loader: "exports-loader?router=window.Routing"
})
let config = Encore.getWebpackConfig();
config.resolve.alias = {
'router': __dirname + '/assets/js/router.js'
}; So, when you need Access to Router in any place, just type p.s. 21.09.2017 some updates was done. Removed unnesesary code from webpack config. |
Hi @Kolbasyatin, your solution looks clean but I have an error with it: Thanks. |
@davidesigner it seems like something wrong with your path to files. Check your relative path here (in my case const {router} = require('../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js'); and also check path in loader. Anyway, if file >fos
{Router: ƒ}Router: ƒ K(a, c)g: ƒ ()getInstance: ƒ ()j: K {a: {…}, d: s}setData: ƒ (a)arguments: nullcaller: nulllength: 2name: "K"prototype: {h: ƒ, o: ƒ, k: ƒ, n: ƒ, l: ƒ, …}__proto__: ƒ ()[[FunctionLocation]]: router.js:8[[Scopes]]: Scopes[3]__proto__: Object
.... If no, type your code here and we found solution. |
I had an even trickier problem as I had to expose some routes and base path to webpack config itself before any of the loaders are run so stuff like
And from there it's easy to use it in webpack config:
Note that for this to work you cannot have your webpack config files written in es6 or named *.babel.js, which automatically parses them with For completeness, a separate file for es6 browser code imports:
For this one I simply used |
this is now much simpler since 2.2: symfony/webpack-encore#97 (comment) |
this will throw an error
The text was updated successfully, but these errors were encountered: