我正在构建一个TypeScript特定的包,该包允许用户自动使用REST API,并将生成的JSON数据转换为真正的TypeScript对象。该软件包将与Vue.js一起用于另一个TypeScript项目中。
我尝试了几种不同的方法来使用Webpack编译和重新打包软件包,并通过npm分发它。我可以将包编译为JavaScript,并生成一个index.d.ts,这些都在package.json中正确配置。我还可以将该包包含在我的其他项目中。尝试编译TypeScript / Vue项目时,在生成的组件中总是出现if
错误。
完整的错误消息是:
strcmp
这是软件包的package.json和webpack.config.js:
Package.json:
This dependency was not found
webpack.config.js:
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
在另一个项目中,我使用例如{
"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"
}
}
包含文件。请注意,const path = require('path');
module.exports = {
entry: path.join(__dirname, '/src/index.ts'),
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: [
'ts-loader'
],
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
library: 'TypeScriptRestMapper',
libraryTarget: 'umd'
},
mode: 'development'
};
是在我的包中的import {Service} from "typescript-rest-mapper";
中定义的。为了从包中导出所有不同的类,我建立了一个index.ts文件,该文件指出:
Service
我尝试将出口更改为src/service/Service.ts
,等等。可能出了什么问题?
注意:我的软件包是开源的,可以在GitHub上找到:https://github.com/AbyxBelgium/TypeScriptRestMapper