My typescript library transpiles smoothly using tsc
with the provided configuration:
{
"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"
]
}
However, when another project attempts to use this library from a linked npm package and bundle it with webpack
and ts-loader
, it fails with the following error for all library files:
Error: Typescript emitted no output for /library/path/to/file.ts
Note: Webpack attempts to load the library from the linked destination rather than from node_modules
due to its npm link.
The webpack configuration of the project that uses the library is as follows:
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'
});
The project's tsconfig.json file that uses the 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"
]
}
Below is an example of a library file that fails to emit 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'm unable to identify why webpack is unable to emit output for that project, as I can transpile the library without any issues. Any assistance would be appreciated.