NativeScript / NativeScript/nx

[question] How to use platform specific code in libs?

Open
#64 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

answered question
Dominant language
TypeScript
Stars
68
Forks
15
PR merge metrics
No merged PRs in 30d

Description

It's me again :)

This time it's a question on how to do platform specific code in the libs. Here's my current issue:

I have these files under libs/nativescript-utils:

  • src/lib/nativescript-utils.d.ts
    • export function nativescriptUtils(): string;
  • src/lib/nativescript-utils.android.ts
  • src/lib/nativescript-utils.ios.ts
  • src/index.ts
    • export * from './lib/nativescript-utils';

Then I import the nativescriptUtils() in the app. When running the app I get this error:

ERROR in ../../libs/nativescript-utils/src/lib/nativescript-utils.ios.ts
Module build failed (from ../../node_modules/@ngtools/webpack/src/index.js):
Error: /Users/frank/git/private/nx-mobile-test/libs/nativescript-utils/src/lib/nativescript-utils.ios.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at /Users/frank/git/private/nx-mobile-test/node_modules/@ngtools/webpack/src/ivy/loader.js:64:26
 @ ../../libs/nativescript-utils/src/index.ts 1:0-41 1:0-41
 @ ./src/features/home/components/home.component.ts 2:0-71 12:41-58
 @ ./src/features/home/home.module.ts 5:0-50
 @ ./src/app.routing.ts 15:28-65
 @ ./src/app.module.ts 3:0-49 10:89-105
 @ ./src/main.ts 6:0-41 11:69-78

I did the same exercise under apps, d.ts + ios.ts + android.ts + index.ts -> same issue -> solved it by adding "**/*.ios.ts" to includes in tsconfig.app.json. But no matter what I do in the libs and tsconfig.json or tsconfig.lib.json, it's not being picked up.

Do you have any tips on how to fix this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with libs/nativescript-utils/src/index.ts and the platform-specific files, then compare the library tsconfig.json and tsconfig.lib.json with apps' tsconfig.app.json. Reproduce the missing-from-TypeScript-compilation error and trace which configuration controls library file inclusion. Done means both .ios.ts and .android.ts implementations compile when imported from the library.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.