gpt4 book ai didi

javascript - *.default 不是构造函数,带有导入的js插件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:49 27 4
gpt4 key购买 nike

我尝试创建一个简单的表单验证并通过示例项目中的 yarn 链接注册它以测试设置。但这绝对行不通,我不知道如何继续。

export default class Proofr {
constructor() {
console.log('test');
}
}

此“脚本”随后由 webpack 使用 eslint-loaderbabel-loader 生成(预设 env 和 stage-0) .

所以它不应该做更多的事情,而不仅仅是为了记录消息的乐趣。但是当我尝试调用 new Proofr() 时,它告诉我以下内容:

 form.js:25 Uncaught TypeError: _proofr2.default is not a constructor

但如果它不是构造函数,那它又是什么?它是一个空对象,至少 crome 开发工具是这么说的。我导入了 proofr,它通过 yarn link 与我在 js 文件中的项目链接。

import Proofr from 'proofr';

import PinguComponent from '../../assets/js/helpers/PinguComponent';

class Form extends PinguComponent {
constructor(el) {
const defaultOpts = {
classes: {
dom: {},
state: {},
},
customEvents: {},
};

const defaultData = {};

super(el, defaultOpts, defaultData);

new Proofr();

this.log('Form loaded');
}
}

export default Form;

据我所知,也许有人看到了最有可能非常小的拼写错误。两个存储库使用相同的 babelrc,因此它们使用相同的预设。

这里是 webpack 和 babel 为 proofr 生成的文件

/******/ (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__) {

"use strict";


Object.defineProperty(exports, "__esModule", {
value: true
});

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
* Proofr a lightweight js tool
*/

var Proofr = function Proofr() {
_classCallCheck(this, Proofr);

console.log('test');
};

exports.default = Proofr;

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

如果您想自己查看,可以通过 npm install proofr(或 yarn proofr)添加它

最佳答案

通过将 output.libraryTarget 设置为 umd 来更改 proofr 库的 webpack 构建配置:

output: {
filename: '[name]',
path: path.resolve(__dirname, '../dist'),
libraryTarget: "umd"
},

参见 https://webpack.js.org/configuration/output/#output-librarytarget更多选项。

我通过使用 npm run start 重建 proofr 并将其导入 node.js 脚本来测试它:

var proofr = require('proofr');
console.log(proofr);

没有 output.libraryTarget: "umd":

$ node index.js
{}

有了它:

$ node index.js
{ default: [Function: Proofr] }

关于javascript - *.default 不是构造函数,带有导入的js插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47219943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com