typescript-require
Advanced tools
Comparing version 0.1.6 to 0.1.7
109
index.js
@@ -8,13 +8,8 @@ var fs = require('fs'); | ||
['TMPDIR', 'TMP', 'TEMP'].forEach(function(td) { | ||
if (!tsTempFile && process.env[td]) | ||
tsTempFile = process.env[td]; | ||
if(!tsTempFile && process.env[td]) tsTempFile = process.env[td]; | ||
}); | ||
tsTempFile = path.join((tsTempFile || "/tmp"), "typescript-require-" + Date.now() + ".js"); | ||
var contents = [ | ||
"(function() {", | ||
fs.readFileSync(path.join(__dirname, "/node_modules/typescript/bin/typescript.js"), "utf8"), | ||
"module.exports = TypeScript;", | ||
"}).call({});" | ||
].join(""); | ||
var contents = ["(function() {", fs.readFileSync(require.resolve("typescript"), "utf8"), "module.exports = TypeScript;", "}).call({});"].join(""); | ||
fs.writeFileSync(tsTempFile, contents, "utf8"); | ||
@@ -27,8 +22,21 @@ | ||
//setting up the compiler | ||
var settings = new TypeScript.CompilationSettings(); | ||
settings.codeGenTarget = TypeScript.CodeGenTarget.ES5; | ||
settings.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous; | ||
settings.resolve = true; | ||
var env = new TypeScript.CompilationEnvironment(settings, io); | ||
require.extensions['.ts'] = function(module) { | ||
var js = ''; | ||
var output = { | ||
Write: function(value) { js += value; }, | ||
WriteLine: function(value) { js += value + "\n"; }, | ||
Write: function(value) { | ||
js += value; | ||
}, | ||
WriteLine: function(value) { | ||
js += value + "\n"; | ||
}, | ||
Close: function() {} | ||
@@ -42,22 +50,20 @@ }; | ||
}; | ||
var compiler = new TypeScript.TypeScriptCompiler(null, new TypeScript.NullLogger(), settings); | ||
compiler.parser.errorRecovery = true; | ||
var units = [{ | ||
fileName: path.join(__dirname, "./typings/lib.d.ts") | ||
}]; | ||
var settings = new TypeScript.CompilationSettings(); | ||
settings.codeGenTarget = TypeScript.CodeGenTarget.ES5; | ||
settings.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous; | ||
settings.resolve = true; | ||
var moduleFilename = TypeScript.switchToForwardSlashes(module.filename); | ||
var env = new TypeScript.CompilationEnvironment(settings, io); | ||
var resolver = new TypeScript.CodeResolver(env); | ||
var moduleFilename = TypeScript.switchToForwardSlashes(module.filename); | ||
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 }); | ||
if(!units.some(function(u) { | ||
return u.fileName == code.path | ||
})) units.push({ | ||
fileName: code.path, | ||
code: code.content | ||
}); | ||
}, | ||
@@ -68,37 +74,27 @@ postResolutionError: function(file, message) { | ||
}); | ||
var isErrorPrinted = false; | ||
var compiler = new TypeScript.TypeScriptCompiler(null, null, new TypeScript.NullLogger(), settings); | ||
compiler.parser.errorRecovery = true; | ||
compiler.setErrorCallback(function(start, len, message, block) { | ||
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+$/, "") | ||
]; | ||
if(isErrorPrinted == false && units[block].fileName == moduleFilename) { | ||
var code = units[block].code; | ||
var cursor = code.substr(0, start).split('\n'); | ||
var line = [ | ||
cursor.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 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') | ||
var error = new Error('TypeScript Error: ' + message); | ||
error.stack = ['TypeScript Error: ' + message, 'File: ' + units[block].fileName, '', 'Line '+cursor.length +': '+ line.join(""), '------' + underline.join("")].join('\n'); | ||
throw error; | ||
console.error('\n' + error.stack); | ||
isErrorPrinted = true; | ||
} | ||
}); | ||
units.forEach(function(u) { | ||
if (!u.code) | ||
u.code = fs.readFileSync(u.fileName, "utf8"); | ||
if(!u.code) u.code = fs.readFileSync(u.fileName, "utf8"); | ||
@@ -110,10 +106,9 @@ compiler.addUnit(u.code, u.fileName, false); | ||
compiler.emit(true, function(fn) { | ||
if (fn == moduleFilename.replace(/\.ts$/, ".js")) | ||
return output; | ||
else | ||
return nulloutput; | ||
compiler.emit(function(fn) { | ||
count = 0; | ||
if(fn == moduleFilename.replace(/\.ts$/, ".js")) return output; | ||
else return nulloutput; | ||
}); | ||
module._compile(js, moduleFilename); | ||
}; | ||
}; |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"main": "./index.js", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
"dependencies": { | ||
"typescript": "" | ||
"typescript": ">= 0.8.1-1" | ||
}, | ||
@@ -20,0 +20,0 @@ "keywords": ["typescript", "require"], |
@@ -46,2 +46,4 @@ TypeScript Require Extension | ||
///<reference path='node.d.ts'/> | ||
// Load a JavaScript module with standard Node.JS require | ||
@@ -52,3 +54,3 @@ var foomodule = require('./foomodule.js'); | ||
import barmodule = module('barmodule'); | ||
``` | ||
Note that the second one essentially gets compiled to a `require` call just like the first one. However, | ||
@@ -62,4 +64,2 @@ `import ... module` syntax makes it possible to use TyepScript compile time validation features (like type checking). | ||
**Note:** I've been toying with TypeScript for just a couple of hours now, so I might have some weird bugs here. | ||
License | ||
@@ -66,0 +66,0 @@ ======= |
@@ -0,1 +1,2 @@ | ||
///<reference path='node.d.ts'/> | ||
import tsmodule = module("tsmodule"); | ||
@@ -2,0 +3,0 @@ var jsmodule = require("./jsmodule.js"); |
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
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
286434
8316
0
Updatedtypescript@>= 0.8.1-1