Webpack 生产构建不加载任何东西

Webpack production build does not load anything

我一直在使用 React 和 Webpack 开发一个应用程序有一段时间了。我的开发环境运行良好,使用 webpack-dev-server 可以正确加载所有内容。

我决定 运行 应用程序的生产构建,以查看最终产品的大小,并观察 webpack 产品构建的一般输出。

事实证明,运行ning webpack -p 虽然确实会产生输出(稍后会详细介绍),但当我在浏览器中访问该站点时,它根本不会加载任何内容。 . 对输出的快速检查告诉我 none 的组件代码正在进入 webpack -p 构建。

图像和 HTML 复制过来,因为它们存在于我的 src (dev) 文件夹中,但是我的 JS 包输出非常小 - 文件 (main.js) 只有 246 字节。

这是 运行ning webpack -p

的输出
$ npm run build

> project@0.1.0 build /Users/me/Development/project
> NODE_ENV=production webpack -p --bail --progress --config webpack.config.babel.js

Hash: 9e5f6974ce21c920a375
Version: webpack 1.12.10
Time: 2003ms
            Asset       Size  Chunks             Chunk Names
       index.html    1.45 kB          [emitted]
  images/edit.svg  524 bytes          [emitted]
images/search.svg    1.19 kB          [emitted]
          main.js  246 bytes    0, 1  [emitted]  javascript, html
    + 219 hidden modules

当我 运行 项目的开发版本时,输出明显不同......我知道开发服务器依赖项在那里,代码没有缩小......而且,最重要的是- 运行连接开发服务器时,一切都按预期工作。

$ npm start

> project@0.1.0 start /Users/me/Development/project
> webpack-dev-server --hot --display-modules --config webpack.config.babel.js

http://localhost:3333/
webpack result is served from /
content is served from /Users/me/Development/project/dist
Hash: 1b34ed58f9e323966ada
Version: webpack 1.12.10
Time: 2745ms
            Asset       Size  Chunks             Chunk Names
       index.html    1.45 kB          [emitted]
  images/edit.svg  524 bytes          [emitted]
images/search.svg    1.19 kB          [emitted]
          main.js    1.54 MB    0, 1  [emitted]  html, javascript

这是我的 webpack.config.babel.js 文件:

import webpack from 'webpack';
import path from 'path';

import ModernizrWebpackPlugin from 'modernizr-webpack-plugin';
import modernizrConfig from './modernizr.config';

const appDir = path.resolve(__dirname, './src');
const distDir = path.resolve(__dirname, './dist');
const nodeModulesDir = path.resolve(__dirname, './node_modules');

const excludeDirs = /(node_modules|bower_components)/;

module.exports = {
    entry: {
        javascript: appDir + '/main.js',
        html: appDir + '/index.html'
    },
    output: {
        path: distDir,
        filename: 'main.js',
    },
    devServer: {
        contentBase: distDir,
        inline: true,
        port: 3333
    },
    resolve: {
        extensions: ['', '.js', '.es6'],
        modulesDirectories: [
            'node_modules',
            './src'
        ]
    },
    plugins: [
        // new webpack.optimize.CommonsChunkPlugin('common.js'),
        // new ModernizrWebpackPlugin(modernizrConfig),
    ],
    sassLoader: {
        sourceMap: false,
        includePaths: [
            appDir,
            nodeModulesDir,
            nodeModulesDir + '/breakpoint-sass/stylesheets/',
            nodeModulesDir + '/susy/sass'
        ]
    },
    module: {
        loaders: [
            { // js/jsx
                test: /\.js?$/,
                exclude: excludeDirs,
                loader: 'babel',
                query: {
                    cacheDirectory: true,
                    presets: [
                        'es2015', 'react'
                    ]
                }
            },
            { // html
                test: /\.html$/,
                exclude: excludeDirs,
                loader: 'file?name=[name].[ext]'
            },
            { // images
                test: /\.(gif|png|jpg|jpeg|svg)$/,
                exclude: excludeDirs,
                loader: 'file?name=images/[name].[ext]'
            },
            { // sass
                test: /\.scss$/,
                exclude: excludeDirs,
                loader: 'style!css!sass'
            }
        ]
    }
}

我认为我没有特别复杂或不常见的设置,而且我已经尝试将所有内容从 es2015/es6 更改为 commonJS,结果相同。

我不知道这里可能有什么问题;希望有人能指出我遇到的一些明显错误,或者建议可以解决此问题的配置 updates/changes。

感谢您花时间阅读所有内容!

正如我在评论中提到的,我已经成功使用 webpack 几个月了,而且我从未遇到过使用 HTML 文件作为入口点的情况。

通常你会使用 Webpack 来打包 javascript, css, 有时图像 (ie: "assets") 被 [=18] 使用=] 一个 html 文件。提供 HTML 文件不在 Webpack 的职责范围内。

我建议使用 Webpack 仅生成 最终的 javascript 包,然后使用其他方法(即:express 或其他网络服务器)来提供服务该文件和使用它的 html。