【问题标题】:Error building .js files with ng build on lib在 lib 上使用 ng build 构建 .js 文件时出错
【发布时间】:2019-12-12 19:41:42
【问题描述】:

我有一个应用程序,我正在尝试将其转换为使用 angular cli (v7)。该应用程序运行正常。

该应用由 2 个站点和一个公共库组成:

- ui-site-1
- ui-site-2
- ui-lib

我正在使用 ng build 来构建和打包 UI-lib,但是在 FESM2015 阶段存在指向 public_api.ts 的错误:

public_api.ts:

// Lots of exports...
export { setup } from './core/connect/setup'; <-- Falls over if including this.

连接/setup.js

function setup(p1, p2, p3) {
// Some code
}


function method12(p1, p2, p3) {
// Some code
}

export { setup };

错误:

Could no resolve './setup' from ui-lib/dist/esm2015/core/setup.js

注意:如果我将文件转换为 .ts,ng build 运行良好并创建了一个 dist 文件夹。但是,在 setup.js 中,我引入了许多其他 .js 文件,因此现在无法将所有这些文件转换为 .ts 并解决类型问题

更新:

tsconfig.json

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "importHelpers": true,
        "target": "es5",
        "typeRoots": [
            "node_modules/@types"
        ],
        "lib": [
            "es2017",
            "dom"
        ],
        "paths": {
            "my-lib": [
                "dist/my-lib"
            ],
            "my-lib/*": [
                "dist/my-lib/*"
            ]
        }
    }
}

tsconfig.lib.json:

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../../out-tsc/lib",
        "target": "es2015",
        "module": "es2015",
        "moduleResolution": "node",
        "declaration": true,
        "sourceMap": true,
        "inlineSources": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "importHelpers": true,
        "types": []
    },
    "angularCompilerOptions": {
        "annotateForClosureCompiler": false,
        "skipTemplateCodegen": true,
        "strictMetadataEmit": false,
        "fullTemplateTypeCheck": true,
        "strictInjectionParameters": true,
        "enableResourceInlining": true
    },
    "exclude": [
        "src/test.ts",
        "**/*.spec.ts"
    ],
    "include": [
        "src/**/*",
        "./src/**/*",
    ]
}

【问题讨论】:

  • 你能发布你的 tsconfig 吗?
  • @Robertgarcia - 更新了两个 tsconfig 文件。
  • 在您的 tsconfig 中尝试使用 allowJs: true
  • 在 tsconfig 或 tsconfig.lib 中?
  • 您的文件需要在编译时解决问题?

标签: angular angular-cli ng-packagr ng-build angular-cli-v7


【解决方案1】:

connect/setup.js 应该是:

export function setup(p1, p2, p3) {
// Some code
}


function method12(p1, p2, p3) {
// Some code
}

【讨论】:

  • 谢谢,但似乎遇到了同样的问题。
  • 是核心/连接/设置、连接/设置还是核心/设置?我在您的问题中看到了所有三个版本。
猜你喜欢
  • 2019-12-19
  • 2019-11-29
  • 2021-11-24
  • 2022-08-03
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多