Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

baseUrl and paths resolution using Webpack 2 and awesome-typescript-loader

I want to setup my project so where ever I use the line below:

import { BlahBlahService } from '@blahblah/core/BlahBlahService';

it is resolved to:

./dist/blahblah/core/BlahBlahService

I already have read through online resources and applied the instructed settings (which I also include below). This is working in Visual Studio Code; i.e. It is not showing error to me hence it is able to correctly read my settings at tsconfig and understand it. I want to make the same thing happen to Webpack.

However I am getting the following error:

ERROR in ./xxxxxxxxxxx.ts Module not found: Error: Can't resolve '@blahblah/core/BlahBlahService' in 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

I have the following setup in my web application:

  • Angular 2 Version 2.2.0
  • Typescript Version 2.0.8
  • Webpack Version 2.1.0-beta.26
  • awesome-typescript-loader Version 2.2.4

I also have the following in my tsconfig:

"compilerOptions": {

    ...

    "baseUrl": ".",
    "paths": {
       "@blahblah/core": ["./dist/blahblah/core"],
       "@blahblah/core/*": ["./dist/blahblah/core/*"],
    }

    ...

 }

And I have the following setting in my webpack.js file:

plugins: [

    ...

    new TsConfigPathsPlugin(),

    ...
],
like image 347
Aidin Avatar asked Dec 11 '16 00:12

Aidin


People also ask

Does TS-loader use Tsconfig?

ts-loader uses tsc , the TypeScript compiler, and relies on your tsconfig. json configuration. Make sure to avoid setting module to "CommonJS", or webpack won't be able to tree-shake your code.

Does TS-loader do type checking?

It performs type checking in a separate process with ts-loader just handling transpilation.

Does Webpack use TSC?

Like Babel, Webpack depends on TSC to transpile TypeScript to JavaScript but as TSC doesn't have a clue about Webpack, hence Webpack needs a loader to talk to TSC. This is where the ts-loader comes into play. Webpack compiles a TypeScript file using ts-loader package which asks TSC to do the compilation.


1 Answers

I'm currently working with [email protected] and [email protected].
Same issue. I found the solution in a github issue.

tsconfig.js:

"compilerOptions": {

    ...

    "baseUrl": ".",
    "paths": {
        "~/*": ["./src"],
    }
}

webpack.config.js:

var TsConfigPathsPlugin = require('awesome-typescript-loader').TsConfigPathsPlugin;

resolve: {
    alias: {
        "~/": "./src"
    },
    plugins: [
        new TsConfigPathsPlugin()
    ]
}
like image 113
borracciaBlu Avatar answered Sep 22 '22 13:09

borracciaBlu