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

Content hash output file names #1025

Merged
merged 5 commits into from
Mar 21, 2018
Merged

Content hash output file names #1025

merged 5 commits into from
Mar 21, 2018

Conversation

devongovett
Copy link
Member

@devongovett devongovett commented Mar 19, 2018

This implements the strategy described in #872. In production mode, a hash of bundle content is appended to names of files that aren't entry points (e.g. HTML files, things linked to a <a href>, service workers, etc.). Unhashed files retain their original name, and path relative to the main entry point.

In development mode, rather than using content hashes, the hash of the filename is used as before, so that filenames don't change during development. The hashes are necessary at all to avoid name collisions since the file structure is flattened. In addition, the filenames look more similar to the real production ones - the hashes just don't change.

It works by using a temporary filename (md5 of asset path) as references (which should be unique), and at the end, once file contents is known for all bundles, it replaces those temporary names with the real content-hashed names during packaging.

To do:

  • fix hashing of raw assets (which aren't in memory)
  • more unit tests
  • test in real world apps.

Please help me test! cc. @zeakd @songlipeng2003 @ssuman @Munter @benhutton @shanebo @leeching @gamebox @npup

@devongovett devongovett added this to the v1.7.0 milestone Mar 19, 2018
@davidnagli davidnagli added the 📝 WIP Work In Progress label Mar 19, 2018
@devongovett devongovett changed the title [WIP] Content hash output file names Content hash output file names Mar 20, 2018
@devongovett devongovett removed the 📝 WIP Work In Progress label Mar 20, 2018
@codecov-io
Copy link

codecov-io commented Mar 20, 2018

Codecov Report

Merging #1025 into master will decrease coverage by 0.04%.
The diff coverage is 92.68%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1025      +/-   ##
==========================================
- Coverage   89.27%   89.23%   -0.05%     
==========================================
  Files          69       70       +1     
  Lines        3393     3631     +238     
==========================================
+ Hits         3029     3240     +211     
- Misses        364      391      +27
Impacted Files Coverage Δ
src/packagers/HTMLPackager.js 97.5% <100%> (ø) ⬆️
src/utils/md5.js 100% <100%> (ø) ⬆️
src/Bundle.js 99.17% <100%> (+0.17%) ⬆️
src/packagers/JSPackager.js 90% <100%> (ø) ⬆️
src/Asset.js 100% <100%> (ø) ⬆️
src/assets/RawAsset.js 94.73% <100%> (+1.87%) ⬆️
src/Bundler.js 89.96% <100%> (+0.03%) ⬆️
src/Server.js 92.59% <100%> (+0.13%) ⬆️
src/packagers/CSSPackager.js 100% <100%> (ø) ⬆️
src/packagers/Packager.js 92.3% <100%> (+4.8%) ⬆️
... and 27 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7f9c64...219ad7f. Read the comment docs.

@jamiebuilds
Copy link
Member

This is a really nice way to do it, we should make a chart of the implications of the different ways to import resources:

Language Syntax Dynamic (possibly code split) Public URL (keeps name)
JavaScript import 'x'
JavaScript import('x')
JavaScript SW.register('x')
HTML <iframe src="x"
HTML <a href="x">
CSS @import "x"

@devongovett
Copy link
Member Author

iframes do not currently retain the filename (they are hashed) since they aren't linked to directly. do you think we should keep the name?

@jamiebuilds
Copy link
Member

Hmm... Yeah, I think they should be. I would treat them as public resources that could be linked to elsewhere and not partials

@jamiebuilds
Copy link
Member

🎉

@TimNZ
Copy link

TimNZ commented Mar 28, 2018

LOVING the new filenames.
Along with simplifying cache busting deployment will make debugging easier.
Thanks!

@rogeriochaves
Copy link

is there any way to opt out of hashing at all? I'm currently building a browser extension, and I have to explicitly list every js I'll need to load on the manifest.json file, so I can't have filenames changing all the time

danr pushed a commit to spraakbanken/swell-editor that referenced this pull request Apr 5, 2018
Includes the long-waited "Content hash output file names"
parcel-bundler/parcel#1025
@TheAlexPorter
Copy link

I'm doing the same thing as @rogeriochaves . It would be nice to be able to turn off the hashing so that tools, like Chrome Extensions, get static names that don't change so we can manage them better.

@felixrabe
Copy link

@rogeriochaves @TheAlexPorter Please find or open an issue for this and mention it here. Thx!

@denis1stomin
Copy link

Hey guys,
I have a few questions for you regarding the strategy.
I have a folder somewhere inside my repo some/path/webui.
This folder contains standard package structure and src folder in it.
I moved index,html file into src folder since for me it is a source code.
(I'm a pro developer but really new in those all frontend things :))
Now when I run "npx parcel build" (production mode) or globally "parcel build" it produces a file like
dist/src.4320c2f6.html. Looks like it treats all src/* files like "aren't entry points".
For me it looks like a bug.
So the questions are:

  1. Is it a bug for you and can it be easily fixed in future versions?
  2. If it is "by design", how you'd describe (your vision) where html files should be places and why?

Thanks

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jun 6, 2018

@denis1stomin html files are not content hashed if they are the entry-point or required from html. So not sure why this file is content hashed?

Feel free to open an issue, as you won't get much response on a merged PR

@imWildCat
Copy link

I really need the support for disabling hashing for a specific path, for example ./images/.

I am struggling to use parcel with lottie-web,

This way <a href="x"> does not support wildcard paths, such as <a href="./images/*">.

Currently I have to do this in a very ugly way:

        <a href="./images/img_0.png" />
        <a href="./images/img_1.png" />
        <a href="./images/img_2.png" />
        <a href="./images/img_3.png" />
        <a href="./images/img_4.png" />
        <a href="./images/img_5.png" />
        <a href="./images/img_6.png" />
        <a href="./images/img_7.png" />
        <a href="./images/img_8.png" />
        <a href="./images/img_9.png" />
        <a href="./images/img_10.png" />
        <a href="./images/img_11.png" />

@micopiira
Copy link

Would it be possible to hash the filenames when using a JS entrypoint?

For example: parcel build index.js test.js would createdist/index.da39a3.jsand a dist/test.ee5e6b.js, possibly with a JSON file containing the mappings

{
  "index.js": "dist/index.da39a3.js",
  "test.js": "dist/test.ee5e6b.js"
}

This feature would make working with a Maven/Java web project a lot easier (for me, atleast..)

@LLe27
Copy link

LLe27 commented May 16, 2019

Hey everyone,

I apologize if my question is not clear and concise. I am relatively new to the parceljs bundler and progressive web apps. I am currently trying to create an offline experience but I need to specify specific JS and CSS files that are needed for this experience in production. How do I, or how would I detect the exact filenames considering they are appended with hashes? This is needed for the cache file list for my service worker.

Thanks!

@mischnic
Copy link
Member

@LLe27 Some options:

  • pass --no-content-hash to Parcel so that the hash stays constant (is only dependent on the path and not the content).
  • generate the Service worker cache list after building based on the actual output names

@LLe27
Copy link

LLe27 commented May 17, 2019

@mischnic

Thanks, i'll give this a try! Worse case i'll just implement the Workbox library.

EDIT: This method worked perfect!

@joeosburn
Copy link

@devongovett We've tested this and the bundle files do get updated with new filename hashes when their contents change. However, the entry file does not get updated to point to the newly hashed files.

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