【问题标题】:`npm run build` does not generate `index.html``npm run build` 不会生成 `index.html`
【发布时间】:2018-04-09 00:21:19
【问题描述】:

在我的 Vue.JS 项目中,我运行后:

npm run build 

dist 目录下没有index.html 文件:

我的webpack.base.config.js 文件是:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: {
    main: './src/main',
    vendors: './src/vendors'
  },
  output: {
    path: path.join(__dirname, './dist')
  },
  module: {
    rules: [
      {
        test: /.vue$/,
        use: [{
          loader: 'vue-loader',
          options: {
            loaders: {
              less: ExtractTextPlugin.extract({
                use: ['css-loader?minimize', 'autoprefixer-loader', 'less-loader'],
                fallback: 'vue-style-loader'
              }),
              css: ExtractTextPlugin.extract({
                use: ['css-loader', 'autoprefixer-loader', 'less-loader'],
                fallback: 'vue-style-loader'
              })
            }
          }
        },
          {
            loader: 'iview-loader',
            options: {
              prefix: false
            }
          }
        ]
      },
      {
        test: /iview\/.*?js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: ['css-loader?minimize', 'autoprefixer-loader'],
          fallback: 'style-loader'
        })
      },
      {
        test: /\.less$/,
        use: [{
          loader: "style-loader" // creates style nodes from JS strings
        }, {
          loader: "css-loader" // translates CSS into CommonJS
        }, {
          loader: "less-loader" // compiles Less to CSS
        }]
      },
      {
        test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
        loader: 'url-loader?limit=1024'
      },
      {
        test: /\.(html|tpl)$/,
        loader: 'html-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.vue'],
    alias: {
      'vue': 'vue/dist/vue.esm.js',
      '@': path.resolve('src')
    }
  }
};

我的webpack.prod.config.js 代码如下:

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');
const webpackBaseConfig = require('./webpack.base.config.js');
const fs = require('fs');

fs.open('./src/config/env.js', 'w', function (err, fd) {
  const buf = 'export default "production";';
  fs.write(fd, buf, 0, buf.length, 0, function (err, written, buffer) {
  });
});

module.exports = merge(webpackBaseConfig, {
  output: {
    publicPath: '/dist/',
    filename: '[name].[hash].js',
    chunkFilename: '[name].[hash].chunk.js'
  },
  plugins: [
    new ExtractTextPlugin({
      filename: '[name].[hash].css',
      allChunks: true
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendors',
      filename: 'vendors.[hash].js'
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),
    new HtmlWebpackPlugin({
      filename: '../index_prod.html',
      template: './src/template/index.ejs',
      inject: false
    })
  ]
});

edit-1

$ npm run build

> wx_backup@1.0.0 build /Users/lkl/Desktop/my-project/web/wx_backup
> webpack --progress --hide-modules --config webpack.prod.config.js

【问题讨论】:

  • 你能告诉我们你的npm run build调用了哪个命令吗?
  • 查看我的编辑。兄弟

标签: npm vue.js


【解决方案1】:

您的webpack.output.path 配置是dist 目录。

HTMLWebpackPlugin.filename 相对于 dist 目录。您指定的文件名会将您的 HTML 文件保存在 dist 上方的目录中。

如果您希望将 HTML 文件保存在您的 dist 目录中,请尝试使用 ./index_prod.html。

new HtmlWebpackPlugin({
  filename: './index_prod.html',
  template: './src/template/index.ejs',
  inject: false
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2018-09-23
    • 2019-10-05
    • 2019-03-19
    • 2020-08-29
    • 2021-02-18
    • 2020-05-10
    相关资源
    最近更新 更多