Webpack html插件不生成html

时间:2017-10-05 09:35:23

标签: javascript node.js webpack graphql html-webpack-plugin

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

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?

3 个答案:

答案 0 :(得分:5)

这是适合我的那个。如果您仍然遇到任何问题,请告诉我。我将与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'
    })
  ]
}

webpack-html-ejs

答案 1 :(得分:4)

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

答案 2 :(得分:3)

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

这样做的一种方法是向prestart添加package.json脚本。当您执行startmore details)时,这会自动在npm start脚本之前执行:

{
    "version": "1.0.0,
    "name": "my-app",
    "scripts": {
        "prestart": "webpack",
        "start": "nodemon server.js --exec babel-node --presets es2015,stage-2"
    }
}