How can I get webpack to find angular modules? How can I get webpack to find angular modules? angularjs angularjs

How can I get webpack to find angular modules?


Finally figured this out.

First issue is that the typescript compiler removes import statements that are not used.

The compiler detects whether each module is used in the emitted JavaScript. If a module identifier is only ever used in type annotations and never as an expression then no require call is emitted for that module. This culling of unused references is a good performance optimization, and also allows for optional loading of those modules.

source: https://github.com/Microsoft/TypeScript/issues/4717

I assigned the imported value to a dummy array and it seems to fix this. Logging the value out to the console also works. (See my final note on why I couldn't simply pass it into the dependency array).

EDIT: Better solution is to use import "module"; syntax since that is always emitted based on the github above, ex: import 'angular-ui-router';


Second, my webpack file was missing an empty string in the resolve array:

resolve { extensions: ['', '.ts', '.js'] }

Without this it couldn't pull in the file for ui router.

A few things I noticed while working on this: webpack --display-error-details is super useful. For some reason it was looking for double .js.js extensions inside node_modules/angular-ui-router/release:

resolve file  /Users/mich2264/projects/angular-ts/node_modules/angular-ui-router/release/angular-ui-router.js.ts doesn't exist  /Users/mich2264/projects/angular-ts/node_modules/angular-ui-router/release/angular-ui-router.js.js doesn't exist

--traceResolution is equally useful for typescript.

EDIT: Appears to be a awesome-typescript-loader loader bug: https://github.com/s-panferov/awesome-typescript-loader/pull/264


Finally, I'm not sure why but when I import the default value from angular-ui-router and I log it or set a breakpoint it shows up correctly as ui.router, but if I attempt to pass it into the dependency array it becomes undefined.

@types/angular-ui-router defines the following export inside their type file : export default "ui.router";


This one should do the job:

import * as angular from 'angular';import * as uiRouter from 'angular-ui-router';let myApp = angular.module('myApp', ['ui.router']);

Notice, that import is just importing router code and in application module you need to inject 'ui.router' string.


I suppose that since I'm using UI-Router for AngularJS (1.x), I had to use...

import '@uirouter/angularjs'

...instead of...

import 'angular-ui-router'