jantimon / jantimon/html-webpack-plugin

Unable to include a static JS file (generated hashed filename does not match the one in the index.html)

Open
#1,814 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

wontfix
Dominant language
JavaScript
Stars
10.7k
Forks
1.3k
Avg merge
1m
Merged PRs (30d)
1

Description

## Current behaviour πŸ’£

In the `index.html` I want to include a static JS File (a 3rd party JS file, not bundled with `webpack`):

**index.html**
```

```

This file should be included **before** the main JS file (injected by `html-webpack-plugin)` and should not be defered. It's name should also be unique (hashed).

After build, the `dist` folder contains:

`index.html`:
```

<script defer="defer" src="js/index.0e428668b6b726cf637e.js">
```

but the generated js file is: `modernizr.d14201e2d4a0ec290a14.js`

```
asset modernizr.d14201e2d4a0ec290a14.js 10.5 KiB [emitted] [immutable] [from: modernizr.js] [minimized] 1 related asset
```

So the filenames do not match (content hashes are different) and the browser returns a 404 error.

When running `webpack serve`, it seems that the generated hashes are the same.

## Expected behaviour β˜€οΈ

After build, the generated filename should match the filename referanced in `dist` html file.

### Reproduction Example πŸ‘Ύ

Use the following code above.

`webpack.config.js`

```
...
module: {
rules: {
{
test: /\.html$/i,
loader: "html-loader",
},
{
test: /modernizr\.js$/i,
type: "asset/resource",
generator: {
filename: "[name].[contenthash].js"
},
},

}
}
...
```

## Environment πŸ–₯

```
Node.js v18.15.0
darwin 22.3.0
9.5.0
job-index@1.0.0 /Users/andreivictor/Work/job-index
β”œβ”€β”¬ clean-webpack-plugin@4.0.0
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ copy-webpack-plugin@11.0.0
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ css-loader@6.8.1
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ css-minimizer-webpack-plugin@5.0.1
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ html-loader@4.2.0
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ html-webpack-plugin@5.5.3
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ image-minimizer-webpack-plugin@3.8.3
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ mini-css-extract-plugin@2.7.6
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ sass-loader@13.3.2
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ webpack-cli@5.1.4
β”‚ β”œβ”€β”¬ @webpack-cli/configtest@2.1.1
β”‚ β”‚ └── webpack@5.88.1 deduped
β”‚ β”œβ”€β”¬ @webpack-cli/info@2.0.2
β”‚ β”‚ └── webpack@5.88.1 deduped
β”‚ β”œβ”€β”¬ @webpack-cli/serve@2.0.5
β”‚ β”‚ └── webpack@5.88.1 deduped
β”‚ └── webpack@5.88.1 deduped
β”œβ”€β”¬ webpack-dev-server@4.15.1
β”‚ β”œβ”€β”¬ webpack-dev-middleware@5.3.3
β”‚ β”‚ └── webpack@5.88.1 deduped
β”‚ └── webpack@5.88.1 deduped
└─┬ webpack@5.88.1
└─┬ terser-webpack-plugin@5.3.9
└── webpack@5.88.1 deduped

job-index@1.0.0 /Users/andreivictor/Work/job-index
└── html-webpack-plugin@5.5.3
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up β€” it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduced webpack.config.js, index.html, and the interaction between html-loader, html-webpack-plugin, and the asset/resource rule for modernizr.js. Compare the production build with webpack serve to trace why the content hash referenced in the generated dist index.html differs from the emitted asset. Done means the generated HTML references the emitted hashed filename and the browser no longer receives a 404.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, webpack
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.