Reputation: 5462
I have a library written with typescript. I can transpile it using tsc
and with the configuration below without any problems.
{
"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"
]
}
But when another project tries to use this library from a linked npm package, it fails to transpile and bundle using webpack
and ts-loader
. For all the files of the library webpack gets error:
Error: Typescript emitted no output for /library/path/to/file.ts
Note: webpack tries to load it from the linked destination not from node_modules
as it is linked with npm.
Webpack configuration of the project uses library is below.
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 of the project uses library
{
"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"
]
}
And an example of library file that is not emitting output:
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;
}
}
}
I couldn't find any reason why it is not emitting output for that project. I can transpile the library without any problems. Can someone help me about this?
Upvotes: 2
Views: 7984
Reputation: 30949
I can reproduce this error if the application tries to import a TypeScript source file from the library (i.e., from the lib
subdirectory). Try importing the generated JavaScript file from the dist
subdirectory instead. If you really want to recompile the library as part of the application, you probably need to add it to your "include"
list in the tsconfig.json
of the application.
Upvotes: 1