ng-packagr给出了没有为外部模块提供的名称

时间:2018-02-05 05:18:44

标签: angular ng-packagr

使用ng-packagr打包一个项目@ my / common-util时,没有问题。该类在abstract-person.ts中包含一个名为AbstractPerson的抽象类。

在另一个名为@ my / common-impl的项目中,创建了另一个Person类,它扩展了AbstractPerson并使用@ my / common-util包名称导入它。当我使用ng-packagr打包它时,我收到以下错误 - >

没有为外部模块提供名称' @ my / common / abstract-person'在options.globals中 - 猜测' abstractPerson'

因为它似乎是一个警告,我继续npm将@ my / common和@ my / common-impl安装到另一个项目中,但是当我从@ my / common-impl导入Person类时出现以下错误

  错误在./node_modules/@my/common-impl/esm5/common-impl.js模块没有   发现:错误:无法解决' @ my / common / abst ract-person'在   ' C:\ Data \ me \ node_modules \ @my \ common-impl \ e sm5'解决   ' @我的/通用/抽象的人'在   ' C:\ DATA \我\ node_modules \ @my \共IMPL \ esm5'解析请求是一个   使用描述文件的模块:C:\ Data \ me \ node_modules \ @my \ co   mmon-impl \ package.json(相对路径:./ esm5)字段'浏览器'   使用说明后,不包含有效的别名配置   file:C:\ Data \ me \ node_modules \ @my \ common-impl \ package.json(相对   路径:./ esm5)解析为模块

我在package.json中尝试了外部,全局,umdModuleIds之类的几个东西(见下文),但这些都没有用。

这是package.json

{
  "name": "@my/common-impl",
  "version": "1.0.0-alpha.0",
  "private": true,
  "dependencies": {
    "@my/common": "1.0.0-alpha.0"
  },
  "peerDependencies": {
    "lodash": "^4.17.4"
  },
  "ngPackage": {
    "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
    "dest": "dist/common-impl",
    "workingDirectory": "../.ng_build",
    "lib": {
      "entryFile": "src/public_api.ts",
      "externals": [
        "@my/common/abstract-person" 
      ],
      "globals": {
        "@my/common/abstract-person": "AbstractPerson"
      },
      "umdModuleIds": {
        "abstract-person" : "AbstractPerson"

      }
    }
  }
}

需要进一步纠正这个问题吗?

3 个答案:

答案 0 :(得分:14)

我正在使用一个名为 katex 的npm模块。将其添加到 ./ projects / myLibname / ng-package.json 中的umdModuleIds对我有用。

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/documentations",
  "lib": {
    "entryFile": "src/public_api.ts",
    "umdModuleIds": {
      "katex": "katex"
    }
  }
}

这使以下警告消失了

  

在output.globals中没有为外部模块'katex'提供名称-猜测'katex'

答案 1 :(得分:2)

你应该使用"路径"您图书馆中class ProductForm(forms.ModelForm): @staticmethod def parse_filter_kwargs(**kwargs): if 'initial' in kwargs: if u'_changelist_filters' in kwargs['initial']: filters = kwargs['initial'].pop('_changelist_filters') for key, value in [p.split('=') for p in filters.split('&')]: kwargs['initial'][key] = value return kwargs def __init__(self, *args, **kwargs): kwargs = self.parse_filter_kwargs(**kwargs) super(ProductForm, self).__init__(*args, **kwargs) 的属性。

这就是问题所在:

我将我的library1和library2添加到我的monorepo项目文件夹中。

tsconfig.lib.json

project: -> library1 -> tsconfig.lib.json -> ... -> library2 -> tsconfig.lib.json -> ... -> tsconfig.json -> ...

tsconfig.json

我在library2代码中使用了{ ... "compilerOptions": { ... "paths": { "library1": [ "../dist/library1" ], "library2": [ "../dist/library2" ], }, } 。 解决import {lib1Module} from 'library1'错误我在library2 no module lib1Module found

中的ovveride library1路径

tsconfig.lib.json

tsconfig.lib.json

如果你使用{ "extends": "../../tsconfig.json", "compilerOptions": { ... "paths": { "library1": [ "../../../dist/library1" ] }, ... }, 属性代替" umdModuleIds"," globals"你的问题可能会解决。和"外部"

最后我应该提到这个事实," externals" ng-packagr schema中不再存在。

答案 2 :(得分:1)

对于我来说,我是通过这种方式解决警告的。

ng-package.json

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/test-lib",
  "lib": {
    "entryFile": "src/public-api.ts",
    "umdModuleIds": {
      "@libs/shared": "../../dist/shared",
      "@libs/animations": "../../dist/animations",
      "saturn-datepicker": "saturn-datepicker",
      "moment": "moment"
    }
  }
}

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "es2019",
      "dom"
    ],
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "paths": {
      "@app/*": [ "projects/test-app/src/app/*" ],
      "@libs/*": [ "dist/*" ],
      "shared": [ "dist/shared" ],
      "shared/*": [ "dist/shared/*" ],
      "animations": [ "dist/animations" ],
      "animations/*": [ "dist/animations/*" ],
    }
  }
}