Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use exported models #992

Closed
sataqi opened this issue Jul 5, 2018 · 10 comments
Closed

Unable to use exported models #992

sataqi opened this issue Jul 5, 2018 · 10 comments

Comments

@sataqi
Copy link

sataqi commented Jul 5, 2018

Type of Issue

[ ] Bug Report
[ ] Feature Request
[x] Question

Description

How to create sub package for each kind of export? For example, I want import statement to look like this for each type of file exported:

Models (inluding interfaces and classes)
import {FooModel, BarModel} from 'my-lib/models';

Enums
import {FooTypes, BarTypes} from 'my-lib/enums';

Right now, as all the exports are from public_api.ts file, my import statements look like this:

Models (inluding interfaces and classes)
import {FooModel, BarModel} from 'my-lib';

Enums
import {FooTypes, BarTypes} from 'my-lib';

I tried creating export files but webpack fails to compile. Here's the link to full question, with code and error statement: StackOverflow

@alan-agius4
Copy link
Member

How are you exporting? from your example all you are doing is importing.

If you wanrt export you should replace the import with export keyword

@sataqi
Copy link
Author

sataqi commented Jul 5, 2018

@alan-agius4

Model class: src/app/modules/my-custom-module/models/my-model.ts

export class MyModel {
  constructor() {
    // default values for props here
  }

  // ...props here
}

Index file: src/app/modules/my-custom-module/models/index.ts

export * from './my-model';
export * from './my-other-model';

Export file at root: models.ts
export * from './src/app/modules/my-custom-module/models/index';

ng-packagr file at root: public_api.ts

export * from './models';

export * from './src/app/modules/my-custom-module/my-custom-module.module';
export * from './src/app/modules/my-custom-module/components/index';

Main project code:

import {MyCustomModule} from 'my-custom-library'; // works
import {MyModel} from 'my-custom-library'; // works
import {MyModel} from 'my-custom-library/models'; // does not work

I believe, I'm missing something about mapping webpack module resolution because VS Code recognizes the import, intellisense works and even Ctrl + Click navigates to right file but webpack fails in compilation.

Error:

module not found error can't resolve my-custom-library/models

@alan-agius4
Copy link
Member

alan-agius4 commented Jul 5, 2018 via email

@sataqi
Copy link
Author

sataqi commented Jul 5, 2018

Ok but wouldn't my import still look like this:

import {MyModel} from 'my-custom-library'; // as models are exported from public_api.ts

but I want my models import to look something like this:

import {MyModel} from 'my-custom-library/models' // note the /models sub path in the end

@alan-agius4
Copy link
Member

alan-agius4 commented Jul 5, 2018

If you wish to use import {MyModel} from 'my-custom-library/models' you'd need to create secondary entrypoints.

With your current implementation that is something which is not possible.

Take a look at this documentation; https://github.com/dherges/ng-packagr/blob/master/docs/secondary-entrypoints.md

@sataqi
Copy link
Author

sataqi commented Jul 6, 2018

I'll try that and update. thank you!

@sataqi
Copy link
Author

sataqi commented Jul 6, 2018

Tried you suggestion but getting this error.

@alan-agius4
Copy link
Member

Can you create a small reproduction please?

@sataqi
Copy link
Author

sataqi commented Jul 8, 2018

nvm. It worked using secondary-endpoints. Thank you for your help!

@sataqi sataqi closed this as completed Jul 8, 2018
@github-actions
Copy link

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
This action has been performed automatically by a bot.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants