Socket
Socket
Sign inDemoInstall

tsify

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsify - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

test/x.js

5

index.js

@@ -7,3 +7,6 @@ var Tsifier = require('./lib/Tsifier');

b.on('bundle', function () { tsifier.compileAndCacheFiles(b._entries); });
b.on('bundle', function () {
tsifier.clearCompilationCache();
tsifier.compileAndCacheFiles(b._entries.filter(Tsifier.isTypescript));
});

@@ -10,0 +13,0 @@ b.transform(tsifier.transform.bind(tsifier));

69

lib/Tsifier.js

@@ -11,2 +11,4 @@ var convert = require('convert-source-map');

var COMPILE_ERROR_SENTINEL = {};
function isTypescript(file) {

@@ -16,2 +18,6 @@ return (/\.ts$/i).test(file);

function jsFileForTsFile(tsFile) {
return tsFile.replace(/\.ts$/i, '.js').replace(/\\/g, '/');
}
function Tsifier(opts) {

@@ -27,3 +33,3 @@ opts = opts || {};

};
this.tscCache = {};
this.tscCache = null;
}

@@ -33,11 +39,4 @@

Tsifier.prototype.compileAndCacheFiles = function (files) {
var self = this;
ts.compile(files, extend({}, self.opts), function (error, results) {
if (error) self.emit('error', new CompileError(error));
self.tscCache = {};
if (results) {
results.forEach(function (result) { self.tscCache[result.name] = result.text; });
}
});
Tsifier.prototype.clearCompilationCache = function () {
this.tscCache = {};
};

@@ -55,18 +54,26 @@

function end() {
var jsFile = self.getCompiledFileFromCache(file, data);
if (!jsFile) {
self.emit('error', new Error('File not compiled: ' + file));
stream.queue(null);
}
else {
var jsFile = self.getCompiledFile(file, data);
if (jsFile)
stream.queue(jsFile);
stream.queue(null);
}
stream.queue(null);
}
};
Tsifier.prototype.getCompiledFileFromCache = function (tsFile, tsSource) {
var jsFile = tsFile.replace(/\.ts$/i, '.js').replace(/\\/g, '/');
Tsifier.prototype.getCompiledFile = function (tsFile, tsSource) {
var jsFile = jsFileForTsFile(tsFile);
var jsSource = this.tscCache[jsFile];
if (!jsSource) return null;
if (!jsSource) {
this.compileAndCacheFiles([tsFile]);
jsSource = this.tscCache[jsFile];
}
if (!jsSource) {
this.emit('error', new Error('File missing from compilation cache: ' + jsFile));
return null;
}
if (jsSource === COMPILE_ERROR_SENTINEL) {
this.emit('error', new Error('Compilation error for: ' + jsFile));
return null;
}
jsSource = this.inlineSourcemapFile(jsFile, jsSource, tsSource);

@@ -76,2 +83,21 @@ return jsSource;

Tsifier.prototype.compileAndCacheFiles = function (files) {
var self = this;
var tsFiles = files.filter(isTypescript);
if (tsFiles.length === 0) return;
ts.compile(tsFiles, extend({}, self.opts), function (error, results) {
if (error) self.emit('error', new CompileError(error));
self.tscCache = {};
if (results) {
results.forEach(function (result) { self.tscCache[result.name] = result.text; });
} else {
tsFiles.forEach(function (tsFile) {
self.tscCache[jsFileForTsFile(tsFile)] = COMPILE_ERROR_SENTINEL;
});
}
});
};
Tsifier.prototype.inlineSourcemapFile = function (jsFile, jsSource, tsSource) {

@@ -92,1 +118,2 @@ var mapFileComment = convert.mapFileCommentRegex.exec(jsSource);

module.exports = Tsifier;
module.exports.isTypescript = isTypescript;
{
"name": "tsify",
"version": "0.1.3",
"version": "0.1.4",
"description": "Browserify plugin for compiling Typescript",

@@ -25,6 +25,6 @@ "main": "index.js",

"dependencies": {
"convert-source-map": "~0.3.4",
"through": "~2.3.4",
"ts-compiler": "~2.0.0",
"util-extend": "~1.0.1",
"convert-source-map": "~0.3.4"
"util-extend": "~1.0.1"
},

@@ -31,0 +31,0 @@ "devDependencies": {

@@ -23,2 +23,15 @@ var test = require('tape');

test('non-TS main file', function (t) {
t.plan(1);
var expected = fs.readFileSync('test/expected.js').toString();
browserify({ entries: ['./test/x.js'] })
.plugin('./index.js')
.bundle()
.pipe(es.wait(function (err, actual) {
expectCompiledOutput(t, expected, actual);
}));
});
test('--sourcemap', function (t) {

@@ -49,6 +62,6 @@ t.plan(1);

.pipe(es.wait(function () {
t.equal(allErrors.length, 4);
t.equal(allErrors[0].name, 'TS1005');
t.equal(allErrors[1].name, 'TS1005');
t.ok(/^File not compiled/.test(allErrors[3].message));
t.equal(allErrors.length, 4, 'Should have 4 errors in total');
t.equal(allErrors[0].name, 'TS1005', 'Should have syntax error on first import');
t.equal(allErrors[1].name, 'TS1005', 'Should have syntax error on second import');
t.ok(/^Compilation error/.test(allErrors[3].message), 'Should have compilation error message for entire file');
}));

@@ -68,6 +81,6 @@ });

.pipe(es.wait(function () {
t.equal(allErrors.length, 4);
t.equal(allErrors[0].name, 'TS2082');
t.equal(allErrors[1].name, 'TS2087');
t.ok(/^File not compiled/.test(allErrors[3].message));
t.equal(allErrors.length, 4, 'Should have 4 errors in total');
t.equal(allErrors[0].name, 'TS2082', 'Should have "Supplied parameters do not match any call signature of target" error');
t.equal(allErrors[1].name, 'TS2087', 'Should have "Could not select overload for call expression" error');
t.ok(/^Compilation error/.test(allErrors[3].message), 'Should have compilation error message for entire file');
}));

@@ -79,6 +92,6 @@ });

if (expected === actual) {
t.pass('Test passed');
t.pass('Compiled output should match expected output');
} else {
console.log(ansidiff.lines(expected, actual));
t.fail('Test failed');
t.fail('Compiled output should match expected output');
}

@@ -85,0 +98,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc