Parcel: --no-content-hash or something else ? (don't touch the assets please)

Created on 19 Oct 2019  ยท  13Comments  ยท  Source: parcel-bundler/parcel

โ” Question

Hello!

I've searched all along but I don't see anything that really answer the question (or the problem).

I want to take benefit of the --no-content-hash but the console say unknown option--no-content-hash'` and I tried with the 'build' command and it make the (nearly) same output, files are still hashed.

I use the plugin parcel-plugin-asset-copier

Here are my full commands:
Dev: "dev:local": "cross-env parcel html/index-dev.html --no-autoinstall --port 8081 -d dist/dev/ --cert ssl/localhost.crt --key ssl/localhost.key",
Prod: "build": "cross-env parcel build --no-content-hash html/index-dev.html -d dist/prod/"

So far so good (without the --no-content-hash) but the problem is that every single assets used by html/css is hashed and throw in the dev/ folder, while I also have every assets in my /assets/ sub repos.

So I have fonts, icons, etc.
And also got them in /assets/.

Maybe that something we will be able to do in Parcel 2? (I'm on LTS projects only..)

๐Ÿ”ฆ Context

I would like that parcel do not modify resources that way I can keep them in the folder.
It's not "clean" that way and we have several builds on the server using the same resources.
Also, each templates (and JS) are using images, so I can't just "delete" my assets folders, as it is now, parcel put some images in the dist/, and I have duplicates in assets/ and both are used...

๐ŸŒ Your Environment

Last parcel 1.12.4
parcel plugin asset copier: 1.0.0

Question

All 13 comments

That's the problem for me too. I want to not hash images referenced in CSS because I am trying to not change some old project, just rebuild CSS in-place with Parcel.

Now I got structure like this:

templates
  default
    style.css
    images
      arrow.png

and I want to make it like this:

templates
  default
    style.styl
    images
      arrow.png

but Parcel fails on compiling it because style.styl now contains broken URLs like background: url(/path/to/server/folder/some.jpg). I fixed all of them, but now I have all referenced images in dist folder I set to templates/default to override style.css when I will build it on server. So I got this:

templates
  default
    style.css
    arrow.b5776194.png
    images
      arrow.png

But do not want to change URLs in style.css.

Maybe we can configure or disable this feature in Parcel?

I found parcel-plugin-disable-loaders but it can disable only one of css/html/js/wasm but not url-loader or something.

Also I found this comment in #1186 but commenting out all style.define('url', node => {}) hook didn't help me.

And finally, I found a plugin that monkey-patches original Parcel code, so we can find a workaround on how to disable assets copying: parcel-plugin-bundle-deep-hash

In sources, there is following thing:

const Bundle = require('parcel-bundler/src/Bundle');
module.exports = function (bundler) {
    Object.assign(Bundle.prototype, {
        // here you can re-define methods from node_modules/parcel-bundler/src/Bundle.js
    });
};

There is no public repository of this plugin but you can browse code published in NPM using npmfs: https://npmfs.com/package/parcel-plugin-bundle-deep-hash

So, I copied getHashedBundleName here and now I can control how hashes is added. Maybe I will release a plugin that fixes this issue by monkey-patching.

i will just get Gulp for now

As I needed this for some electron builds and others here is my tip.
Just do a script tag in your html page that will load the CSS, and put the CSS files into the assets folder.

Or second option, you add the CSS lines with a bash or node script after parcel processed.

No the best tho but still effective.

also this issue is not a question but a feature request

The doc mention this so it's an issue as this does nothing so far ^^

I also cannot use webfonts in PROD via CSS @font-face rules, because all the font filename are hashed.

Yeah it's a big pain in the ass for those who do custom stuff by hand and not the mainthread techs :p

But you can find different loaders for differents things, also what I've done to one of my project I build with Electron (and parcel just DIE if I put the script in reference LOL) it's to load custom files with a JS script, adding the tag in the DOM will do the job you are looking for, but in some cases you might need an extra script in your package to build those "detached" files.

Waiting for parcel2 should resolve A LOT of those problems (hopefully !)

This problem is frustrating me since parcel 1 and still in parcel 2.
This is one of 2 points why i aaaaalways when i try out parcel have to switch back to fusebox or webpack.
Because parcel does things and makes it reeeeally really hard to undo these things.
Sorry @Inateno but this problem still persists in Parcel 2 and will stay forever i guess.

But dont worry. You can create a npm package which MUST begin with "parcel-namer-" and add it to a config.
Oh why you cant add it from a local folder? Because your life is otherwise a bit too easy... Sad. Really sad...

Parcel is essentially unusable for Shopify projects. Guess I'm going to use webpack then. ๐Ÿ‘ This issue.

I have a (temporary) workaround...

I wrote a Node script to remove the hash from Parcel-bundled SVG sprites, so I can reference the sprites directly in the HTML or from JS-generated markup in components like sliders & accordions.

Our production build & deploy process manages versioning of static assets (CSS/JS, and SVG) so I don't need to retain any hashes that Parcel generates.

You can solve this with a custom namer plugin: https://v2.parceljs.org/plugin-system/namer#content. This gives you complete control over the file naming of all bundles produced by parcel.

Also --no-content-hash is already supported in v2, though it doesn't remove the hash entirely just the "content" part of the hash.

Thanks... but I'm not sure what you mean by removing just the "content" part of the hash? ;)

I've looked at the docs for namer plugins... sorry, but I still don't understand how to author one and use it? Can an example be added to namers folder?

This line ultimately adds the hash: name += "." + bundle.hashReference;

https://v2.parceljs.org/plugin-system/namer#including-a-hash

So removing this line would never add a hash:
https://github.com/parcel-bundler/parcel/blob/891f6611a74f72c242d9b3bfceca47a24221d2b4/packages/namers/default/src/DefaultNamer.js#L99

But one problem is that you still need to generate a unique name for each bundle (which is rather difficult without using hashes for shared bundles).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidnagli picture davidnagli  ยท  3Comments

mnn picture mnn  ยท  3Comments

jzimmek picture jzimmek  ยท  3Comments

Niggler picture Niggler  ยท  3Comments

philipodev picture philipodev  ยท  3Comments