Webpack html插件不会生成html
我正在使用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?
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
时,会在start
脚本之前自动执行( 更多详细信息 ):
{ "version": "1.0.0, "name": "my-app", "scripts": { "prestart": "webpack", "start": "nodemon server.js --exec babel-node --presets es2015,stage-2" } }