Плохо упакованная библиотека - Angular пользовательская библиотека - NPM - PullRequest
0 голосов
/ 29 мая 2020

Я учусь публиковать sh простую Angular библиотеку в NPM. Я следил за множеством различных руководств (например, здесь или здесь или здесь ) и получил пакет на NPM. Однако, когда я пытаюсь использовать его в тестовом проекте, он всегда выдает ошибку.

ЧТО Я СДЕЛАЛ

TL; DR: Вы можете найти то, что я пытался сделать из github и npm.

  1. Я создал новый проект angular, как указано в их документации:
ng new angularx-wrapper-workspace --create-application=false
cd angularx-wrapper-workspace
ng generate library angularx-wrapper

Моя структура папок следующая:

enter image description here

  1. Я разрабатываю библиотеку, проще говоря, это основные файлы:
//public-api.ts
export * from './lib/angularx-wrapper.module';
export * from './lib/angularx-wrapper.component';
//angularx-wrapper.module.ts
import { NgModule } from '@angular/core';
import { AngularXWrapperComponent } from './angularx-wrapper.component';
import { CommonModule } from '@angular/common';

@NgModule({
  declarations: [AngularXWrapperComponent],
  imports: [
      CommonModule
  ],
  exports: [AngularXWrapperComponent]
})
export class AngularXWrapperModule { }

(По всей видимости, ошибка связана с процессом упаковки npm, поэтому я опущу нерелевантные подробности)

Собираю библиотеку для публикации:
ng build angularx-wrapper --prod
Я публикую sh библиотеку:
npm publish

Библиотека была успешно опубликована. Однако, когда я создаю новое тестовое приложение, чтобы посмотреть, работает ли оно (приложение test совершенно другое, оно не находится внутри папки библиотеки):

ng new test
cd test
npm install angularx-wrapper

ВОПРОСЫ

Произошли две вещи . Во-первых, мне не удалось импортировать библиотеку в свое тестовое приложение:

enter image description here

The only way to import it is to do this:

введите описание изображения здесь

Даже после успешного импорта библиотеки в мое тестовое приложение его запуск приводил к следующей ошибке:

ERROR in ./node_modules/angularx-wrapper/src/lib/angularx-wrapper.component.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: /code/user/test/node_modules/angularx-wrapper/src/lib/angularx-wrapper.component.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format.
    at AngularCompilerPlugin.getCompiledFile (/code/user/code/user/test/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:933:23)
    at /code/afunworm/code/user/test/node_modules/@ngtools/webpack/src/loader.js:41:31
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
ERROR in ./node_modules/angularx-wrapper/src/lib/angularx-wrapper.module.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: /code/user/code/user/test/node_modules/angularx-wrapper/src/lib/angularx-wrapper.module.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format.
    at AngularCompilerPlugin.getCompiledFile (/code/user/code/user/test/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:933:23)
    at /code/user/code/user/test/node_modules/@ngtools/webpack/src/loader.js:41:31
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Какой шаг я сделал неправильно? Была ли проблема в структуре папок моей библиотеки? Кажется, я не могу найти никаких ресурсов о том, как решить эту проблему.

1 Ответ

1 голос
/ 29 мая 2020

Я нашел проблемы. Проблема в том, что вам нужно запускать npm publish из папки dist, а НЕ из самой папки библиотеки. Это была довольно глупая ошибка, но специально для этой ошибки нет документации, поэтому я полагаю, что оставлю ответ здесь.

...