我已经用React和Webpack做了一段时间的应用程序。我的开发环境工作得很好,所有的东西都使用webpack开发服务器正确加载.
我决定运行应用程序的生产构建,以查看最终产品的大小,并观察webpack产品构建的一般输出。
结果是,运行webpack -p,虽然它确实会产生输出(稍后会详细介绍),但当我在浏览器中访问该站点时,它根本不会加载任何内容。对输出的快速检查告诉我,我的任何组件代码都没有进入webpack -p构建。
图片和HTML拷贝在我的src (dev)文件夹中,但是我的JS包输出非常小--文件(main.js)只有246个字节。
这是运行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当我运行项目的开发版本时,输出明显不同.我知道开发服务器依赖项就在那里,并且代码没有缩小..而且,最重要的是,在运行dev服务器时,一切都按预期工作。
$ 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'
}
]
}
}我不认为我有一个特别复杂的,或不寻常的设置,我已经尝试改变一切从as 2015/ES6到commonJS,有同样的结果。
我不知道这里可能会出现什么问题;希望有人能指出我遇到的一些明显的错误,或者建议可以解决这个问题的配置更新/更改。
谢谢你抽出时间阅读所有的东西!
发布于 2016-01-24 21:28:31
正如我在我的评论中提到的,我已经成功地使用webpack几个月了,而且我从未见过使用HTML文件作为入口点。
通常,您将使用Webpack来打包javascript、css,有时图片(即:“资产”)将由html文件使用。提供HTML文件不属于Webpack的职责范围。
我建议使用Webpack只生成最终的javascript包,然后使用其他方法(即: express或其他web服务器)来服务该文件和使用该文件的html。
https://stackoverflow.com/questions/34913095
复制相似问题