gpt4 book ai didi

import - deno 从 deno.land/x 导入 lodash

转载 作者:行者123 更新时间:2023-12-03 23:33:38 36 4
gpt4 key购买 nike

我无法在 Deno 程序中找到正确的语法/URL 来导入 Lodash。
我试过这个:

import _ from 'https://deno.land/x/lodash@4.17.19/lodash.js';
这给出了错误“未捕获的语法错误:请求的模块 'https://deno.land/x/lodash@4.17.19/lodash.js' 不提供名为 'default' 的导出”。
然后我尝试了这个:
import * as lodash from 'https://deno.land/x/lodash@4.17.19/lodash.js';
这似乎给了我一个空的模块对象。
我想看一个从 Deno 程序访问 Lodash 任何功能的示例。

最佳答案

所以经过一点实验,我找到了两种导入lodash的方法进入 deno环境。
1. 把 deno 当作一个普通的浏览器环境

import "https://deno.land/x/lodash@4.17.19/dist/lodash.js";

// now `_` is imported in the global variable, which in deno is `self`
const _ = (self as any)._;

console.log(_.chunk([1, 2, 3], 2)); // --> [ [ 1, 2 ], [ 3 ] ]
2. deno 中的假节点功能
在 deno std 中,有一个节点模块:

This module is meant to have a compatibility layer for the NodeJS standard library.


import { createRequire } from "https://deno.land/std@0.86.0/node/module.ts";

const require = createRequire(import.meta.url);

// seems require don't known how to import file from web,
// so you must download it in your local filesystem.
const _ = require("./lodash.js");

console.log(_.chunk([1, 2, 3], 2)); // --> [ [ 1, 2 ], [ 3 ] ]
添加类型信息
这是我发现的,有点麻烦。
  • https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/lodash 下载类型信息
  • 编辑来自 xxx 的每个导入至xxx.d.ts .例如:在文件 common/common.d.ts :

  • // before
    import _ = required("../index");
    declare module "../index" {
    /* ... */

    // after
    import _ from "../index.d.ts";
    declare module "../index.d.ts" {
    /* ... */
  • 现在您可以导入类型信息并使用它

  • import type Lodash from "./types/lodash/index.d.ts";

    const _: typeof Lodash = (self as any)._;

    // now _ is typed
    a vscode screenshot to demonstrate type info

    以下是 lodash 源文件中的一些片段,我认为这与进口有关。
      // Detect which environment you're currently in:

    /** Detect free variable `global` from Node.js. */
    var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;

    /** Detect free variable `self`. */
    var freeSelf = typeof self == 'object' && self && self.Object === Object && self;

    /** Used as a reference to the global object. */
    var root = freeGlobal || freeSelf || Function('return this')();

    /** Detect free variable `exports`. */
    var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;

    /** Detect free variable `module`. */
    var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;

    /** Detect the popular CommonJS extension `module.exports`. */
    var moduleExports = freeModule && freeModule.exports === freeExports;

    /** Detect free variable `process` from Node.js. */
    var freeProcess = moduleExports && freeGlobal.process;

    // ...
      // And how `lodash` exports its functionality:

    // Some AMD build optimizers, like r.js, check for condition patterns like:
    if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
    // Expose Lodash on the global object to prevent errors when Lodash is
    // loaded by a script tag in the presence of an AMD loader.
    // See http://requirejs.org/docs/errors.html#mismatch for more details.
    // Use `_.noConflict` to remove Lodash from the global object.
    root._ = _;

    // Define as an anonymous module so, through path mapping, it can be
    // referenced as the "underscore" module.
    define(function() {
    return _;
    });
    }
    // Check for `exports` after `define` in case a build optimizer adds it.
    else if (freeModule) {
    // Export for Node.js.
    (freeModule.exports = _)._ = _;
    // Export for CommonJS support.
    freeExports._ = _;
    }
    else {
    // Export to the global object.
    root._ = _;
    }

    // ...

    关于import - deno 从 deno.land/x 导入 lodash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64979829/

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