繁体   English   中英

将 CommonJS 'module.exports = function()' 迁移到 ESM

[英]Migrating a CommonJS 'module.exports = function()' to ESM

我正在使用 NodeJS,并且已经编写 JavaScript 几年了,并且仍在学习。

对于我的 CJS 模块,我编写(我称之为)一个包含所有(我称之为)我的子函数的根函数,然后在我想要公开的函数的根函数上return {subfunction1, subfunction2} 不可否认,我从Jonathan Mills那里学到了这种写作风格,并且对它很满意。

我正在努力解决如何将其从 CommonJS 正确迁移到 ESM 并且希望在不使用Class的情况下这样做。 但是,如果Class是 ESM 的正确方式,那么我会适应。

这是一个 CJS 服务:

service.js

function WebexService(webex) {
  async function processMessage(messageData) {
    try {
      const user = await webex.people.get(messageData.personId);
      //debug(user);
      sendMessage({ displayName: user.displayName, roomId: messageData.roomId });
    } catch (error) {
      debug(error);
      throw error;
    }
  }
  function sendMessage(messageInfo) {
    webex.messages.create({
      roomId: messageInfo.roomId,
      text: `Howdy! ${messageInfo.displayName}`,
    });
  }
  return { processMessage }
}

module.exports = WebexService()

要使用此 CJS 服务,我会将其导入为:

app.js

const { processMessage } = require('../services/webexService');

function superCool() {
  const messageResponse = await processMessage(messageData);
}

我能够让它与 ESM 一起工作的唯一方法是作为一个Class

service.js

import debugInit from 'debug';
import chalk from 'chalk';
const debug = debugInit('app:services:webex');

export default class WebexService {
  constructor(webex) {
    this.webex = webex;
  }
  async processMessage(messageData) {
    try {
      const user = await this.webex.people.get(messageData.personId);
      //debug(user);
      this.sendMessage({ displayName: user.displayName, roomId: messageData.roomId });
    } catch (error) {
      debug(error);
      throw error;
    }
  }
  sendMessage(messageInfo) {
    this.webex.messages.create({
      roomId: messageInfo.roomId,
      text: `Howdy! ${messageInfo.displayName}`,
    });
  }
}

app.js

import WebexService from '../services/webex.js';

const WebexServiceInstance = new WebexService(webex);
WebexServiceInstance.processMessage(event.data);

我希望有人能指出我正确的方向。 如果有人可以帮助我找到一个可以阅读的内容,我很高兴 RTFM。

service.js

function WebexService(webex) {
  async function processMessage(messageData) {
    try {
      const user = await webex.people.get(messageData.personId);
      //debug(user);
      sendMessage({ displayName: user.displayName, roomId: messageData.roomId });
    } catch (error) {
      debug(error);
      throw error;
    }
  }
  function sendMessage(messageInfo) {
    webex.messages.create({
      roomId: messageInfo.roomId,
      text: `Howdy! ${messageInfo.displayName}`,
    });
  }
  return { processMessage }
}

const service = WebexService()
export default service

app.js

import WebexService from '../services/webex.js';
// you can then do this
const { processMessage } = WebexService;
function superCool() {
  const messageResponse = await processMessage(messageData);
}
// or just use it directly
function superCool() {
  const messageResponse = await WebexService.processMessage(messageData);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM