I have project on Symfony 3 with installed assetic. I am using it for merging all css together and minify.
Usage in twig template:
{% stylesheets filter='cssrewrite' filter='uglifycss'
'assets/font-awesome-4.6.3/css/font-awesome.css'
...
%}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}
Config:
filters:
cssrewrite: ~
uglifyjs2:
bin: "%kernel.root_dir%/Resources/node_modules/uglify-js/bin/uglifyjs"
uglifycss:
bin: "%kernel.root_dir%/Resources/node_modules/uglifycss/uglifycss"
In prod mode all works as expected. Assetic loads css files from path /assets/someDir/some.css and generated minified file (css or js) is in default assetic path /css/allCss.css or /js/allJs.css.
But in develop mode assetic generate new files from /assets/someDir/some.css to /css/some.css (each file and not minified) and load css files from there.
QUESTION:
How i can load original files from /assets/someDir/some.css in dev mode?
Because in this situation i must after each change in any css or js run php bin/console assetic:dump --env=dev and it is unacceptable.
Since you don't want to run php bin/console assetic:dump each time you change a CSS or a JS file, use php bin/console assetic:watch instead, it will automatically regenerate the files on every change of CSS or JS.
Related
I'm crating application in angular with SSR
How do I apply <link rel="preload" href="/styles.css" as="style" /> to head section or Link header in order to preload/prefetch whole styles.css file?
Using plain styles.css is not enoug as production build generate file that contains random characters like styles.7541caaaab536370.css (different in every build)
Googling about preloading and prefetching in angular only results in preloading components/modules and I'm out of ideas
You can include the styles.css file in the assets folder. This will prevent the angular build process from "fingerprinting" the file and the resulting build file will simply be named styles.css allowing you to reference it directly from your index.html.
I'm working with Symfony 4 and my deployment platform is Microsoft Azure. This is my website :https://synergie-2i.azurewebsites.net/SynergieInformatique/public/index.php/.
My problem is when my environment is in production, my css and js aren't loaded. When inspect my page in services i see my folder SynergieInformatique/public and an other folder name build. Inside there is 3 css files but there is nothing inside. I think webpack didn't write my css in these files.
I have try to copy my css and put in the empty css file and it works but i don't know how make this with symfony.
When i'm in dev environement, the folder build is in SynergieInformatique/public and my css is loaded.
###webpack.config.js : ###
var Encore = require('#symfony/webpack-encore');
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/app.js')
.addEntry('owlcarouselcss','./node_modules/owl.carousel/dist/assets/owl.carousel.min.css')
.addEntry('owlcarouseljs','./node_modules/owl.carousel/dist/owl.carousel.min.js')
.splitEntryChunks()
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.configureBabel(() => {}, {
useBuiltIns: 'usage',
corejs: 3
})
.enableSassLoader()
.autoProvideVariables({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
});
module.exports = Encore.getWebpackConfig();`
What are the step for deploy an symfony 4 application with Webpack and how i can have my css and js load on production environement ?
If you need more informations ask me :)
I have found, my links were bad. I use {{ encore_entry_link_tags('app') }} and this doesn't search at the good folder so i remplace this by <link rel="stylesheet" href="/SynergieInformatique/public//build/app.css"> and it works.
I generated new bundle and I have styles.css file in following pathsrc/AdminBundle/Resource/public/styles.css. In my base.html.twig file I have link (templates/head.html.twig)
<link href="{{ asset('bundles/adminbundle/css/styles.css') }}" rel="stylesheet" type="text/css" />
But this giving error not found file. Please help.
Files from src/AdminBundle/Resource/public/styles.css are not copied automaticlly to web/bundles
You need to to run command
php app/console assets:install
I tried to look for some info related to bundling, tried different scenario with the same result: doesn't work!
Here is the project structure:
..........
Content
css
style.css
fonts
images
.........
Scripts
Plugin1
src
css
style1.css
js
js1.js
Plugin2
src
css
style2.css
js
js2.js
Plugin3
src
css
style1.css
js
js2.js
somejs.js
someotherjs.js
case 1 - works
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/css/style.css"));
case 2 - doesn't work
bundles.Add(new StyleBundle("~/js/style").Include(
"~/Scripts/Plugin1/src/css/style1.css",
"~/Scripts/Plugin2/src/css/style2.css",
"~/Scripts/Plugin3/src/css/style3.css"));
My question is, how can I bundle my css from different folders in one single bundle? I know that if the styles are all in the same folder then will work.
Does this have to do anything with the path itself? Does the name of the bundle ... new StyleBundle("~/js/style").. must match the actual location of the css files (folder structure)?
When the page loads in the page source there is missing the bundle key for the one that doesn't work while for the other one is present:
<link href="/js/style?v=" rel="stylesheet"/>
<link href="/Content/css?v=z_NN8-Oubjqwg7jFATA50T7SzahDLay9Fi3Lti_eSJ81" rel="stylesheet"/>
I am using Assetic with Twig but not the symfony2 framework.
Here's how the project is set up
/site
/template
/css
/public_html
/css
The raw css is stored under /site/template/css and I want assetic to minify the css and output it to /public_html/css.
Here's how assetic is set up as a twig extension
$factory = new AssetFactory(//absolute path to `/site/template/`);
$factory->setDefaultOutput(//absolute path to `/public_html/`);
$factory->setDebug(false);
$twig->addExtension(new AsseticExtension($factory));
Then in my template:
{% stylesheets 'css/screen.css' output='css/*' %}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}
I can see that assetic has generated a unique url in the final output:
<link href="css/00da241.css" type="text/css" rel="stylesheet" />
However, if I look in /public_html/css, the files are never generated!
I am using a Windows 7 server with apache and PHP has no issues writing files anywhere.
What could be causing this?
What you've done here is asking assetic to generate URL's to assets (using twig).
But assets urls don't point to any file yet. You still have to generate them, using a dump script:
<?php
use Assetic\AssetWriter;
use Assetic\Extension\Twig\TwigFormulaLoader;
use Assetic\Extension\Twig\TwigResource;
use Assetic\Factory\LazyAssetManager;
$am = new LazyAssetManager($factory);
// enable loading assets from twig templates
$am->setLoader('twig', new TwigFormulaLoader($twig));
// loop through all your templates
foreach ($templates as $template) {
$resource = new TwigResource($twigLoader, $template);
$am->addResource($resource, 'twig');
}
$writer = new AssetWriter('/path/to/web');
$writer->writeManagerAssets($am);
This script uses twig templates to define which files to dump.
You will have to define the $templates variable and the $twig variables of course.