Webpack console.log 输出?

问题描述 投票:0回答:3

任何人都可以指导我正确的方向吗?

所以我用 truffle 套件演示设置了 webpack-dev-server,只是为了在我的应用程序基础上打下基础。所以我的配置文件包含 index.html 和 app.js,但它尝试显示 console.log 输出到 app.js 没有通过控制台显示?

webpack.config.js

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports =
  {
  entry: './app/javascripts/app.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'app.js',
  },
  plugins: [
    // Copy our app's index.html to the build folder.
    new CopyWebpackPlugin([
      { from: './app/index.html', to: "index.html" }
    ])
  ],
  module: {
    rules: [
      {
       test: /\.css$/,
       use: [ 'style-loader', 'css-loader' ]
      }
    ],
    loaders: [
      { test: /\.json$/, use: 'json-loader' },
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015'],
          plugins: ['transform-runtime']
        }
      }
    ]
  },
devServer: {
        compress: true,
        disableHostCheck: true,   // That solved .
        quiet: false,
        noInfo: false,
stats: {
  // Config for minimal console.log mess.
  colors: true,
  version: false,
  hash: false,
  timings: false,
  chunks: false,
  chunkModules: false

        }
 }
}

app.js

// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'

// Import our contract artifacts and turn them into usable abstractions.
import metacoin_artifacts from '../../build/contracts/MetaCoin.json'
import dextre_artifacts from '../../build/contracts/Dextre.json'

console.log("starting!");

运行webpack时的输出

Project is running at http://localhost:8080/
webpack output is served from /
     Asset     Size  Chunks                    Chunk Names
    app.js  1.93 MB       0  [emitted]  [big]  main
index.html  19.8 kB          [emitted]         
webpack: Compiled successfully.

哪里可以看到“首发!”输出,这是一个真正的烦恼,因为我需要解决错误。我试过在 http://localhost:8080//http://localhost:8080/webpack-dev-server// 上查看,但没有运气。

javascript webpack output webpack-dev-server console.log
3个回答
3
投票

我有同样的问题。据我所知,问题是 Webpack 实际上并没有在服务器上运行生成的代码。在服务器上运行的进程只是检查文件更改并在必要时重新编译。代码其实都是在客户端运行的。因此,查看 console.log() 输出的唯一方法是查看客户端浏览器的控制台。

这里的部分混淆是,虽然正常情况下,node 在服务器上运行 javascript,但在这种情况下,node 完全委托给一个单独的程序。您可以在根本不安装节点的情况下运行整个过程,只需使用 webpack 可执行文件的独立安装即可。从未使用过 Node 的执行环境,因此您无法登录它。


1
投票

由于您正在使用

webpack-dev-server
,因此
console.log
将被打印到浏览器控制台中。如果您想在终端中看到
console.log
,您可以在
console.log
文件中添加一个
webpack.config

如果您没有提供

webpack-dev-server
端口,您的应用程序应该在 80 上运行,因此在那里打开浏览器并打开浏览器控制台,您应该能够看到
 "starting!
日志。


0
投票

好问题,这取决于您使用的优化器和捆绑器引擎。

TerserJS(很可能是最新的方法):

optimization: {
  minimizer: [
    new TerserPlugin({
      terserOptions: {
        compress: {
            drop_console: true
        }
      }
    })
  ]
}

旧方法 uglifyJsPlugin:

// under plugins
            new webpack.optimize.UglifyJsPlugin({

                // Eliminate comments
                   comments: false,
           
               // Compression specific options
                  compress: {
                    // remove warnings
                       warnings: false,
           
                    // Drop console statements
                       drop_console: false
                  },
               }),

或者优化webpack v4下的uglify:

const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
// load the plugin. its not part of webpack

optimization: {
  minimizer: [
    new UglifyJSPlugin({
      uglifyOptions: {
        compress: {
          drop_console: true,
        }
      }
    })
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.