tsconfig-paths icon indicating copy to clipboard operation
tsconfig-paths copied to clipboard

Working with tslint-loader

Open morriq opened this issue 7 years ago • 7 comments

Hello, since I'm using tslint-loader I have issue with tsconfig-paths.

My problem is that tslint-loader try to load tslint using var Lint = require('tslint'); https://github.com/wbuchwalter/tslint-loader/blob/4d166651d916a0c43dd5a11099d6ff532350e27b/index.js#L8

but same file exists in my project so while tslint-loader try to load tslint from node_modules then it's receiving tslint from my project.

Would somebody help me please?

morriq avatar Oct 31 '17 17:10 morriq

Sorry for the late response. Is this still a problem? Maybe we could implement an option for specifying excluded modules names that should not be resolved with tsconfig-paths

Jontem avatar Sep 02 '18 19:09 Jontem

As far as I know, this is still a problem. A workaround I have used is setting "baseUrl": "./src", in my tsconfig, that way it does not try to resolve files in root.

mihkeleidast avatar Sep 03 '18 08:09 mihkeleidast

In my opinion, this implementation is more correct and also solves the problem above

  Module._resolveFilename = function(request: string, _parent: any): string {
    try {
      // tslint:disable-next-line:no-invalid-this
      return originalResolveFilename.apply(this, arguments);
    } catch (err) {
      const found = matchPath(request);
      if (found) {
        const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
        // tslint:disable-next-line:no-invalid-this
        return originalResolveFilename.apply(this, modifiedArguments);
      }

      throw err;
    }
  };

stenin-nikita avatar Oct 10 '18 23:10 stenin-nikita

Just to clarify let me see if I understand how to re-create the problem:

  • Use tslint-loader with webpack.
  • Have a tslint.json file in the root of the project.
  • Start webpack somehow with tsconfig-paths registered.

Is that correct?

Also, iIf someone would like to provide a repo which reproduces the problem that would also save us some time to try and re-create it.

jonaskello avatar Nov 04 '18 14:11 jonaskello

I recreated the problem in the sandbox. You need to download the project and run npm install and npm start. https://codesandbox.io/s/7klz2y4v30

stenin-nikita avatar Nov 04 '18 16:11 stenin-nikita

I think my solution will not cause performance problems. It may be worth checking the type of error. For example:

    try {
      // tslint:disable-next-line:no-invalid-this
      return originalResolveFilename.apply(this, arguments);
    } catch (err) {
      if (err.code === 'MODULE_NOT_FOUND') {
        const found = matchPath(request);
        if (found) {
          const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
          // tslint:disable-next-line:no-invalid-this
          return originalResolveFilename.apply(this, modifiedArguments);
        }
      }
      throw err;
    }

stenin-nikita avatar Nov 04 '18 16:11 stenin-nikita

what about adding an option disabled by default to enabled this behavior ?

Athorcis avatar Mar 21 '19 22:03 Athorcis