Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Webpack build issue when importing from platform specific ts files #306

Closed
kvindascr opened this issue Oct 29, 2017 · 2 comments
Closed

Comments

@kvindascr
Copy link

kvindascr commented Oct 29, 2017

I have a project which was building properly with previous versions of libraries.
I have updated to version 0.5.0 and webpack 3.x.x.
Previous versions were 0.3.7 and webpack 2.2.0.

Now updating to the latest libraries I get the following errors.

ERROR in /APPDIR/app/main.aot.ts (8,32): Cannot find module './shared/core/app-delegate'.
ERROR in /APPDIR/app/main.ts (7,32): Cannot find module './shared/core/app-delegate'.

Those missing files are delegates specific to the platform. For example app-delegate.android.ts and app-delegate.ios.ts.

The previous build process showed the issues as warning in yellow, but the current one shows them as errors and build is stopped.

I would be good to understand why it was working before but not now, why it was treated as warnings before, but now as errors, and if there can be some tweaks either on tsconfig, or webpack config.
Also how should platform independent code should be included.

Thanks.

@sis0k0
Copy link
Contributor

sis0k0 commented Nov 10, 2017

I can confirm that this is an issue with the latest version of nativescript-dev-webpack (0.8.0). It's reproducible both with TS and Angular apps. Consider having a typescript file with some exports:

// a.android.ts
export default const platform = "android";

The following imports doesn't work if you try the bundle the project:

// b.ts
import platform from "./a";

However, as a workaround one can use require:

// c.ts
const platform = require("./a");

Importing the file as-is, also works:

// d.ts
import "./a";

The last case explains why importing ./vendor-platform inside ./app/vendor.ts manages to resolve to `./vendor-platform.android|ios.ts.
The above problem is probably related to the way typescript resolves files to be compiled. We should consider the problem when implementing #311.

@sis0k0 sis0k0 added the bug label Nov 10, 2017
@sis0k0 sis0k0 changed the title Webpack build issue when platform specific ts files Webpack build issue when importing from platform specific ts files Nov 10, 2017
@sis0k0
Copy link
Contributor

sis0k0 commented Nov 11, 2017

Thinking about the issue again, I realized that this is actually the expected behavior for the TypeScript compiler. The right fix is to add a common d.ts file for the platform specific files. For example if you have a.android.ts:

// a.android.ts
export const platform = "android";

you need to add a declarations file:

// a.d.ts
export declare const platform = "android";

This will allow tsc to resolve imports like:

// b.ts
import { platform } from "./a";

The declaration files can be generated automatically, but you need to keep in mind that they should match the name of the imports (a.d.ts, not a.android.d.ts) and that they should have a common API from the platform-specific files.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants