In order not to import a lot of unused files into a static folder, I copy the necessary items using the Gulp script. I use the NPM script to invoke Gulp after installing the packages, so it does not affect my daily workflow.
package.json :
{ "dependencies": { "bootstrap": "^4.3.1", "gulp": "^4.0.2", "jquery": "^3.4.1" }, "scripts": { "install": "gulp" } }
gulpfile.js
const { series, src, dest } = require('gulp'); // Task 1: copy bootstap assets to /_vendor/ function bootstrap() { const files = [ 'node_modules/bootstrap/dist/css/bootstrap.min.css', 'node_modules/bootstrap/dist/js/bootstrap.min.js' ] return src(files).pipe(dest('_vendor')) } // Task 2: copy jquery assets to /_vendor/ function jquery() { const files = [ 'node_modules/jquery/dist/jquery.min.js' ] return src(files).pipe(dest('_vendor')) } exports.default = series(bootstrap, jquery)
my_project/settings.py :
STATICFILES_DIRS = [ str(BASE_DIR / '_vendor'),
Django Template:
{% load staticfiles %} <link rel="stylesheet" href="{% static 'bootstrap.min.css' %}" />
Bonus π
In Gulpfile, you can combine and minimize files. For example, here is my Gulpfile file upload library for Blueimp:
const uglify = require('gulp-uglify'); const concat = require('gulp-concat'); // Task 3: minify blueimp assets and save to /_vendor/ function blueimp() { const files = [ 'node_modules/blueimp-file-upload/js/vendor/jquery.ui.widget.js', 'node_modules/blueimp-file-upload/js/jquery.iframe-transport.js', 'node_modules/blueimp-file-upload/js/jquery.fileupload.js' ] return src(files).pipe(uglify()) .pipe(concat('jquery.fileupload.min.js')) .pipe(dest('_vendor/')) }
Benoit blanchon
source share