-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Incorrect paths for fonts #88
Comments
Hey @nikrou! Hmm. Could you post your Cheers! |
my config file: var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('./build/')
.setPublicPath('/themes/default/build')
.cleanupOutputBeforeBuild()
.addEntry('js/index', './src/js/index.js')
.addStyleEntry('css/style', './src/scss/style.scss')
.enableSassLoader()
.autoProvidejQuery()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
;
// export the final configuration
module.exports = Encore.getWebpackConfig(); I used manifest.json: {
"themes/default/build/css/style.css": "/themes/default/build/css/style.css",
"themes/default/build/fonts/fontawesome-webfont.eot?v=4.7.0": "/themes/default/build/fonts/fontawesome-webfont.eot",
"themes/default/build/fonts/fontawesome-webfont.ttf?v=4.7.0": "/themes/default/build/fonts/fontawesome-webfont.ttf",
"themes/default/build/fonts/fontawesome-webfont.woff2?v=4.7.0": "/themes/default/build/fonts/fontawesome-webfont.woff2",
"themes/default/build/fonts/fontawesome-webfont.woff?v=4.7.0": "/themes/default/build/fonts/fontawesome-webfont.woff",
"themes/default/build/images/fontawesome-webfont.svg?v=4.7.0": "/themes/default/build/images/fontawesome-webfont.svg",
"themes/default/build/js/index.js": "/themes/default/build/js/index.js"
} And I generate the link: <link rel="stylesheet" href="/my-path/themes/default/build/css/style.css"> The main problem is that my style.css file contains absolute link to font awesome files: @font-face {
font-family: 'FontAwesome';
src: url(/themes/default/build/fonts/fontawesome-webfont.eot);
src: url(/themes/default/build/fonts/fontawesome-webfont.eot?#iefix&v=4.7.0) format("embedded-opentype"), url(/themes/default/build/fonts/fontawesome-webfont.woff2) format("woff2"), url(/themes/default/build/fonts/fontawesome-webfont.woff) format("woff"), url(/themes/default/build/fonts/fontawesome-webfont.ttf) format("truetype"), url(/themes/default/build/images/fontawesome-webfont.svg#fontawesomeregular) format("svg");
font-weight: normal;
font-style: normal;
} I read documentation about setManifestPrefix method but it only changes manifest.json. It's not related with my issue. |
I wish I could have something like that in my style.css file: @font-face {
font-family: 'FontAwesome';
src: url(../fonts/fontawesome-webfont.eot);
src: url(../fonts/fontawesome-webfont.eot?#iefix&v=4.7.0) format("embedded-opentype"), url(../fonts/fontawesome-webfont.woff2) format("woff2"), url(../fonts/fontawesome-webfont.woff) format("woff"), url(../fonts/fontawesome-webfont.ttf) format("truetype"), url(./build/images/fontawesome-webfont.svg#fontawesomeregular) format("svg");
font-weight: normal;
font-style: normal;
} That css part can be generated with the following file-loader config: {
test: /\.(woff|woff2|ttf|eot|otf)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]',
publicPath: '../'
}
} But webpack-encore use something like that: {
test: /\.(woff|woff2|ttf|eot|otf)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]',
publicPath: '/themes/default/build/'
}
} I don't find a way to override webpack-encore default configuration for module rules. |
Thanks for the details! Try this: var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('./build/')
.setPublicPath('/my-path/themes/default/build')
.setManifestKeyPrefix('themes/default/build')
// ... everything else the same
;
// export the final configuration
module.exports = Encore.getWebpackConfig(); The issue is that your This should fix your problem - it will dump something like this: @font-face {
font-family: 'FontAwesome';
src: url(/my-path/themes/default/build/fonts/fontawesome-webfont.eot);
src: url(/my-path/themes/default/build/fonts/fontawesome-webfont.eot?#iefix&v=4.7.0) format("embedded-opentype"), url(/my-path/themes/default/build/fonts/fontawesome-webfont.woff2) format("woff2"), url(/my-path/themes/default/build/fonts/fontawesome-webfont.woff) format("woff"), url(/my-path/themes/default/build/fonts/fontawesome-webfont.ttf) format("truetype"), url(/my-path/themes/default/build/images/fontawesome-webfont.svg#fontawesomeregular) format("svg");
font-weight: normal;
font-style: normal;
} It shouldn't matter to us whether these paths are absolute or relative - so I wouldn't worry about that detail. Webpack just makes sure that these paths are correct - as long as you give it the correct public path :). Let me know if this fixes things! |
@weaverryan My idea is to be able to override rules in method() buildRulesConfig from lib/config-generator.js. The array rules is hard coded and can not be override. |
Hmm, interesting... I wonder if we could update Encore to use relative paths internally. I mean, logically, it makes sense to output a CSS file with a relative path to the fonts - webpack knows where both are placed. Here's an issue that's somewhat related: webpack-contrib/extract-text-webpack-plugin#27 I would need to do some playing... Until then, the hack would be to change this key manually. It would look something like this: // webpack.config.js
// ...
var config = Encore.getWebpackConfig();
config.module.rules[3].options.publicPath = '../'
module.exports = config; The |
Thanks @weaverryan. It's a bit of hack. But instead of something more decent... |
A PR would be cool :). I don't know yet if we should do this approach, or something more "generic" (e.g. a "hook" to edit the config for any rule), but a PR would let us review. And I'm still curious if there's a way to always make the paths relative - that'll take more research. For the PR - make sure that the fonts and images public paths default to the normal |
@weaverryan |
Any movement on this? We run multiple Symfony installs under sub-directories e.g: http://example.org/site-1/web/app_dev.php We're finding it hard to use Encore with this set up for the reasons discussed above. I added the following to my webpack.config.js:
This fixes the issue with paths referenced in code but the manifest.json still starts each file with |
Wouldn't using the |
I read the docs at http://symfony.com/doc/current/frontend/encore/faq.html#my-app-lives-under-a-subdirectory and it suggests using In our project, Is there any way around this? The only way I can think of is to use an environment variable and set the public path to the value of it, but the problem with that is we'd need to set an environment variable for every one of our projects. Setting the public path conditionally based on @weaverryan I'm aware that although related, this situation is not exactly on-topic with the original issue, let me know if you want me to open a new one. |
Sorry for the noise on this one but I just figured out a solution to the problem I described above using Symfony's Json manifest versioning strategy. After retrieving the versioned path from the manifest, Symfony's In practical terms, say for example we have a Symfony app hosted under Encore
.setOutputPath('web/build')
.setPublicPath('/build') We run {
"build/css/style.css": "/build/css/style.css"
} In a twig template, a call to Removing the '/' from the beginning of the publicPath forces Symfony to add the correct base path so our call to Passing a non-absolute or non '/' prefixed value to setPublicPath currently throws an error: webpack-encore/lib/WebpackConfig.js Lines 122 to 127 in 7997206
To get around this I'm using the configureManifestPlugin method like so: .configureManifestPlugin(function (options) {
if(options.publicPath.indexOf('/') === 0) {
options.publicPath = options.publicPath.substr(1);
}
return options;
}) This only fixes the issue when using Symfony and its manifest based versioning strategy so I'm not sure what the implications of removing the thrown error in setPublicPath would be to other non-Symfony apps. |
I tried setting the output path to Edit: I tried using |
this solve my issues too , thanks @weaverryan |
Maybe a solution to be independant of the URL of you site and if your application is a subdirectoy. I can run the symfony 5.x demo with webpack ( the awefont are loaded) In your weebpack.config.js file replace
By:
|
Thank you for this issue. |
My context:
I host my webiste under a given path. I do not use symfony but we don't care here.
I used (of course) webpack-encore to manage my assets. It works well except for fonts because of my extra path.
My website is hosted under /my-path. I generate my stylesheets using sass. Stylesheets paths are composed of my base path and the path found in manifest.json.
I've got for example /my-path/build/css/style.css
I use font-awesome to add web-fonts. The url() in the generated stylesheets is url(/build/fonts/fontawesome-webfont.eot); and so one.
I don't know (just start to investigate) how to fix it. I usually used webpack (without webpack-encore) and webpack used a relative path to fonts from stylesheets instead of an absolute path; something like:
And I've got
Any idea to solve that issue is appreciate. If you have any question, don't hesitate.
The text was updated successfully, but these errors were encountered: