6

We are trying to use Webpack to compile typescript code where we replaced “module” (now defined as external modules) with namespaces (defined as internal modules).

This change was done primarily to be in line with the recommendations of typescript and ensuring that dependency on “require” is not required for running Jasmine based unit tests on Karma. Karma-typescript preprocessor has been configured and the test case is running fine without the need of "require".

The change to namespace caused us to remove the dependency on require which works very well when it comes unit tests and compilation of the code through tsc. But on compilation through Webpack using typescript loaders (I’ve tried ts-loader, Webpack-typescript), the output contains code of just the entry ts file and not its dependencies. Tsc already has an option (--outFile) to concatenate the output into a single file but both the loaders do not use it.

Is there a way (loader or configuration of a loader) to resolve the dependency and bundle it into the single output js produced by Webpack?

2
  • Today, the right way is with the ES6 modules (import/export syntax). Then, you can configure TypeScript to generate CommonJS syntax. This should be help you to use Webpack and Jasmine. Commented Apr 27, 2016 at 16:08
  • Import/export syntax gets converted to a require calls which cause us to have requirejs dependency in karma. I'm trying to avoid that and go for namespaces as per the update in the handbook http://www.typescriptlang.org/docs/handbook/namespaces.html. Is that understanding correct? Commented Apr 27, 2016 at 16:21

1 Answer 1

1

This change was done primarily to be in line with the recommendations of typescript and ensuring that dependency on “require” is not required for running Jasmine based unit tests on Karma

You don't need to do that. You should use --module:commonjs everywhere and bundle for frontend + leave it as it is for running tests using node (node understands commonjs natively).

Example

I do this with alm https://github.com/alm-tools/alm/

Sign up to request clarification or add additional context in comments.

1 Comment

Doing that I would still end up with the using modules for those code pieces which are not external modules. We are trying to avoid that and use namespaces instead. Won't commonJs add the dependency on require?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.