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

2024-05-11

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

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

然后生成这个“脚本”webpack, with eslint-loader and babel-loader(预设 env 和 stage-0)。

所以它不应该仅仅为了有趣地记录消息而做更多的事情。但当我尝试打电话时new Proofr()它告诉我以下内容:

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

但如果它不是构造函数那么它是什么?它是一个空对象,至少 crome 开发工具是这么说的。 我导入通过纱线链接与我的项目在 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 生成的用于校样的文件

/******/ (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 installproofr(或yarnproofr)添加它


更改您的 webpack 构建配置proofr通过设置库output.libraryTarget to umd:

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

See https://webpack.js.org/configuration/output/#output-librarytarget https://webpack.js.org/configuration/output/#output-librarytarget以获得更多选择。

我通过重建校样器来测试这一点npm run start并将其导入到 Node.js 脚本中:

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

Without output.libraryTarget: "umd":

$ node index.js
{}

With it:

$ node index.js
{ default: [Function: Proofr] }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

*.default不是构造函数,带有导入的js插件 的相关文章

随机推荐