jantimon / jantimon/html-webpack-plugin
Unable to include a static JS file (generated hashed filename does not match the one in the index.html)
Nobody has claimed this yet.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up β it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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