Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Webpack+Typescript+Namespace (internal module)

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?

like image 381
U-Day Avatar asked Apr 27 '16 15:04

U-Day


People also ask

What is the difference between namespace and module in TypeScript?

Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .

What is internal module in TypeScript?

Internal Module This was used to logically group classes, interfaces, functions into one unit and can be exported in another module. This logical grouping is named namespace in latest version of TypeScript. So internal modules are obsolete instead we can use namespace.

Can I use TypeScript module in JavaScript?

We cannot use TypeScript modules directly in our application. We need to use the JavaScript for TypeScript modules. To get the JavaScript files for the TypeScript modules, we need to compile modules using TypeScript compiler.

Does TypeScript have namespaces?

In TypeScript, you can use namespaces to organize your code. Previously known as internal modules, namespaces in TypeScript are based on an early draft of the ECMAScript modules.


1 Answers

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/

like image 50
basarat Avatar answered Sep 19 '22 03:09

basarat