Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlWebPackPlugin injection location

dev-server, my webpack will build my bundle which includes jquery.

I use html-webpack-plugin, the line of code below will inject my bundle in the body when build prod.

new HtmlWebPackPlugin({
            template: './src/index.html'
        }),

My problem is, in my index.html, I semantic-ui.js, which depends on Jquery,

 <body>
      <my-app>loading...thaison</my-app>

      <script src="./asset/semantic-ui/dist/semantic.min.js"></script>    
  </body>

So when I fire my dev environment or prod build, the bundle appears after semantic hence i have an error. How do I make is so that the dev injection happens before my manual script source?

dev server serve:

webpack-dev-server --inline --progress --port 4000 --content-base src
like image 937
chungtinhlakho Avatar asked Jul 12 '26 15:07

chungtinhlakho


1 Answers

This is because you are using the default injection location for JS just before the tag. One way I have addressed this is by:

  1. Setting inject to false in the HtmlWebPackPlugin config.
  2. Adding an injection point to the index (ejs) template:

Code below:

<% for(var i=0; i < htmlWebpackPlugin.files.js.length; i++) {%>
 <script type="text/javascript" src="<%= htmlWebpackPlugin.files.js[i] %>"></script>
<% } %>

You can add this anywhere in the index template you like, and therefore avoid it being injected at the end.

You can read more about the options available in the template in the "Writing your own templates" section. You might want to access the files chunks rather than the raw list of files for example.

like image 113
Joe Dreimann Avatar answered Jul 14 '26 08:07

Joe Dreimann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!