1

I am very new to webpack and stuffs, I need a solution to separate base href of index.html and src for bundle.js, for development and production as both are different.

For Development

base href = localhost

src = /bundle.js

For Production

base href = server url

src = /dist/bundle.js

To solve the above problem I am trying to use HtmlWebpackPlugin, following is the webpack.config.js setting

var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname + "/dist",
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    rules: [
    {
     exclude: /node_modules/,
     use:[
      {
      loader: 'babel-loader',
      options:{
          presets: ['react', 'es2015', 'stage-1']
       }
      },
     ]
  },
  plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       }),
       new HtmlWebpackPlugin({
          template:'index.html',
          inject:'head',
          hash: true,
          baseHref: 'http://localhost:8030/'
      })
  ]
};

and following is the way I am trying to use baseHref into index.html

<html>
  <head>
    <% if (htmlWebpackPlugin.options.baseHref) { %>
      <base href="<%= htmlWebpackPlugin.options.baseHref %>">
    <% } %>

    /*
       Several css are defined with relative path here
    */
  </head>
  <body>
    <div class="container-fluid"></div>
  </body>
  <script src="/bundle.js"></script>
</html>

I am receiving following error by using above settings

enter image description here

I need help to know what I am doing wrong here?

Any help would be highly appreciated.

Thanks.

5
  • Is html-loader installed? Commented Sep 9, 2017 at 19:15
  • nope it is not installed, does it make any difference @Prakashsharma ? Commented Sep 9, 2017 at 19:16
  • I would say use ejs template instead. You dont need any loader for that. github.com/jantimon/html-webpack-plugin/blob/master/docs/… Commented Sep 9, 2017 at 19:18
  • The link says that if you use html template then you need html-loader for that. Commented Sep 9, 2017 at 19:19
  • I fixed it, the problem was missing test: /\.js$/, for babel-loader it worked for html, though I changed to ejs now. Thanks for help. Commented Sep 9, 2017 at 19:28

2 Answers 2

2

Main purpose of HtmlWebpackPlugin is to recognise your entry files and place them into appropriate place (dist/index.html). So you don't need to that manually.

If you don't have HtmlWebpackPlugin you should to remember each file which you use in application and manually add them into index.html.

Sign up to request clarification or add additional context in comments.

Comments

1

https://github.com/jantimon/html-webpack-plugin/issues/212

This issue on Github suggests renaming your "index.html" file to "index.ejs".

This appears to be because webpack is trying to apply the Babel transpiler to your html file and it fails, the ".ejs" extension will prevent it.

2 Comments

I fixed it, the problem was missing test: /\.js$/, for babel-loader it worked for html, though I changed to ejs now. Thanks for help.
I was going to suggest that, but your config didn't have a babel-loader!

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.