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

Remove hash from JS file using CLI #2743

Closed
deadcoder0904 opened this issue Mar 8, 2019 · 11 comments
Closed

Remove hash from JS file using CLI #2743

deadcoder0904 opened this issue Mar 8, 2019 · 11 comments

Comments

@deadcoder0904
Copy link

deadcoder0904 commented Mar 8, 2019

🐛 bug report

I want to remove hash of a JS file using CLI, not API

🎛 Configuration (.babelrc, package.json, cli command)

{
  "build": "parcel build --no-content-hash index.html --out-dir dist",
}

🤔 Expected Behavior

I want a plain background.js without hash while still using CLI

😯 Current Behavior

Generates JS file with a hash like background.7as78c.js

💁 Possible Solution

A method like --no-content-hash on CLI just like API has contentHash: false as mentioned here

🔦 Context

I am trying to make a simple Chrome extension using Parcel. It has a background.js script referenced through manifest.json.

So, even if my index.html points to correct background.js file which is hash-based, my manifest.json only points to the background.js which I've mentioned.

💻 Code Sample

background.js

console.log(`Gimme a hell yeah ☠️`)

manifest.chrome.json / manifest.firefox.json

{
  "manifest_version": 2,
  "name": "Chrome extension",
  "version": "1.0.0",
  "background": {
    "scripts": ["background.js"]
  }
}

package.json

Currently, I'm solving removing hash with a hack of renaming using script rename:background. The gripe with that is it doesn't change the reference of index.html but thankfully I'm not using index.html in this project.

{
  "name": "chrome-extension",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "clean": "rimraf dist",
    "build:firefox": "parcel build --no-content-hash index.html --out-dir dist/firefox",
    "build:chrome": "parcel build --no-content-hash index.html --out-dir dist/chrome",
    "copy:firefox": "cpy 'manifest.firefox.json' 'icons' 'dist/firefox'",
    "copy:chrome": "cpy 'manifest.chrome.json' 'icons' 'dist/chrome'",
    "rename:background": "rename -n 'dist/**/background.*.js' 'dist/**/background.js'",
    "rename:firefox": "rename 'dist/firefox/manifest.firefox.json' 'dist/firefox/manifest.json'",
    "rename:chrome": "rename 'dist/chrome/manifest.chrome.json' 'dist/chrome/manifest.json'",
    "prod": "npm-run-all clean build:* copy:* rename:*"
  },
  "license": "MIT",
  "devDependencies": {
    "cpy-cli": "^2.0.0",
    "npm-run-all": "^4.1.5",
    "parcel-bundler": "^1.11.0",
    "rename-cli": "^6.0.0",
    "rimraf": "^2.6.3"
  }
}

🌍 Your Environment

Software Version(s)
Parcel 1.10.3
Node 10.13.0
Yarn 1.13.0
Operating System 10.14.2
@mischnic
Copy link
Member

mischnic commented Mar 8, 2019

There is an option --no-content-hash:

.option('--no-content-hash', 'disable content hashing')

Does that work?

@deadcoder0904
Copy link
Author

@mischnic as you can see I've specified it in my scripts build:firefox it doesn't work sadly :(

@mischnic
Copy link
Member

mischnic commented Mar 8, 2019

Oh, --no-content-hash makes the hash based only on the path of the given file, not on the content:

// If content hashing is enabled, generate a hash from all assets in the bundle.
// Otherwise, use a hash of the filename so it remains consistent across builds.

use a hash of the filename so it remains consistent across builds.

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Mar 8, 2019

You could still use --no-content-hash and just use the generated name, in theory it shouldn't change as long as the path doesn't change. Or you could generate the chrome manifest?
I even think there's already a plugin for generating these manifest files if I'm not mistaken

@deadcoder0904
Copy link
Author

You could still use --no-content-hash and just use the generated name, in theory it shouldn't change as long as the path doesn't change. Or you could generate the chrome manifest?

Doesn't work.

I even think there's already a plugin for generating these manifest files if I'm not mistaken

Any links?

@DeMoorJasper
Copy link
Member

I think this one is for chrome extensions? https://yarnpkg.com/en/package/parcel-plugin-web-extension

@mischnic
Copy link
Member

mischnic commented Mar 8, 2019

Maybe this one works better in this case: https://github.com/mugi-uno/parcel-plugin-bundle-manifest

@deadcoder0904
Copy link
Author

@mischnic so I tried your recommendation but it gives error

Could not load background script 'background.js'.
Could not load manifest.

Because I specified background.js in manifest.json like so, so it can't locate it as it still contains hash.

Complete repo is here 👉https://github.com/deadcoder0904/multiple-new-tab

Trying other recommendation now

@deadcoder0904
Copy link
Author

@DeMoorJasper that doesn't work properly at all. It doesn't even copy my background.js contents & outputs a filename that is specified after build, in my case, it outputs manifest.chrome.js & manifest.firefox.js because I did parcel build manfiest.chrome.json & parcel build manfiest.firefox.json.

@deadcoder0904
Copy link
Author

deadcoder0904 commented Mar 9, 2019

Okay so I found the solution & it was indeed my mistake 🤦‍♂️

What I did was specified --no-content-hash in front of index.html but index.html never had hash

So I accidentally removed my index.html & placed background.js in place of there & it worked.

The final script was parcel build --no-content-hash background.js --out-dir dist/chrome.

I didn't even need parcel-plugin-bundle-manifest which works incredibly well.

Thanks, everyone for helping out 🙌

@kahlan88
Copy link

kahlan88 commented Nov 10, 2022

When I run parcel build --no-content-hash src/index.js

I don't get the hash in the file name, but it also omits minification. Is that how it should work? I thought there was an option to disable minification that was separate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants