2

I have an Angular 7 project which generates styles.[hash].css file in prod build.

I have an iframe within one of my components which loads a static html file defined within the same project (not an angular component, just plain static html).

I want to reuse styles of my project in my static html file which is loaded within the iframe. Is there a way to know the name of the styles.css file which is generated in the dist folder as an output of the prod build?

I am generating prod build using ng build --aot --prod --stats-json

This generates a stats.json file which has the file names in field assetsByChunkName.

"assetsByChunkName": {
        "runtime": "runtime.b2ebd3cc7f73d5966db1.js",
        "main": "main.9872f510320bbf1b0ad9.js",
        "polyfills": "polyfills.b2e1d6dfcc48e480634c.js",
        "styles": "styles.4bd3b6df7eaeb9129b4d.css"
    }

Is there a clean way to (may be using a plugin), to add following line in my iframe's html file like <link rel="stylesheet" href="styles.4bd3b6df7eaeb9129b4d.css"> ?

Looking for a solution that will work in both ng serve and prod build of angular-cli

Note: I want to maintain the hash in prod files for its benefits

2 Answers 2

1

Add "outputHashing": "none", to angular.json

this will remove 4bd3b6df7eaeb9129b4d from style.css, documentation

  "configurations": {
    "production": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }
      ],
      "optimization": true,
      "outputHashing": "none", // here
      "sourceMap": true,
      "extractCss": true,
      "namedChunks": false,
      "aot": true,
      "extractLicenses": true,
      "vendorChunk": false,
      "buildOptimizer": true,
      "budgets": [
        {
          "type": "initial",
          "maximumWarning": "2mb",
          "maximumError": "5mb"
        }
      ]
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I want to maintain the hash in prod files for its benefits
Just an off topic question , can i know what are the benefits of having a hashed file name ?
mostly caching benefits at the browser. If I haven't updated my style.css in between two releases then my user need not fetch the file again. The hash is created based on the file content and remains the same unless the file content changes. This link might provide details webpack.js.org/guides/caching
1
document.querySelector('link[rel=stylesheet][href^=styles][href*=css]')?.href

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.