首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack生产建设不装载任何东西。

Webpack生产建设不装载任何东西。
EN

Stack Overflow用户
提问于 2016-01-21 00:09:55
回答 1查看 7.5K关注 0票数 3

我已经用React和Webpack做了一段时间的应用程序。我的开发环境工作得很好,所有的东西都使用webpack开发服务器正确加载.

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

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

图片和HTML拷贝在我的src (dev)文件夹中,但是我的JS包输出非常小--文件(main.js)只有246个字节。

这是运行webpack -p的输出

代码语言:javascript
复制
$ 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

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

代码语言:javascript
复制
$ 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文件:

代码语言:javascript
复制
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'
            }
        ]
    }
}

我不认为我有一个特别复杂的,或不寻常的设置,我已经尝试改变一切从as 2015/ES6到commonJS,有同样的结果。

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

谢谢你抽出时间阅读所有的东西!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-24 21:28:31

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

通常,您将使用Webpack来打包javascript、css,有时图片(即:“资产”)将由html文件使用。提供HTML文件不属于Webpack的职责范围。

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34913095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档