【发布时间】:2018-06-01 04:01:44
【问题描述】:
在尝试调试更大的问题时,我想出了一个准系统模块和 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 配置有什么问题吗?
编辑:我发现为了使其正常工作,我必须将类包装在一个对象中。我测试了一些其他数据类型(导出函数、数字等),看来您必须导出一个对象才能使其正常工作。
【问题讨论】:
-
你是在浏览器还是在节点引擎中运行你的包?
-
如果您仅使用 Node.js 运行此代码,则无需通过 Webpack 转译运行上述代码。 Node 已经支持 ES6 类,因为 v4.8.7
-
@ChrisR 为了测试,我在 node 中运行它,但稍后我将为 node 和浏览器提供 2 个单独的包。
-
@peteb 我意识到 - 稍后,我将使用它来捆绑浏览器的依赖项。我刚刚遇到了这个类问题,并将这个测试用例作为可重现的示例。