jackieli123723 / jackieli123723.github.io

✅lilidong 个人博客
9 stars 0 forks source link

koa-cookie esm问题 #86

Open jackieli123723 opened 2 years ago

jackieli123723 commented 2 years ago
import Koa from 'koa'; 
import cookie from 'koa-cookie';
const app = Koa();
app.use(cookie());

报错

Hint: hit control+c anytime to enter REPL. file:///home/runner/import-koa/index.js:11 app.use(cookie()); ^

TypeError: cookie is not a function at file:///home/runner/import-koa/index.js:11:9

卧槽 这个是什么操作分析 是esm 不支持

koa-cookie源码

'use strict';

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

function cookie() {
  return (() => {
    var _ref = _asyncToGenerator(function* (context, next) {
      const cookieHeader = context.headers.cookie;
      if (cookieHeader) {
        const cookies = cookieHeader.split(';');
        context.cookie = {};
        cookies.forEach(function (item) {
          const crumbs = item.split('=');
          if (crumbs.length > 1) context.cookie[crumbs[0].trim()] = crumbs[1].trim();
        });
      }
      yield next();
    });

    return function (_x, _x2) {
      return _ref.apply(this, arguments);
    };
  })();
}

exports.default = cookie;

修复1删除Object.defineProperty(exports, "__esModule", { value: true}); && exports.default = cookie;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

function cookie() {
  return (() => {
    var _ref = _asyncToGenerator(function* (context, next) {
      const cookieHeader = context.headers.cookie;
      if (cookieHeader) {
        const cookies = cookieHeader.split(';');
        context.cookie = {};
        cookies.forEach(function (item) {
          const crumbs = item.split('=');
          if (crumbs.length > 1) context.cookie[crumbs[0].trim()] = crumbs[1].trim();
        });
      }
      yield next();
    });

    return function (_x, _x2) {
      return _ref.apply(this, arguments);
    };
  })();
}

// way1 ok esm fix 参考 ts-sort-utils 
export { cookie }

使用方式

import {cookie} from './middleware/koa-cookie.js';
const app = new Koa();
app.use(cookie());

修复方式2 way1 改为 //WAY2 export default cookie;

使用方式

//way2
import cookie from './middleware/koa-cookie.js';
const app = new Koa();
app.use(cookie());

修复方式3 包装下koa-cookie2.js import cookie from "koa-cookie"; export default cookie.default;

使用方式

//way3
import cookie from './middleware/koa-cookie2.js';

const app = new Koa();
app.use(cookie());

修复方式4 使用方式

import cookie from 'koa-cookie';
const app = new Koa();
app.use(cookie.default());