Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path Mapping Module Resolution (Typescript 2.0)

Tags:

typescript

i have a question about Path Mapping Module Resolution (Typescript 2.0).

I have a project with structure https://github.com/sanex3339/javascript-obfuscator/tree/strictNullChecks

My tsconfig.ts

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": true,
    "baseUrl": "src/",
    "paths": {
      "*": [
        "*"
      ]
    }
  }
}

In file https://github.com/sanex3339/javascript-obfuscator/blob/dev/src/JavaScriptObfuscator.ts i set

import { INode } from 'interfaces/nodes/INode';
import { IObfuscator } from "interfaces/IObfuscator";
import { IOptions } from 'interfaces/IOptions';
import { IOptionsPreset } from "interfaces/IOptionsPreset";

import { JavaScriptObfuscatorCLI } from 'cli/JavaScriptObfuscatorCLI';
import { Obfuscator } from 'Obfuscator';
import { Options } from 'Options';

All interfaces (.d.ts extension) will correctly imported without any errors, but on 3 last imports i get an error:

Can't resolve 'cli/JavaScriptObfuscatorCLI' in '/home/username/PhpstormProjects/javascript-obfuscator/src'

Why i got this error and why no error during interfaces imports?

Interesting, if i look at trace resolution log, i will see:

======== Resolving module 'cli/JavaScriptObfuscatorCLI' from '/home/username/PhpstormProjects/javascript-obfuscator/src/JavaScriptObfuscator.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
'baseUrl' option is set to '/home/username/PhpstormProjects/javascript-obfuscator/src', using this value to resolve non-relative module name 'cli/JavaScriptObfuscatorCLI'
'paths' option is specified, looking for a pattern to match module name 'cli/JavaScriptObfuscatorCLI'.
Module name 'cli/JavaScriptObfuscatorCLI', matched pattern '*'.
Trying substitution '*', candidate module location: 'cli/JavaScriptObfuscatorCLI'.
Loading module as file / folder, candidate module location '/home/username/PhpstormProjects/javascript-obfuscator/src/cli/JavaScriptObfuscatorCLI'.
File '/home/username/PhpstormProjects/javascript-obfuscator/src/cli/JavaScriptObfuscatorCLI.ts' exist - use it as a name resolution result.
Resolving real path for '/home/username/PhpstormProjects/javascript-obfuscator/src/cli/JavaScriptObfuscatorCLI.ts', result '/home/username/PhpstormProjects/javascript-obfuscator/src/cli/JavaScriptObfuscatorCLI.ts'
======== Module name 'cli/JavaScriptObfuscatorCLI' was successfully resolved to '/home/username/PhpstormProjects/javascript-obfuscator/src/cli/JavaScriptObfuscatorCLI.ts'. ========
like image 491
Качалов Тимофей Avatar asked Jun 30 '16 16:06

Качалов Тимофей


People also ask

What is module resolution in TypeScript?

Module resolution is the process of taking a string from the import or require statement, and determining what file that string refers to. TypeScript includes two resolution strategies: Classic and Node. Classic, the default when the compiler option module is not commonjs , is included for backwards compatibility.

What is default module resolution strategy in TypeScript?

There are two possible module resolution strategies: Node and Classic. You can use the moduleResolution option to specify the module resolution strategy. If not specified, the default is Node for --module commonjs , and Classic otherwise (including when module is set to amd , system , umd , es2015 , esnext , etc.).

How does Tsconfig work?

The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

What is allowJs Tsconfig?

allowJs is the option newly available in 1.8. The TypeScript compiler will run a quick sanity check on . js files for syntax errors but otherwise passes them straight through to the output directory.


1 Answers

The error you are getting is definitely not from typescript, but from webpack. Since you are using webpack 2.0 the relevant loader property you should set is the modules: property:

resolve: {
    extensions: ['.ts'],
    modules: ['./src', './node_modules']
},

I cloned your project and set the resolve property to the above setup and was able to compile the project wihtout issues.

like image 196
Daniel Tabuenca Avatar answered Oct 21 '22 13:10

Daniel Tabuenca