Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
Why is webpack-html-plugin adding script tag in head instead of body when using template?

I am using html-webpack-plugin.

As per the documentation, script tag should be added in body.

But in my case, it's being added in header.

My folder structure :

│   package-lock.json
│   package.json
│   webpack.config.js
│   
├───dist
│       index.html
│       main.js
│       
├───node_modules
│   │   .package-lock.json
│   └─── And many more files
│           
└───src
    │   index.html
    │   
    └───scripts
            index.js
            

My webpack.config.js :

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    mode: 'development',
    entry: './src/scripts/index.js',
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ]
}

Output dist/index.html :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Webpack Demo</title>
<script defer src="main.js"></script></head>
<body>
    <h1>Webpack Demo</h1>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The behavior is the same as in the very first usage example in the documentation, so the script tag is actually added in the head by default. The script still loads only after the HTML has been parsed, since the script tag has the defer attribute, so there should not be any problem with it. But if you still want the script tag to be added at the end of the body, you can use the inject option. Please refer to the documentation for more details.

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    mode: 'development',
    entry: './src/scripts/index.js',
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            inject: 'body'
        })
    ]
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda