最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Webpack没有正确导出ES6类?

IT培训 admin 4浏览 0评论

Webpack没有正确导出ES6类?

在尝试调试更大的问题时,我想出了一个准系统模块和Webpack配置,似乎不正确地导出ES6类。

模块(index.js):

module.exports = class Foo {
    constructor() {
        console.log('foo!');
    }
}

webpack.config.js

module.exports = {
    target: 'node',
    entry: './index.js',
    output: {
        filename: 'bundle.js'
    }
};

输出(bundle.js)是:

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(1);


/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


module.exports = class Foo {
    constructor() {
        console.log('foo!');
    }
}


/***/ })
/******/ ]);

当我尝试使用捆绑包中的Foo类时,我收到此错误:

TypeError: Foo is not a constructor

它使用源代码很好,但不是捆绑版本。 Webpack配置有什么问题吗?

编辑:我发现为了使它正常工作,我必须将类包装在一个对象中。我测试了一些其他数据类型(导出函数,数字等),似乎必须导出一个对象才能正常工作。

回答如下:

只需将此添加到Webpack配置:

output: {
    libraryTarget: 'umd'
}

Webpack没有正确导出ES6类?

在尝试调试更大的问题时,我想出了一个准系统模块和Webpack配置,似乎不正确地导出ES6类。

模块(index.js):

module.exports = class Foo {
    constructor() {
        console.log('foo!');
    }
}

webpack.config.js

module.exports = {
    target: 'node',
    entry: './index.js',
    output: {
        filename: 'bundle.js'
    }
};

输出(bundle.js)是:

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(1);


/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


module.exports = class Foo {
    constructor() {
        console.log('foo!');
    }
}


/***/ })
/******/ ]);

当我尝试使用捆绑包中的Foo类时,我收到此错误:

TypeError: Foo is not a constructor

它使用源代码很好,但不是捆绑版本。 Webpack配置有什么问题吗?

编辑:我发现为了使它正常工作,我必须将类包装在一个对象中。我测试了一些其他数据类型(导出函数,数字等),似乎必须导出一个对象才能正常工作。

回答如下:

只需将此添加到Webpack配置:

output: {
    libraryTarget: 'umd'
}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论