html-webpack-plugin: Error in tagged template literal in script tag

Expected behaviour

Pass this code.

<!--  index.html -->
<script>
  if (window.location.hostname !== 'localhost' && window.location.protocol === 'http:') {
    window.location.href = `https://${location.host}/${location.search}`
  }
</script>

Current behaviour

ERROR in Template execution failed: ReferenceError: location is not defined

ERROR in   ReferenceError: location is not defined


  - loader.js:4 eval
    [index.html?.]/[html-webpack-plugin]/lib/loader.js:4:10

  - loader.js:9 module.exports
    [index.html?.]/[html-webpack-plugin]/lib/loader.js:9:3

  - index.js:284 Promise.resolve.then
    [myapp]/[html-webpack-plugin]/index.js:284:18

  - next_tick.js:68 process._tickCallback
    internal/process/next_tick.js:68:7

Environment

    "webpack": "4.16.5",
    "html-webpack-plugin": "^3.2.0"

Config

        new HtmlPlugin({
          template: path.join(__dirname, "src/index.html")
        }),
<!DOCTYPE html>
<html>
    <head>
        <title>My App</title>
    </head>
    <body>
    <script>
        if (window.location.hostname !== 'localhost' && window.location.protocol === 'http:') {
          window.location.href = `https://${location.host}/${location.search}`
        }
    </script>
    </body>
</html>

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I’m having this issue as well after running npm i html-webpack-plugin@next

@jantimon thanks!

just copy the default loader, and update config like template: '!!' + require.resolve('./loader') + '!' + template fix the problem.