当我在浏览器中运行 `index.html` 时无法加载 dist 文件

sof*_*-03 5 webpack vue.js

之后,我npm run build产生了DIST文件,我运行index.html,所有的资源文件无法加载:

[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (main.1f51eed1059b282ae3ed.css, line 0)
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (main.1f51eed1059b282ae3ed.js, line 0)
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (vendors.1f51eed1059b282ae3ed.js, line 0)
[Error] Did not load script at 'http://localhost:63342/dist/vendors.1f51eed1059b282ae3ed.js' because non script MIME types are not allowed when 'X-Content-Type: nosniff' is given.
[Error] Did not load script at 'http://localhost:63342/dist/main.1f51eed1059b282ae3ed.js' because non script MIME types are not allowed when 'X-Content-Type: nosniff' is given.
Run Code Online (Sandbox Code Playgroud)

enter image description here


我的 dist 文件:

我的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')
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

我的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.html',
      template: './src/template/index.ejs',
      inject: false
    })
  ]
});
Run Code Online (Sandbox Code Playgroud)

不知道是不是配置的原因,谁能告诉我为什么我index.html在浏览器中运行dist时无法加载dist文件?


浏览器中的网址:

http://localhost:63342/wx_backup/dist/index.html?_ijt=gutju68tikagu6rldhshoenmrv
Run Code Online (Sandbox Code Playgroud)

我也试过:

http://localhost:63342/wx_backup/dist/index.html
Run Code Online (Sandbox Code Playgroud)

Las*_*nen 0

您的文件夹结构与网址不匹配。

在这种情况下,您应该能够通过以下 url 访问 js 包:

http://localhost:63342/wx_backup/dist/vendors.1f51eed1059b282ae3ed.js
Run Code Online (Sandbox Code Playgroud)

您的index.html 文件中的url 缺少文件夹结构的一部分,因此无法找到它。

无需更改任何配置即可解决该问题。这只是意味着您需要更改文件的文件夹结构。js 包需要位于相对于 index.html 文件的 dist 文件夹中。如果您有以下文件夹结构并在此处运行您的网络服务器,它将按预期工作。

index.html
dist
    js bundles here
Run Code Online (Sandbox Code Playgroud)