Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path mapping should relative path ? #9910

Closed
panudetjt opened this issue Jul 23, 2016 · 3 comments
Closed

Path mapping should relative path ? #9910

panudetjt opened this issue Jul 23, 2016 · 3 comments
Labels
Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@panudetjt
Copy link

TypeScript Version: nightly (2.1.0-dev.20160722)

Code

// src/views/foo.ts
import foo from 'view/bar';

console.log('Hi ', foo);
// generated/templates/views/bar.ts
let foo = 'foo'

export default foo;
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
        "view/*": ["generated/templates/views/*"]
    },
    "outDir": "dist"
  }
}

Expected behavior:
the import foo from 'views/bar'; in src/views/foo.ts should generate 'var bar_1 = require('../../../view/bar');' because I've already define it with path mapping, It should relative path or not ?
Actual behavior:
the import foo from 'views/bar'; insrc/views/foo.tsgenerate 'var bar_1 = require('view/bar');

if you don't understand my english you should try by yourself ts-path-mapping

Sorry for my bad eng, Thank you :)

@mhegazy
Copy link
Contributor

mhegazy commented Jul 23, 2016

Reposting my answer from #5039 (comment)

The compiler does not rewrite module names. module names are considered resource identifiers, and are mapped to the output as they appear in the source

The module names you write are not going to change in the output. the "paths" and "baseURL" are there to tell the compiler where they are going to be at runtime.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 23, 2016
@panudetjt
Copy link
Author

Thank you @mhegazy, I've understood 👍

@mhegazy mhegazy added the Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design label Sep 13, 2016
@papercuptech
Copy link

papercuptech commented Dec 20, 2016

Appreciating TS's position, here's a simple solution to the 90% use case for those of us using node, but wanting the convenience of using baseUrl relative require() calls without any fuss.

This solution hooks node's require() call, and resolves requests using the dirname of "main" to mimic baseUrl. It therefore assumes the baseUrl compiler option was also set to the same directory where the source "main.ts" was located.

To use, paste this tiny chunk of code at the top of your "main.ts".

import * as path from 'path'
import * as fs from 'fs'
(function() {
  const CH_PERIOD = 46
  const baseUrl = path.dirname(process['mainModule'].filename)
  const existsCache = {d:0}; delete existsCache.d
  const moduleProto = Object.getPrototypeOf(module)
  const origRequire = moduleProto.require
  moduleProto.require = function(request) {
    let existsPath = existsCache[request]
    if(existsPath === undefined) {
      existsPath = ''
      if(!path.isAbsolute(request) && request.charCodeAt(0) !== CH_PERIOD) {
        const ext = path.extname(request)
        const basedRequest = path.join(baseUrl, ext ? request : request + '.js')
        if(fs.existsSync(basedRequest)) existsPath = basedRequest
        else {
          const basedIndexRequest = path.join(baseUrl, request, 'index.js')
          existsPath = fs.existsSync(basedIndexRequest) ? basedIndexRequest : ''
        }
      }
      existsCache[request] = existsPath
    }
    return origRequire.call(this, existsPath || request)
  }
})()

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants