typescript - ไลบรารี Angular หลายตัวไม่ทำงานในโหมดโปรดักชัน
ฉันได้สร้างโปรเจกต์ Angular ซึ่งประกอบด้วยไลบรารี Angular สองตัว คุณสามารถดูซอร์สโค้ดได้ที่นี่ หลังจากนั้น ฉันได้สร้างไลบรารีแต่ละตัวและแชร์โฟลเดอร์ dist ที่ได้ผ่านทาง npm นี่คือลิงก์แพ็กเกจ: https://www.npmjs.com/package/@agilie/angular-helpies
หลังจากนั้น ฉันกำลังพยายามใช้ตัวดักจากไลบรารีนี้และนำเข้าแบบนี้
import {DuplicateRequestFilter} from '@agilie/angular-helpies/interceptors';
ทุกอย่างทำงานถูกต้องในโหมดพัฒนา แต่เมื่อฉันพยายามสร้างโปรเจกต์ในโหมดผลิต ฉันได้รับข้อผิดพลาด
ERROR in ./src/app/app.module.ngfactory.js
Module not found: Error: Can't resolve 'interceptors' in '/Users/sergey/libs/test-helpies/src/app'
resolve 'interceptors' in '/Users/sergey/libs/test-helpies/src/app'
Parsed request is a module
using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./src/app)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
looking for modules in /Users/sergey/libs/test-helpies/
using description file: /Users/sergey/libs/test-helpies/package.json (relative path: .)
Field 'browser' doesn't contain a valid alias configuration
using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./interceptors)
no extension
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/interceptors doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/interceptors.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/interceptors.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/interceptors.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/interceptors.js doesn't exist
as directory
/Users/sergey/libs/test-helpies/interceptors doesn't exist
/Users/sergey/libs/test-helpies/src/app/node_modules doesn't exist or is not a directory
/Users/sergey/libs/test-helpies/src/node_modules doesn't exist or is not a directory
/Users/sergey/libs/node_modules doesn't exist or is not a directory
/Users/sergey/node_modules doesn't exist or is not a directory
/Users/node_modules doesn't exist or is not a directory
/node_modules doesn't exist or is not a directory
looking for modules in /Users/sergey/libs/test-helpies/node_modules
using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
using description file: /Users/sergey/libs/test-helpies/package.json (relative path: ./node_modules/interceptors)
no extension
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/node_modules/interceptors doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/node_modules/interceptors.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/node_modules/interceptors.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/node_modules/interceptors.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/Users/sergey/libs/test-helpies/node_modules/interceptors.js doesn't exist
as directory
/Users/sergey/libs/test-helpies/node_modules/interceptors doesn't exist
[/Users/sergey/libs/test-helpies/interceptors]
[/Users/sergey/libs/test-helpies/interceptors.ts]
[/Users/sergey/libs/test-helpies/interceptors.tsx]
[/Users/sergey/libs/test-helpies/interceptors.mjs]
[/Users/sergey/libs/test-helpies/interceptors.js]
[/Users/sergey/libs/test-helpies/src/app/node_modules]
[/Users/sergey/libs/test-helpies/src/node_modules]
[/Users/sergey/libs/node_modules]
[/Users/sergey/node_modules]
[/Users/node_modules]
[/node_modules]
[/Users/sergey/libs/test-helpies/node_modules/interceptors]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.ts]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.tsx]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.mjs]
[/Users/sergey/libs/test-helpies/node_modules/interceptors.js]
@ ./src/app/app.module.ngfactory.js 14:0-35 15:1945-1970
@ ./src/main.ts
@ multi ./src/main.ts
คุณสามารถทำให้เกิดข้อผิดพลาดได้โดยใช้โครงการสาธิตนี้: https://github.com/SergeyMell/test-helpies
ฉันทำอะไรผิดหรือเปล่า? จะขอบคุณมากถ้าได้รับความช่วยเหลือ
อัปเดต
คำตอบในช่อง 'browser' ไม่มีการตั้งค่าอาลิอัสที่ถูกต้อง ดูเหมือนจะให้วิธีแก้ปัญหาว่าจะซ่อมแพ็กเกจที่เสียหายในแอปที่ใช้งานได้อย่างไร ในกรณีของฉัน ตามที่ฉันเข้าใจ ฉันมีปัญหาบางอย่างในการสร้างหรือแจกจ่ายแพ็กเกจของฉัน ดังนั้นสิ่งนี้ควรได้รับการแก้ไข
คำตอบ #1
ข้อผิดพลาดเกิดจาก /interceptors ในการนำเข้าของคุณ
นอกจากนี้คุณยังไม่มีไฟล์ public-api.ts ในโฟลเดอร์ interceptors
มีเอกสารอ้างอิงที่นี่เกี่ยวกับตำแหน่งที่ควรจะวาง public-api.ts ของคุณและวิธีการส่งออกคลาสของคุณอย่างถูกต้อง เช่นเดียวกับที่ @angular/common/http ส่งออกมา
https://github.com/angular/angular/tree/master/packages/common/http
คำตอบ #2
สุดท้ายฉันก็สามารถเอาชนะปัญหาได้ ปัญหาอยู่ที่ชื่อแพ็กเกจ ตราบใดที่แพ็กเกจหลักของฉันมีชื่อว่า @agilie/angular-helpies แพ็กเกจย่อยควรถูกตั้งชื่อว่า @agilie/angular-helpies/interceptors และ @agilie/angular-helpies/decorators ไม่ใช่เพียงแค่ interceptors หรือ decorators.
นี่ดูแปลกมากเพราะถ้าฉันพยายามสร้างไลบรารีด้วยชื่อนั้น ฉันจะได้รับข้อผิดพลาด ชื่อโปรเจ็กต์ "@agilie/angular-helpies/<some name here>" ไม่ถูกต้อง นอกจากนี้เมื่อตั้งชื่อแพ็กเกจใหม่ด้วยตนเอง ฉันจะได้รับคำเตือนว่า สตริงละเมิดรูปแบบ อย่างไรก็ตาม มันใช้งานได้
อาจจะแก้ไขได้ด้วยวิธีอื่น (เช่น การเชื่อมโยง npm หรือบางสิ่งที่แตกต่าง) แต่ฉันพบวิธีแก้ปัญหานี้จริง ๆ แท้จริงแล้ว Angular มีชื่อที่คล้ายกันหากคุณตรวจสอบ https://github.com/angular/angular/blob/master/packages/common/http/package.json เป็นตัวอย่าง