警告:在output.globals中没有为外部模块'X'提供名称-猜测'X' [英] WARNING: No name was provided for external module 'X' in output.globals – guessing 'X'

查看:334
本文介绍了警告:在output.globals中没有为外部模块'X'提供名称-猜测'X'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WARNING: No name was provided for external module 'moment' in output.globals – guessing 'momentImported'
WARNING: No name was provided for external module 'odata-parser' in output.globals – guessing 'parser'

当我尝试将我的库绑定到通用模块定义时,我收到此消息.可以通过在ng-package.json中添加umdModuleIds来解决该警告.

I receive this message when I try to bundle my library to Universal Module Definition. The warning can be fixed by adding umdModuleIds in ng-package.json.

文档给我们以下解释:

在编写UMD软件包时,ng-packagr会尽力提供UMD模块标识符的通用默认值.另外,汇总会尽力猜测外部依赖项的模块ID.即使如此,您仍应确保外部依赖性是正确的.万一ng-packagr不提供默认值并且汇总无法猜测正确的标识符,您应该通过使用umdModuleIds显式提供模块标识符在库的打包文件部分中,如下所示:...

When writing the UMD bundle, ng-packagr does its best to provide common default values for the UMD module identifiers. Also, rollup will do its best to guess the module ID of an external dependency. Even then, you should make sure that the UMD module identifiers of the external dependencies are correct. In case ng-packagr doesn't provide a default and rollup is unable to guess the correct identifier, you should explicitly provide the module identifier by using umdModuleIds in the library's package file section like so: ...

umdModuleIds :

外部依赖关系及其对应的UMD模块的映射身份标识.映射键是TypeScript/EcmaScript模块标识符.映射值是UMD模块ID.这张地图的目的是为了正确捆绑UMD模块文件(带有 rollup ).默认情况下, rxjs tslib @angular/* 依赖项符号受支持.

A map of external dependencies and their correspondent UMD module identifiers. Map keys are TypeScript / EcmaScript module identifiers. Map values are UMD module ids. The purpose of this map is to correctly bundle an UMD module file (with rollup). By default, rxjs, tslib and @angular/* dependency symbols are supported.

如何查找矩量,odata-parser或必须添加到umdModuleIds的任何其他模块的UMD ID或正确性?

How can I find or check for correctness the UMD ID of moment, odata-parser or any other module that must be added to the umdModuleIds?

推荐答案

我发现文档也很难理解.ng-package.json 中的ng-packagr umdModuleIds 设置是导入名称到UMD在javascript global 对象中注册的模块ID的映射.

I found the docs hard to understand, too. The ng-packagr umdModuleIds setting in ng-package.json is a mapping of import names to module IDs that UMD registers in the javascript global object.

我的意思是在您的打字稿代码中输入名称:

By import name I mean, in your typescript code:

import * as moment from 'moment';

字符串 moment 应该是 umdModuleIds 下的键.

该值应与在UMD软件包中注册的全局变量相匹配.如果您查看要导入的JS文件,则会看到设置了 global.X 值.对于 moment.js ,代码块为:

The value should match up with the global variable registered in the UMD bundle for the package. If you take a look in the JS file you're importing, you'll see the global.X value being set. For moment.js the code block is:

//! moment.js

;(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    global.moment = factory()
}(this, (function () { 'use strict';

global.moment = ... 行为您提供了所需的内容.因此,为了正确导入时刻,您的 ng-package.json 应该包括:

The line global.moment = ... gives you what you need. So, to import moment correctly, your ng-package.json should include:

  "lib": {
    "entryFile": "src/public-api.ts",
    "umdModuleIds": {
      "moment": "moment"
    }
  }

在这种情况下,事实证明ng-packagr的猜测是正确的-导入名称与UMD全局变量匹配,但是您需要明确指定它,以便ng-packagr知道.

In this case, it turns out that ng-packagr's guess is correct - the import name matches the UMD global variable, but you need to specify it explicitly for ng-packagr to know for sure.

应该有另一种(更好的)方式可以解决此问题,以解决NPM依赖项,即将库依赖项添加到库 package.json 文件(同一目录中的package.json).dir作为ng-package.json文件).我会首先尝试-我相信ng-packagr在package.json中包含依赖项时会正确找到UMD模块ID.

There should be another (and better) way that you can fix this for NPM dependencies, which is to add the library dependencies to the library package.json file (the package.json in the same dir as the ng-package.json file). I would try that first - I believe ng-packagr correctly finds the UMD module ids when the dependency is included in the package.json.

但是,如果您使用的是本地库(相同角度工作区中的库),或者使用ng-packagr无法分析的库,则必须查看UMD id并确保它们配对.例如,如果您为内部库使用范围/命名空间名称,例如 @ mycompany/util ,则会看到UMD模块ID的注册方式如下:

However, in the case that you're using local libraries (libraries in the same angular workspace), or libraries which otherwise can't be analyzed by ng-packagr, it's necessary to look at the UMD ids and make sure they match up. For example, if you're using scoped/namespaced names for your internal libs, like @mycompany/util, you'll see that the UMD module IDs are registered like this:

文件:〜/dist/util/bundles/mycompany-util.umd.js

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define('@mycompany/util', ['exports'], factory) :
    (global = global || self, factory((global.mycompany = global.mycompany || {}, global.mycompany.util = {})));
}(this, (function (exports) { 'use strict';

因此,给定行 global.mycompany.util = ,您需要指定UMD模块ID,例如:

so given the line global.mycompany.util = you'll need to specify UMD module IDs like:

    "umdModuleIds": {
      "@mycompany/util": "mycompany.util"
      ...
    }

这篇关于警告:在output.globals中没有为外部模块'X'提供名称-猜测'X'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆