![]() ![]() The object constructor accepts 0, 1 or multiple paths of files, or even complete CSS/JS content, that should be minified.Īll CSS/JS passed along, will be combined into 1 minified file. or just output the content echo $minifier-> minify() JS // just look at the CSS example it's exactly the same, but with the JS class & JS files :) MethodsĪvailable methods, for both CSS & JS minifier, are: _construct(/* overload paths */) save minified file to disk $minifiedPath = '/path/to/minified/css/file.css' or we can just add plain CSS $css = 'body ' we can even add another file, they'll then be // joined in 1 output file $sourcePath2 = '/path/to/second/source/css/file.css' $minifier = new Minify\ CSS( $sourcePath) $sourcePath = '/path/to/source/css/file.css' statements and small assets in CSS files), and optimizes/shortens a few common programming patterns, such as:Īnd it comes with a huge test suite. If you run into this, whitenoise is a very nice library that will serve your static files through Django.Removes whitespace, strips comments, combines files (incl. ![]() It provides you with the template tag compress, which you use like this: ĭeploying static files to production can be a bit of a hassle. It can combine JS and CSS files (respectively, of course, we’re not monsters!), and can also process SASS/LESS markup while it’s doing so. Compressionĭjango-compressor is pretty much what you are looking for in most cases, I think. On the other hand, I have no dependency on Node.js at all, which is especially nice when your project is used by many other people, and there’s no need to learn how to write Webpack configs (which can have an infinite degree of complexity). This is a considerable disadvantage, to my mind. I don’t get the luxury of automatic updates, by pinning versions etc. This has a very noticeable number of drawbacks: I have to do updates the same way, by copying files in manually. Typically I put both the minified and the non-minified file there, and decide depending on settings.DEBUG which version to serve.You can see one example for this setup here. I put the vendored files there by hand (most projects offer a finished bundled version for this purpose, often from a CDN for ready use). All of these will have subdirectories called js, css, and possibly assets (for images etc). Additionally I’ll have a vendored directory for all JavaScript/CSS dependencies. Within my /static/ directory, I’ll have subdirectories by app, if that makes sense for your projects (ie if you actually have and use mutiple apps), or just one directory with my project’s name. Frontend-heavy projects often use a setup as Leigh described, and I just want to add a version that I often use in smaller/less frontend-heavy projects: Structure If you don’t need that complexity, there are other solutions that can be installed with Pip (or poetry or pipenv) and integrated directly into Django’s static asset management is definitely right that there is no one blessed approach, and there is a wild variety of ways to handle static files out there. The Webpack approach is more or less infinitely flexible, but also rather complex (configuring Webpack is something you could write an entire book on, and it requires you to have Node.js available at build time). The webpack config we use for some things at work is on GitHub here, but I don’t really get loads of time to make these sorts of things consumable by people outside the company, so I wouldn’t use it wholesale. ![]() We then use django-webpack-loader to actually load the entry points into our templates, so that Django can find the right hashed file name. To my knowledge at least, there isn’t a blessed One True Way to manage static files in Django, but rather lots of different ways with different advantages and drawbacks.Īt my work, we use Webpack (and an assortment of plugins) to build TypeScript and Sass into JS and CSS, bundle it, minify it, and add cache-busting hashes to the file names. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |