typescript-require
Advanced tools
Comparing version 0.1.5 to 0.1.6
72
index.js
@@ -6,12 +6,9 @@ var fs = require('fs'); | ||
var fileName = null; | ||
// Make up a temp file | ||
var tsTempFile = null; | ||
['TMPDIR', 'TMP', 'TEMP'].forEach(function(td) { | ||
if (!fileName && process.env[td]) | ||
fileName = process.env[td]; | ||
if (!tsTempFile && process.env[td]) | ||
tsTempFile = process.env[td]; | ||
}); | ||
tsTempFile = path.join((tsTempFile || "/tmp"), "typescript-require-" + Date.now() + ".js"); | ||
fileName = path.join((fileName || "/tmp"), "typescript-require-" + Date.now() + ".js"); | ||
var contents = [ | ||
@@ -23,8 +20,8 @@ "(function() {", | ||
].join(""); | ||
fs.writeFileSync(fileName, contents, "utf8"); | ||
fs.writeFileSync(tsTempFile, contents, "utf8"); | ||
var TypeScript = require(fileName); | ||
var TypeScript = module.exports.TypeScript = require(tsTempFile); | ||
TypeScript.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous; | ||
fs.unlinkSync(fileName); | ||
fs.unlinkSync(tsTempFile); | ||
@@ -52,12 +49,15 @@ require.extensions['.ts'] = function(module) { | ||
var env = new TypeScript.CompilationEnvironment(settings, io); | ||
var resolver = new TypeScript.CodeResolver(env); | ||
var fpath = TypeScript.switchToForwardSlashes(module.filename); | ||
var units = [path.join(__dirname, "./typings/lib.d.ts"), path.join(__dirname, "./typings/node.d.ts")]; | ||
var moduleFilename = TypeScript.switchToForwardSlashes(module.filename); | ||
resolver.resolveCode(fpath, "", false, { | ||
postResolution: function(p, code) { | ||
if (units.indexOf(p) < 0) | ||
units.push(p); | ||
var units = [ | ||
{ fileName: path.join(__dirname, "./typings/lib.d.ts") }, | ||
{ fileName: path.join(__dirname, "./typings/node.d.ts") } | ||
]; | ||
resolver.resolveCode(moduleFilename, "", false, { | ||
postResolution: function(file, code) { | ||
if (!units.some(function(u) { return u.fileName == code.path })) | ||
units.push({ fileName: code.path, code: code.content }); | ||
}, | ||
@@ -69,9 +69,30 @@ postResolutionError: function(file, message) { | ||
var compiler = new TypeScript.TypeScriptCompiler(output, output, new TypeScript.NullLogger(), settings); | ||
var compiler = new TypeScript.TypeScriptCompiler(null, null, new TypeScript.NullLogger(), settings); | ||
compiler.parser.errorRecovery = true; | ||
compiler.setErrorCallback(function(start, len, message, block) { | ||
var error = new Error('TypeScript Error: ' + message + '\n File: ' + units[block] + ' Start position: ' + start + ' Length: ' + len); | ||
error.stack = ''; | ||
var code = units[block].code; | ||
var line = [ | ||
code.substr(0, start).split('\n').slice(-1)[0].replace(/^\s+/, ""), | ||
code.substr(start, len), | ||
code.substr(start + len).split('\n').slice(0, 1)[0].replace(/\s+$/, "") | ||
]; | ||
var underline = [ | ||
line[0].replace(/./g, '-'), | ||
line[1].replace(/./g, '^'), | ||
line[2].replace(/./g, '-'), | ||
]; | ||
var error = new Error('TypeScript Error: ' + message); | ||
error.stack = [ | ||
'TypeScript Error: ' + message, | ||
'File: ' + units[block].fileName, | ||
'Start: ' + start + ', Length: ' + len, | ||
'', | ||
'Line: ' + line.join(""), | ||
'------' + underline.join("") | ||
].join('\n') | ||
throw error; | ||
@@ -81,3 +102,6 @@ }); | ||
units.forEach(function(u) { | ||
compiler.addUnit(fs.readFileSync(u, "utf8"), u, false); | ||
if (!u.code) | ||
u.code = fs.readFileSync(u.fileName, "utf8"); | ||
compiler.addUnit(u.code, u.fileName, false); | ||
}); | ||
@@ -88,3 +112,3 @@ | ||
compiler.emit(true, function(fn) { | ||
if (fn == fpath.replace(/\.ts$/, ".js")) | ||
if (fn == moduleFilename.replace(/\.ts$/, ".js")) | ||
return output; | ||
@@ -95,3 +119,3 @@ else | ||
module._compile(js, fpath); | ||
module._compile(js, moduleFilename); | ||
}; |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"main": "./index.js", | ||
@@ -12,0 +12,0 @@ "repository": { |
@@ -39,10 +39,25 @@ TypeScript Require Extension | ||
You should know that; | ||
I've been toying with TypeScript for just a couple of hours now, so I might have some weird bugs here. | ||
# Module Dependencies in TS files | ||
You can load any other TypeScript or Javascript module from your typescripts. However, you should | ||
use different methods for different modules | ||
### sample.ts | ||
Given that there are two files, `foomodule.js` and `barmodule.ts` at the same directory as sample.ts | ||
// Load a JavaScript module with standard Node.JS require | ||
var foomodule = require('./foomodule.js'); | ||
// Load a TypeScript module with TypeScript module syntax | ||
import barmodule = module('barmodule'); | ||
Note that the second one essentially gets compiled to a `require` call just like the first one. However, | ||
`import ... module` syntax makes it possible to use TyepScript compile time validation features (like type checking). | ||
Developed By | ||
============ | ||
* Ekin Koc - <ekin@eknkc.com> | ||
Ekin Koc - <ekin@eknkc.com> | ||
**Note:** I've been toying with TypeScript for just a couple of hours now, so I might have some weird bugs here. | ||
License | ||
@@ -49,0 +64,0 @@ ======= |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
285630
8315
78