user2739103 user2739103 - 1 month ago 6
TypeScript Question

Webpack built typescript files-modules are empty objects

I have this in webpack config

module: {
loaders: [
{ test: /\.ts$/, loader: 'awesome-typescript-loader' }
]
}


also this in tsconfig.json

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node"
},
"files": [
"./src/library.ts",
"./src/test-utils.ts",
"./src/request-service",
"./typings/index.d.ts"
]
}


and this in tests for mocha:

const Library = require('../dist/library');

const testUtils = require('../dist/test-utils');

const MockRequestService = testUtils.MockRequestService;

const assert = require('assert');

const djs = new Library('/');


testUtils and Library are empty objects. So new Library throws error that it cannot be used with new because it's not a constructor.

I did dir proper export default and exports as I did in simple babel+es6 build and files in /dist seem to be ok. Trying to figure out, maybe I did something wrong with typescript config (new to it)?

edit: worth saying, I have something like dependency to webpack entry point (test-utils import interfaces from library) but (!) because of interfaces in TypeScript, I import only them from another modules, this is why there is hack in webpack config ([] brackets for entries)

Answer

testUtils and Library are empty objects. So new Library throws error that it cannot be used with new because it's not a constructor.

Use import instead of require:

import Library = require('../path/to/src/library');

And run mocha with ts-node : https://github.com/TypeStrong/ts-node#mocha

Comments