Vue JS:构建命令“npm run build”无法生成“index.html”。

3

当我运行npm run build时,它没有在dist/目录中创建index.html。我需要index.html的原因是我想将我的Vue项目部署到AWS EC2 (/var/www/html/)。如何生成这个index.html?

我的运行npm run build之后的dist/目录结构:

enter image description here

我的package.json:

{
  "name": "proto",
  "description": "Prototype",
  "version": "1.0.0",
  "license": "MIT",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "dependencies": {
    "@melmacaluso/vue-modal": "^2.1.0",
    "firebase": "^7.14.2",
    "fusioncharts": "^3.15.1-sr.1",
    "vue": "^2.5.11",
    "vue-fusioncharts": "^3.0.4",
    "vue-router": "^3.1.6",
    "vuex": "^3.3.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-stage-3": "^6.24.1",
    "cross-env": "^5.0.5",
    "css-loader": "^0.28.7",
    "file-loader": "^1.1.4",
    "vue-loader": "^13.0.5",
    "vue-template-compiler": "^2.4.4",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.9.1"
  }
}


我的webpack.config.js文件:
var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}


构建命令输出:
$ npm run build

> proto@1.0.0 build C:\Users\john\Documents\VUE\dummy_prototype_1
> cross-env NODE_ENV=production webpack --progress --hide-modules

1
部署 /dist 文件夹(包括捆绑文件)到 /var/www/html/ 目录下的 index.html 文件中,并确保 index.html 中有一行 <script> 加载捆绑文件。 - willystyle
谢谢提供信息。由于我已经分享了dist/文件夹,您能否给出更多有关<script>行的想法?请原谅,我是新手。 - user10930017
1
<script src="./dist/your_web.min.bundle.js"></script> - willystyle
2个回答

4
在您的webpack.config.js中,向下滚动,然后像@Michal Levy建议的那样将HtmlWebpackPlugin插件添加到module.exports.plugins中。 更新: 对于错误(webpack.js:348 throw err; TypeError: Cannot read property 'make' of undefined),可能的原因之一是版本问题。解决方法是将html-webpack-plugin降级(卸载然后安装)到3.2.0以下是新的插件配置:
if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    // below is the plugin you need to add
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, './dist/index.html'), // the path where is the `index.html` generated.
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

将上述代码复制到 webpack.config.js 后,当我运行 npm run build 时,出现了以下错误:C:\Users\...\node_modules\webpack\bin\webpack.js:348 throw err; TypeError: Cannot read property 'make' of undefined - user10930017
你安装了插件吗?npm install --save-dev html-webpack-plugin - Sphinx
2
降级(卸载然后安装)html-webpack-plugin到3.2.0,然后再试一次。 - Sphinx
非常感谢,它起作用了。你要么是一个魔术师,要么是天才。 - user10930017

2

只需使用HtmlWebpackPlugin

它会自动在dist文件夹中创建新的或从您提供的模板创建index.html,并注入任何必要的<script>(用于js捆绑包)或<link>(用于提取的CSS)标记

看起来您已经有了index.html模板,请使用template选项-请参阅文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接