最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack html plugin is not generating html - Stack Overflow

programmeradmin1浏览0评论

I am using webpack html plugin to generate the html page from the graphiql.ejs but it is not generating html page when I am running 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
    })
  ]
};

I want to generate the index.html inside the /public/graphql directory. Does anyone know what I am doing wrong ? Is there any other command to run webpack ?

I am using webpack html plugin to generate the html page from the graphiql.ejs but it is not generating html page when I am running 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
    })
  ]
};

I want to generate the index.html inside the /public/graphql directory. Does anyone know what I am doing wrong ? Is there any other command to run webpack ?

Share Improve this question edited Oct 7, 2017 at 13:54 N Sharma asked Oct 5, 2017 at 9:35 N SharmaN Sharma 34.5k98 gold badges265 silver badges452 bronze badges 7
  • You need to put it into the plugins array and export it webpack.github.io/docs/using-plugins.html – serge1peshcoff Commented Oct 5, 2017 at 10:13
  • @serge1peshcoff I did pastebin.com/1NgiM3kY but still it is not generating – N Sharma Commented Oct 5, 2017 at 10:30
  • Does your npm start script run webpack.config.js file? – Jehy Commented Oct 9, 2017 at 16:14
  • @Jehy Nope. it runs "start": "nodemon server.js --exec babel-node --presets es2015,stage-2" – N Sharma Commented Oct 9, 2017 at 18:10
  • @Jehy How to enable webpack too ? – N Sharma Commented Oct 11, 2017 at 7:46
 |  Show 2 more comments

3 Answers 3

Reset to default 5 +50

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
            })
        ]      
    }

run webpack -p to generate the html

webpack -p

Here is the one that worked for me. If still you face any issue let me know. I will share the code with 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'
    })
  ]
}

You need ensure that you actually run webpack when you do npm start.

One way of doing that is to add a prestart script to package.json. This will automatically be executed before the start script when you do npm start (more details):

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

评论列表(0)

  1. 暂无评论