Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack Asset Modules support #182

Merged
merged 1 commit into from Nov 16, 2022
Merged

Webpack Asset Modules support #182

merged 1 commit into from Nov 16, 2022

Conversation

colingourlay
Copy link
Contributor

We've been running into problems when porting older stories where some images have been exported as static assets, but contain the text of a JS module with an import value of the original image page. These appear in our output as little white squares (in the case of PNGs). @phocks discovered that it is a side-effect of us using some deprecated asset loaders in the Webpack 5 environment.

Webpack 5 now has built-in support for loading assets which previously required the use of file-loader, url-loader and raw-loader. I've updated the internal Webpack config rules to leverage this feature, and it's solved the issues we were seeing (and is probably more future-proof anyway).

Webpack allows the definition of either asset/resource and asset/inline types to decide whether assets are output as files (with hashed names) or inlined into the JS/CSS (as data-uris). We previously output all but images as resouces and split images between the two strategies on a 10kb threshold. Webpack takes care of this entire thing by giving us the asset type, and automatically selects based on an 8kb threshold (which I think is acceptable).

We were also using the html-loader package to import .html files as raw text. This is now handled by the new webpack asset/source type, so html-loader has also been removed. The ability to import other arbitrary file types as strings has also been enabled (by adding ?raw to your import path). SVG files can also now opt-into raw loading, rather than being handled as an asset (we sometimes like to use them as innerHTML).

- This replaces the deprecated file-loader and url-loader packages
- Using type=asset allows automatic inline (data-uri) bundline for files under 8kb
- This applies to all images, audios, videos and fonts
- Enable ?raw option for importing files as strings (including SVG)
- HTML files will be imported as strings instead of using html-loader (which has also been removed)
Copy link
Member

@phocks phocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Simple and effective!

@drzax
Copy link
Member

drzax commented Nov 16, 2022

This looks good to me, but I wanted to clarify whether it's a (potentially) breaking change for older projects updating aunty?

@phocks
Copy link
Member

phocks commented Nov 16, 2022

@drzax For me, older projects are already breaking whether you update aunty or not. All the older projects I've been working on won't even install the old version of aunty without throwing a million npm errors. So hopefully this is a fixing change and not a breaking change :)

@colingourlay
Copy link
Contributor Author

@drzax it shouldn't be a breaking change. The only things that have been removed (rather than modernised) are html-loader and the version numbers (appended to SVG and font assets) in the pattern matches.

html-loader was an enhanced raw-loader which did some minifaction (including attribute quote stripping), which we've never relied on, and TBH, probably shouldn't. The version number stuff was ancient Webpack voodoo from when cache staleness workarounds had to be partially implemented with import paths.

Both of those features were historically ported directly from @abcnews/webpacker when aunty implemented Webpack config internally instead of using that dependency.

@colingourlay colingourlay merged commit 639b133 into main Nov 16, 2022
@colingourlay colingourlay deleted the webpack-asset-modules branch November 16, 2022 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants