【问题标题】:ES6 (babel+webpack) Silent errors in the class methodsES6 (babel+webpack) 类方法中的静默错误
【发布时间】:2015-10-27 16:56:13
【问题描述】:

问题

我使用 webpack 包来定位和转译 ES6。

我想在没有 try/catch 块的情况下捕获错误。我怎样才能得到这个,是什么原因使异常保持沉默?这个问题不仅出现在 constructor() 方法中。类的任何方法都会默默地出现这样的错误

import React from 'react';
class TestComponent extends React.Component {
    constructor(props) {
        super(props);
        /*
            Try/catch block works fine.
            "test is not defined(…)" 
        */
        try {
            test.fake(1);
        } catch (e) {
            console.error(e);
        }
        // Line below should theoretically throw exception but it does not 
        test.fake(1);
    }
    render() {
        return <div>TEST</div>
    }
}
export default TestComponent;   

这是我的 webpack.config

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'cheap-module-eval-source-map',
    //devtool: 'eval',
    entry: [
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
        './src/js/main'
    ],
    output: {
        filename: 'app.js',
        path: path.join(__dirname, 'dist'),
        publicPath: '/assets/'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new ExtractTextPlugin('app.css', {
            allChunks: true
        }),
        new webpack.DefinePlugin({
            __DEV_TOOLS__: true
        }),
        new HtmlWebpackPlugin({
            title: 'Redux Boilerplate',
            filename: 'index.html',
            template: 'index.template.html'
        })
    ],
    resolve: {
        root: path.resolve('./src/js')
    },
    module: {
        loaders: [
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass')
            }, {
                test: /\.js$/,
                loaders: ['babel'],
                exclude: /node_modules/
            }
        ]
    },
    cssnext: {
        browsers: 'last 2 versions'
    }
};

【问题讨论】:

    标签: javascript debugging ecmascript-6 webpack babeljs


    【解决方案1】:

    Webpack transpile 不会编译你的代码。当您对上述内容进行转码时,不会出现语法问题或缺少要求,因此 webpack 将简单地通过 babel 运行该代码并转码到 EC5。您可能想要寻找在转译后实际执行代码的加载器或插件。更好的是,将 webpack 与 gulp 或 karma 结合起来,得到一个完整的解决方案。我们使用这样的东西。 https://github.com/webpack/karma-webpack

    【讨论】:

    • 感谢您的回复!我只是想知道为什么我的片段的转译代码不会在控制台窗口中显示错误。事实上,如果我尝试在 (babeljs.io/repl/…) 中运行这个片段,一切正常。
    • 也许删除?新的 webpack.NoErrorsPlugin()。不太熟悉 webpack-dev-server 的内部结构
    猜你喜欢
    • 2017-01-24
    • 2020-04-07
    • 1970-01-01
    • 2018-03-11
    • 2017-11-13
    • 1970-01-01
    • 2017-06-09
    • 2016-02-21
    相关资源
    最近更新 更多