Ignores generated font files on start/watch #695
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #693
DESCRIPTION
This resolves (or attempts to resolves... test test test!) the issue of fonts disappearing ("randomly") when running
start
orwatch
. I was able to nail down the why – I'll copy my notes from the initial ticket below, but first I'll give the tldr; of everything.We only need to add one line to
clean-webpack-plugin
to resolve this. After adding this, you can still add new fonts while runningstart
orwatch
and they will end up in thebuild
directory, but if you remove fonts fromsrc/fonts
while runningstart
orwatch
, the removed font will remain in thebuild
directory – until you stop your process and run a freshnpm run build
.The Cause
I can replicate the font erasure issue if I add an image to the
src/images/
directory. Everything is fine if I add an SVG tosrc/images/svg
, but if I add an image to (or remove an image from) the top-level/images/
directory, some of the fonts disappear.On initial
npm run build
I wind up with my first screenshot above with basically duplicate font files – one named normally, then one with an string at the end.While
npm run start
is running and I add an image, the extra string fonts are removed and the regular string ones remain.I think this issue stems from Clean Webpack Plugin. We use that to clean our build directory so we don’t keep old/deleted files, and it works great and does its job. But, Tailwind wants to create font files itself and it does so with a string at the end like
/raleway-medium-webfont.06ef4a38.woff2
It also changes the src in the built CSS file to match
src: local("Raleway Medium"), url(fonts/raleway-medium-webfont.06ef4a38.woff2) format("woff2");
So when the Clean Webpack Plugin happens, it cleans our build directory (good), but Tailwind doesn’t regenerate its font files on
npm run start
(bad) which means we don’t get those fonts regenerated (bad) which means the built CSS file is now pointing to files that don’t exist (bad).The Solution
For like a very iffy workaround, you can do this
This will ignore anything in
build/fonts
and any*.woff2
files in the build directory (just to be safe, I guess). This will still add new fonts to your build directory if you add them to yoursrc
while runningnpm run start
, but it won’t remove anything frombuild
if you remove it fromsrc
and won't actually create the Tailwind versions of the files. It'll just do the basic CopyWebpackPlugin thing of copying the file.If you run a full
npm run build
, it seems like it generates all of the fonts that should be there (so far). Might not want to push your webpack config changes to a dev site or anywhere else, but may be a good workaround for locals since you shouldn’t really need to add too many new fonts mid-project anyway?Still going to look into this some more.
Additional Notes
So, the above addition to
CleanWebpackPlugin
works with the following caveats:npm run build
/src/fonts
directory while runningnpm run start
ornpm run watch
, nothing changes – fonts are not added or removed because of thecleanAfterEveryBuildPatterns
ignore ruleI'm not sure if this is necessarily a blocker for our workflow? As I mentioned above, you generally aren't adding new fonts halfway through a project so you shouldn't have to be swapping fonts in and out all the time while running
npm run start
or anything in your daily flow.Plus, knowing that the build directory does get cleaned and rebuilt (including
/build/fonts
means that fonts should always be doing what we expect them to be doing on lab/production sincenpm run build
doesn't seem to be confined by thecleanAfterEveryBuildPatterns
ignore rule.I'll see about getting a PR spun up for this later this morning, and this will need extensive testing, but hopefully this solves the disappearing font issue or at least leads us down a path to solving it.
SCREENSHOTS
Tailwind-generated fonts
OTHER
STEPS TO VERIFY
npm ci
/src/fonts
directory_fonts.scss
partial)npm run build
to see your fonts generated in thebuild
directorynpm run start
and work like normalnpm run start
(orwatch
), add an image to thesrc/images/
directory. Previously, this would remove the Tailwind-generated fonts from the/build/
directory and break things. This should no longer happen.start
orwatch
to see how everything reactsnpm run build
to see all of your fonts get built as expectedPer step 5 above, add something like this to your Tailwind config file (depending on the fonts you're adding, of course):
By adding the fonts in the Tailwind config, we're ensuring that Tailwind will build the fonts. This means we don't need to add a rule to CopyWebpackPlugin to copy fonts the way we're doing with images and SVGs – Tailwind will just handle it.
DOCUMENTATION
Will this pull request require updating the wd_s wiki?
Yes, we'll need documentation for sure.