如何使用npm导入纯TypeScript包
我正在构建一个特定于TypeScript的包,它允许用户自动使用RESTAPI,并将生成的JSON数据转换为真正的TypeScript对象。该软件包将与Vue.js一起用于另一个TypeScript项目 我尝试了几种不同的方法,使用Webpack编译和重新打包包,并通过npm分发。我能够将包编译成JavaScript,并生成index.d.ts,这些都在package.json中正确配置。我也能够包括到我的其他项目包。当试图编译我的TypeScript/Vue项目时,我总是得到一个如何使用npm导入纯TypeScript包,typescript,npm,webpack,Typescript,Npm,Webpack,我正在构建一个特定于TypeScript的包,它允许用户自动使用RESTAPI,并将生成的JSON数据转换为真正的TypeScript对象。该软件包将与Vue.js一起用于另一个TypeScript项目 我尝试了几种不同的方法,使用Webpack编译和重新打包包,并通过npm分发。我能够将包编译成JavaScript,并生成index.d.ts,这些都在package.json中正确配置。我也能够包括到我的其他项目包。当试图编译我的TypeScript/Vue项目时,我总是得到一个错误,结果组件
错误,结果组件中没有找到该依赖项
完整的错误消息是:
This dependency was not found:
* typescript-rest-mapper in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/ts-loader??ref--12-1!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/sample/SampleDetailsComponent.vue?vue&type=script&lang=ts&, ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_module
s/ts-loader??ref--12-1!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/sample/SampleOverviewComponent.vue?vue&type=script&lang=ts& and 7 others
这是包的package.json和webpack.config.js:
Package.json:
{
"name": "typescript-rest-mapper",
"version": "1.0.6",
"description": "Package that allows you to automagically build services that consume REST API's. Received/send data is automatically converted between real TypeScript-objects and JSON-objects.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"build": "webpack && dts-bundle --configJson dts-bundle.config.json",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AbyxBelgium/TypeScriptRestMapper.git"
},
"keywords": [
"service",
"rest-api",
"typescript",
"rest",
"mapper"
],
"author": "Pieter Verschaffelt",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/AbyxBelgium/TypeScriptRestMapper/issues"
},
"homepage": "https://github.com/AbyxBelgium/TypeScriptRestMapper#readme",
"devDependencies": {
"@babel/core": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"babel-loader": "^8.0.5",
"dts-bundle": "^0.7.3",
"ts-loader": "^5.3.3",
"typescript": "^3.3.4000",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0"
},
"dependencies": {
"reflect-metadata": "^0.1.13",
"axios": "^0.18.0",
"pluralize": "^7.0.0"
}
}
webpack.config.js:
const path=require('path');
module.exports={
条目:path.join(uu dirname,'/src/index.ts'),
devtool:“内联源映射”,
模块:{
规则:[
{
测试:/\.tsx?$/,,
使用:[
“ts加载器”
],
排除:/node\u模块/
}
]
},
决心:{
扩展名:['.tsx','.ts','.js']
},
输出:{
文件名:“bundle.js”,
path:path.resolve(uu dirname,'dist'),
库:“TypeScriptRestMapper”,
图书馆目标:“umd”
},
模式:“发展”
};
在我的另一个项目中,我使用例如从“typescriptrestmapper”导入{Service}的import来包含文件代码>。请注意,Service
是在我的包的src/Service/Service.ts
中定义的。为了从我的包中导出所有不同的类,我构建了一个index.ts文件,其中说明:
从“/Entity/Entity”导出{Entity};
从“/Service/Service”导出{Service}”;
从“/service/Status”导出{Status}”;
从“/decorators/Read”导出{Read}”;
从“/decorators/ReadArray”导出{ReadArray}”;
从“/decorators/Store”导出{Store}”;
从“/decorators/StoreArray”导出{StoreArray}”;
从“/decorators/Update”导出{Update}”;
从“/decorators/UpdateArray”导出{UpdateArray}”;
我已尝试将导出从“/entity/entity
”更改为“export*”,等等。可能出了什么问题
注意:我的软件包是开源的,可以在GitHub上找到:你的软件包main
指向dist/index.js
,你的网页包cfg看起来像是生成dist/bundle.js
@H.B。谢谢:)我已经检查了好几次配置,每次一定都忽略了这一点。它现在正在工作:)很高兴H请随意删除问题(如果您认为可能对任何人都没有帮助)或者将解决方案作为答案发布并接受它。请参阅此处的工作示例:我对这个问题的回答是:您的包main
指向dist/index.js
,您的网页cfg看起来像是生成了dist/bundle.js
@H.B。谢谢:)我已经检查了好几次配置,一定是忽略了这一点每次都是。它现在起作用:)很高兴这有帮助。请随意删除该问题(如果您认为可能对任何人都没有帮助),或将解决方案作为答案发布并接受。请参阅此处的工作示例:以及我对该问题的回答: