我有一个用打字稿写的图书馆。我可以使用tsc
下面的配置对其进行转换,而不会出现任何问题。
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es6",
"es5",
"dom",
"es2017"
],
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"listEmittedFiles": true
},
"exclude": [
"./dist",
"./test",
"./bin"
],
"include": [
"./lib"
]
}
但是当另一个项目尝试从链接的 npm 包中使用这个库时,它无法使用webpack
and进行转换和捆绑ts-loader
。对于库 webpack 的所有文件都会出错:
错误:Typescript 没有为 /library/path/to/file.ts 发出输出
注意:webpack 尝试从链接的目的地加载它,而不是从node_modules
它与 npm链接的目的地加载。
项目使用库的 Webpack 配置如下。
module.exports = (entry, dist) => Object.assign({
entry,
mode: "production",
output: {
filename: "index.js",
path: dist,
},
resolve: {
extensions: [".js", ".ts"]
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
stats: 'verbose'
});
项目使用库的tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es6",
"es5",
"dom",
"es2017"
],
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"./dist"
],
"include": [
"./index.ts"
]
}
还有一个不发出输出的库文件示例:
import {Ctor, Injector} from "./injector";
import {ERROR_CODES, PuzzleError} from "./errors";
import {Route} from "./server";
export interface ApiEvents {}
export interface ApiConfig {
route: Route;
subApis?: Array<Ctor<Api>>;
}
interface ApiBase {
}
export function PuzzleApi<T>(config: ApiConfig) {
return Injector.decorate((constructor: () => void) => {
console.log(`Registering Api: ${constructor.name}`);
}, config);
}
export class Api implements ApiBase {
config: ApiConfig;
constructor() {
const config = (this.constructor as any).config as ApiConfig;
if (!config) {
throw new PuzzleError(ERROR_CODES.CLASS_IS_NOT_DECORATED, this.constructor.name);
} else {
this.config = config;
}
}
}
我找不到它没有为该项目发出输出的任何原因。我可以毫无问题地编译库。有人可以帮我解决这个问题吗?