可以控制统计的WebPack输出到终端通过一个统计设置对象的水平。这里是我的服务器应用程序的相关部分(节点+快递+的WebPack DEV)
if (process.env.NODE_ENV === 'development') {
// dynamically require webpack dependencies
// to them in devDependencies (package.json)
const webpackConfig = require('../../webpack/development.webpack.config')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const webpack = require('webpack')
const compiler = webpack(webpackConfig)
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: webpackConfig.stats,
progress: true,
hot: true
}))
app.use(webpackHotMiddleware(compiler))
} else {
// compression middleware compresses your server responses
// which makes them smaller (applies also to assets).
// You can read more about that technique and other good
// practices on official Express.js docs http://mxs.is/googmy
app.use(compression())
app.use(express.static(path.resolve(process.cwd(), './dist')))
}
在我的WebPack配置:
module.exports = {
devtool: 'source-map',
context: path.join(__dirname, '..'),
entry: {
bundle: [
'webpack-hot-middleware/client',
path.resolve('./src/client/index.js')
]
},
output: {
path: path.resolve(__dirname, '../dist'),
filename: "[name].js",
publicPath: '/'
},
stats: {
// Add asset Information
assets: true,
// Sort assets by a field
assetsSort: "field",
// Add information about cached (not built) modules
cached: true,
// Add children information
children: true,
// Add chunk information (setting this to `false` allows for a less verbose output)
chunks: false,
// Add built modules information to chunk information
chunkModules: true,
// Add the origins of chunks and chunk merging info
chunkOrigins: false,
// Sort the chunks by a field
chunksSort: "field",
// Context directory for request shortening
context: "../src/",
// `webpack --colors` equivalent
colors: true,
// Add errors
errors: true,
// Add details to errors (like resolving log)
errorDetails: true,
// Add the hash of the compilation
hash: false,
// Add built modules information
modules: false,
// Sort the modules by a field
modulesSort: "field",
// Add public path information
publicPath: false,
// Add information about the reasons why modules are included
reasons: false,
// Add the source code of modules
source: false,
// Add timing information
timings: true,
// Add webpack version information
version: true,
// Add warnings
warnings: false
},
// ... rest of config ...
对于生产版本,我使用的是这样的:
const webpack = require('webpack')
function webpackCompiler (webpackConfig) {
return new Promise((resolve, reject) => {
const compiler = webpack (webpackConfig)
compiler.run((err, stats) => {
if (err) {
console.log('Webpack compiler encountered a fatal error.', err)
return reject(err)
}
const jsonStats = stats.toJson()
console.log(stats.toString(webpackConfig.stats))
resolve(jsonStats)
})
})
}
module.exports = webpackCompiler
Webpack在终端中显示编译错误。你可以在编译后截取你的终端吗?很可能这是您的配置问题。 –
我已根据您的回答编辑了我的帖子 –
请发布您的完整webpack配置文件。 – kudlajz