-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Comments
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. |
Thank you @mhegazy, I've understood 👍 |
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 This solution hooks node's 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)
}
})() |
TypeScript Version: nightly (2.1.0-dev.20160722)
Code
Expected behavior:
the
import foo from 'views/bar';
insrc/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'; in
src/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 :)
The text was updated successfully, but these errors were encountered: