16

How can I webpack a web app into an output .html file, starting from a traditional input .html?

Here is a simple starting point:

index.html

<body>
  <output></output>
  <script src="./main.js"></script>
</body>

main.js

import React from "react";
document.querySelector("output").innerText = React.version;

webpack.config.js

module.exports = {
  entry: "./index.html",
  output: {
    filename: "output.html"
  },
  module: {
    rules: [
      {test: /^index\.html$/, use: [
        {loader: "extract-loader"},
        {loader: "html-loader", options: {
          attrs: ["script:src"]
        }}
      ]}
    ]
  }
}

This results in SyntaxError: Unexpected token import when processing main.js, however.

P Varga
  • 19,174
  • 12
  • 70
  • 108

1 Answers1

0

I hava the same issue. My solution is: create empty js file as entry and you can remove that js from output path if don't need it.

Addtionally, if you use html-webpack-plugin, you can set inject to false, the html would not inject js.

new HtmlWebpackPlugin({
  template: './views/cubes.njk', // your template
  filename: 'cubes.html',
  inject: false,
})
JackChouMine
  • 947
  • 8
  • 22