angularbuildng-packagrangular20

Build of angular library with ng-packagr fails for json translation files not found


I migrated one of our projects from Angular 18 to Angular 20. The projects is strutured in this way:

project_name
-- projects
---- frontend
---- lib
------ src
------ lib
-------- i18n
-------- public-api.ts
------ ng-package.json

The upgrade change also to new build system.

After I update all the deps and fix the build errors, I push on our git, but the pipeline fails due to this error

Could not resolve "./lib/i18n/en-GB.json" from "./lib/tmp-typings/public-api.d.ts" error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1

This one is my tsconfig.lib.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "declarationDir": "../../out-tsc/lib",
    "outDir": "../../out-tsc/lib",
    "declarationMap": true,
    "inlineSources": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "moduleResolution": "bundler",
    "types": [],
    "lib": [
      "dom",
      "es2019"
    ]
  },
  "angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "enableResourceInlining": true,
    "enableIvy": true,
    "compilationMode": "partial"
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

This one the ng-package.json

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/project_name_lib",
  "lib": {
    "entryFile": "src/public-api.ts"
  },
  "assets": [
    "./prebuilt-themes/**/*",
    "./src/lib/**/*.scss"
  ],
  "allowedNonPeerDependencies": [
    "lz-string"
  ]
}
//public-api.ts
...
import * as TC_DE_Translations from './lib/i18n/de-DE.json';
import * as TC_EN_Translations from './lib/i18n/en-GB.json';
import * as TC_ES_Translations from './lib/i18n/es-ES.json';
import * as TC_IT_Translations from './lib/i18n/it-IT.json';

export { TC_DE_Translations, TC_EN_Translations, TC_ES_Translations, TC_IT_Translations };
...

I tried to add configuration, on tsconfig.lib.json, I try to add

typings.d.ts

declare module '*.json' {
  const value: any;
  export default value;
}

But nothing changes.

Any suggestions to fix this issue?


Solution

  • Try adding them from the assets, maybe that will fix the issue.

    {
      "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
      "dest": "../../dist/project_name_lib",
      "lib": {
        "entryFile": "src/public-api.ts"
      },
      "assets": [
        "./prebuilt-themes/**/*",
        "./src/lib/**/*.scss",
         {
           "glob": "**/*",
           "input": "./src/lib/i18n/",
           "output": "/assets/"
         }
      ],
      "allowedNonPeerDependencies": [
        "lz-string"
      ]
    }
    

    Then reference the assets path instead.

    //public-api.ts
    ...
    import * as TC_DE_Translations from './assets/i18n/de-DE.json';
    import * as TC_EN_Translations from './assets/i18n/en-GB.json';
    import * as TC_ES_Translations from './assets/i18n/es-ES.json';
    import * as TC_IT_Translations from './assets/i18n/it-IT.json';
    
    export { TC_DE_Translations, TC_EN_Translations, TC_ES_Translations, TC_IT_Translations };
    ...