Webpack html 插件未生成 html

Webpack html plugin is not generating html

我正在使用 webpack html 插件从 graphiql.ejs 生成 html 页面,但当我 运行 时它没有生成 html 页面宁npm start

webpack.config.js

var HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      filename: "public/graphql/index.html", // Write the file to <public-path>/graphql/index.html
      inject: false, // Do not inject any of your project assets into the template
      GRAPHQL_VERSION: packageJSON.dependencies.graphql.replace(/[^0-9.]/g, ""), // Get the graphql version from my package.json
      template: "graphiql.ejs" // path to template
    })
  ]
};

我想在 /public/graphql 目录中生成 index.html。有谁知道我做错了什么? 运行 webpack 还有其他命令吗?

webpack.config.js

    const path = require('path');
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    const packageJSON=require("./package.json");
    module.exports = {
        entry: './src/app.js',
        output: {
            path: path.resolve(__dirname, 'public'),
            filename:"build.js"
        },
        plugins: [
            new HtmlWebpackPlugin({
                filename: "graphql/index.html", // Write the file to <public-path>/graphql/index.html
                inject: false, // Do not inject any of your project assets into the template
                GRAPHQL_VERSION: packageJSON.dependencies.graphql.replace(/[^0-9.]/g, ""), // Get the graphql version from my package.json
                template: "graphiql.ejs" // path to template
            })
        ]      
    }

运行 webpack -p 生成html

webpack -p

这是对我有用的那个。如果您仍然遇到任何问题,请告诉我。我将与 github.

分享代码
const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const packageJson = require("./package.json");

const GRAPHQL_VERSION = packageJson.dependencies.graphql.replace(/[^0-9.]/g, '');

module.exports = {
  entry: 'index.js',
  output: {
    path: path.resolve(__dirname, 'public'),
    filename: 'index.bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin({ 
      filename: 'index.html',
      inject: false,
      GRAPHQL_VERSION: GRAPHQL_VERSION,
      template: 'graphiql.ejs'
    })
  ]
}

您需要确保在执行 npm start 时确实 运行 webpack。

一种方法是将 prestart 脚本添加到 package.json。当您执行 npm start (more details):

时,这将在 start 脚本之前自动执行
{
    "version": "1.0.0,
    "name": "my-app",
    "scripts": {
        "prestart": "webpack",
        "start": "nodemon server.js --exec babel-node --presets es2015,stage-2"
    }
}