Proposal to Add Djlint and Prettier formatter#2151
Proposal to Add Djlint and Prettier formatter#2151
Conversation
There was a problem hiding this comment.
Nice work @julhoang!! I think this is absolutely necessary and will improve our daily work by a lot.
Just a couple of things:
- When I ran
yarn installwhile on your branch, myyarn.lockgot updated, but those changes are not included in your branch. Would you please make sure that your lockfile doesn't have any pending changes that should be included in the PR? Kinda weird though, as you do have updates on your lockfile, so I'm not sure why we're seeing differences. - Nice touch adding the formatter as a pre-commit hook! I think it'd be nice too if we were able to manually trigger the linting, as I'd much rather have a local keystroke to apply the formatting rather than waiting until the hook process it. A
yarn add djlintto the project might be enough, and runningdjlint . --reformatis the command I used locally. Something curious is that running this command also touched a few files incore/tests/contentthat are not in your PR. - A while back I was looking into ways to solve a common issue when applying these large lint updates, which is that after linting this many files,
git blamepoints to you as the author of all of those files/lines of code, and that's not really what we want. Check below a way to solve this and see if makes sense to you!
Solving the Git Blame issue when committing several lint fixes
Since 2019, Git has a built-in feature specifically for this! You can tell Git to completely ignore "style-only" commits when someone runs a blame.
Step 1: Create the ignore file
After you commit massive linting changes, get the commit hash of that change (in your case, it'll be from multiple commits). Create a file in the root of the repo namef .git-blame-ignore-revs and paste the hashes there (just a hash example there):
# Avoid blaming the commit author for the massive lint/reformat
# (Add the full 40-character SHA-1 hash)
c1891b8ffb74c87ebd11bc42e03f86e6384fd5d5
Step 2: Configure Git to use it
Run this command:
git config blame.ignoreRevsFile .git-blame-ignore-revsStep 3: Share it with the team
You'll have to commit the .git-blame-ignore-revs file, and everyone in the team can run the same command in step 2 on their end. There's no harm if they don't run it, it's totally optional.. But they'll keep seeing you as the author of those lines of code until they run it.
Caveat
Git is "blind" to the context of your changes, it doesn't know whether a change is a lint or an actual logic change, so these kinds of changes must live in completely separate commits so you don't mess with the correct authoring of your own actual logic changes (i.e. adding some new dependency to package.json).
Thanks a ton for this!
c1891b8 to
5e923d5
Compare
|
Thanks a ton for all of your suggestions @herzog0 !! The |
herzog0
left a comment
There was a problem hiding this comment.
This is looking just perfect @julhoang, thanks so much! I tested and the ignoring is working flawless. The other commands are working and they provide a great dev experience, thanks for that too!
I'm just selecting "Request changes" because for some reason yarn format:check exists with an error:
Buut, what's weird is that yarn format doesn't actually format anything.. 🤔 sorry I didn't have the time to go deeper into why this is happening
|
Thanks for raising that @herzog0 – I notice that the linter is parsing through 1093 files, which is a lot more than the number on my end (I think around max 200 files). Also another clue is that |
|
Hmm sorry @julhoang, didn't even notice that file wasn't part of the project! It is however part of Python's virtual environment created for the project. Are you running these yarn commands from the root of project too? Maybe it's a matter of inclusion/exclusion settings from djlint? Like, I see that the pre-commit hook looks at files in paths like Even if that's the case, we need a folder/file exclusion list, maybe in a separate file if djlint allows or inline in the package.json command. I'm assuming you don't have a |
8477082 to
09313f8
Compare
|
Hey @julhoang quick feedback on this: now the |
3270c64 to
af029dd
Compare
af029dd to
ef08651
Compare
All changes requests have been resolved ✅
gregjkal
left a comment
There was a problem hiding this comment.
A couple of minor requests in the GHA, and one real bug created by djlint.
a944dc4 to
2438c53
Compare
gregjkal
left a comment
There was a problem hiding this comment.
Thanks for making those changes - looks good!
Move third-party and generated JS bundles into static/js/vendor/ so that only custom project JS lives at static/js/*.js. Moved files: - highlight.js → vendor/highlight/ - marked.js + source map → vendor/marked/ - wysiwyg-editor.js → vendor/wysiwyg-editor/ - boost-gecko → vendor/boost-gecko/
Wrap the multi-line Alpine.js x-data attribute with djlint:off/on comments to prevent djlint from mangling the JavaScript object.
Add Prettier for JS/CSS and djlint for Django HTML template formatting. Configure pre-commit hooks, CI checks, and justfile commands. Scope djlint to templates/ directories and Prettier to static/js/*.js to avoid scanning vendor and virtual environment files.
b0e4e3e to
b40227a
Compare
Add Django HTML template formatter (djlint) and frontend formatter (Prettier)
Summary
The codebase had no automated formatting or linting for HTML templates, JS, or CSS. This made allowed inconsistent style to accumulate over time. This PR establishes a consistent baseline by introducing Prettier (JS/CSS) and djLint (HTML templates) with
pre-commitintegration and CI enforcement.Changes
.prettierrcand.prettierignorefor JS and CSS files. Vendor/generated bundles instatic/js/vendor/are excluded.static/js/intostatic/js/vendor/subfoldershighlight,marked,wysiwyg-editor,boost-gecko.static/js/*.jsas the convention for custom project code only.pyproject.tomlfor alltemplatedirectories (templates/,users/templates/,reports/templates/) — includes auto-reformat and lint hooks..pre-commit-config.yaml— replaces DjHTML with djLint for Django template formatting and linting..github/workflows/actions.yml— now installsrequirements-dev.txtand runsyarn format:check+pre-commit run -a.justfileandpackage.jsoncommands for local use (just format,just format-check, etc.)git-blame-ignoresolution (credits to @herzog0), these files are reformatted but the git history is preserved.Known djLint Quirks
README.mdWhat Developers Should Expect
Existing developers — after pulling, reinstall dependencies to pick up both tools:
Key commands for your regular usage:
<pre>blocks, inline elements).<pre>blocks and no rendering changes observed.@clickhandlers.static/js/vendor/move updates paths referenced via{% static %}anddynamic
import(). If any path was missed, it would be a runtime 404 (not a build error).yarn.lock(fromyarn) andpackage-lock.json(fromnpm) in the repo – I only ranyarn installsince it seems to be the latest preference as per the project set-up documentation. Ifpackage-lock.jsonis truly a legacy artifact, we should consider removing it.Djlint Rules we're currently ignoring
The following djlint rules are not yet enforced (existing violations need cleanup first). They're listed in
pyproject.tomlunderignore:djlint Ignored Codes & Descriptions (open for details!)
{% url %}tag<html>tag should have a lang attribute<img>tag should have height and width attributes<img>tag should have an alt attribute (accessibility)<title>tag in HTML<script>tags{% endblock body %}){% url %}instead of hardcoded path{{ foo | bar }}(spaces around pipe)