ts-loader
Advanced tools
Comparing version 0.2.3 to 0.3.0
# Changelog | ||
## v0.3.0 | ||
- Change how modules are resolved. Imports and declaration file references are | ||
now resolved through TypeScript instead of being resolved through webpack's | ||
`resolve` API. This fixes a number of issues and better aligns the loader to | ||
work as a replacement for the `tsc` command. (#3, #4, #5) | ||
## v0.2.3 | ||
@@ -4,0 +11,0 @@ |
80
index.js
@@ -5,3 +5,2 @@ ///<reference path="node_modules/typescript/bin/typescript.d.ts" /> | ||
///<reference path="typings/node/node.d.ts" /> | ||
///<reference path="typings/q/Q.d.ts" /> | ||
///<reference path="typings/loaderUtils/loaderUtils.d.ts" /> | ||
@@ -14,7 +13,5 @@ ///<reference path="typings/objectAssign/objectAssign.d.ts" /> | ||
var os = require('os'); | ||
var Q = require('q'); | ||
var loaderUtils = require('loader-utils'); | ||
var objectAssign = require('object-assign'); | ||
require('colors'); | ||
var readFile = Q.denodeify(fs.readFile); | ||
var instances = {}; | ||
@@ -69,2 +66,3 @@ function ensureTypeScriptInstance(options) { | ||
return instances[options.instance] = { | ||
compilerOptions: compilerOptions, | ||
files: files, | ||
@@ -74,22 +72,2 @@ languageService: languageService | ||
} | ||
function rootReferencePath(referencePath, dirname) { | ||
return typescript.isRootedDiskPath(referencePath) ? referencePath : typescript.combinePaths(dirname, referencePath); | ||
} | ||
function ensureDependencies(resolver, instance, filePath, contents) { | ||
filePath = path.normalize(filePath); | ||
if (!Object.prototype.hasOwnProperty.call(instance.files, filePath)) { | ||
var fileInfo = typescript.preProcessFile(contents); | ||
var dirname = path.dirname(filePath); | ||
var dependencies = fileInfo.referencedFiles.concat(fileInfo.importedFiles).map(function (f) { return ({ | ||
original: f.filename, | ||
resolved: '', | ||
pos: f.pos, | ||
end: f.end, | ||
reference: fileInfo.referencedFiles.indexOf(f) != -1 | ||
}); }); | ||
instance.files[filePath] = { version: 0, text: contents }; | ||
return Q.all(dependencies.map(function (f) { return resolver(dirname, f.reference ? rootReferencePath(f.original, dirname) : f.original).then(function (newPath) { return f.resolved = newPath; }).then(function (newPath) { return f; }); })).then(function (filePaths) { return filePaths.filter(function (f) { return f.resolved.match(/\.ts$/) != null; }); }).then(function (filePaths) { return Q.all(filePaths.map(function (f) { return readFile(f.resolved, 'utf-8').then(function (fileContents) { return ensureDependencies(resolver, instance, f.resolved, fileContents); }); })); }).then(function () { return contents; }); | ||
} | ||
return Q(contents); | ||
} | ||
function loader(contents) { | ||
@@ -106,32 +84,40 @@ var _this = this; | ||
var instance = ensureTypeScriptInstance(options); | ||
var resolver = Q.denodeify(this.resolve.bind(this)); | ||
ensureDependencies(resolver, instance, filePath, contents).then(function (contents) { | ||
var file = instance.files[filePath], langService = instance.languageService; | ||
file.text = contents; | ||
file.version++; | ||
var output = langService.getEmitOutput(filePath); | ||
var diagnostics = langService.getCompilerOptionsDiagnostics().concat(langService.getSyntacticDiagnostics(filePath)).concat(langService.getSemanticDiagnostics(filePath)).forEach(function (diagnostic) { | ||
if (diagnostic.file) { | ||
var lineChar = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start); | ||
_this.emitError(" " + diagnostic.file.filename.blue + " (" + lineChar.line.toString().cyan + "," + lineChar.character.toString().cyan + "): " + diagnostic.messageText.red); | ||
if (!Object.prototype.hasOwnProperty.call(instance.files, filePath)) { | ||
var filePaths = Object.keys(instance.files); | ||
filePaths.push(filePath); | ||
var program = typescript.createProgram(filePaths, instance.compilerOptions, typescript.createCompilerHost(instance.compilerOptions)); | ||
program.getSourceFiles().forEach(function (file) { | ||
var filePath = path.normalize(file.filename); | ||
if (!Object.prototype.hasOwnProperty.call(instance.files, filePath)) { | ||
instance.files[filePath] = { version: 0, text: file.text }; | ||
} | ||
else { | ||
_this.emitError(" " + "unknown file".blue + ": " + diagnostic.messageText.red); | ||
} | ||
}); | ||
if (output.outputFiles.length == 0) | ||
throw new Error("Typescript emitted no output for " + filePath); | ||
var sourceMap; | ||
if (options.sourceMap) { | ||
sourceMap = JSON.parse(output.outputFiles[0].text); | ||
sourceMap.sourcesContent = [contents]; | ||
contents = output.outputFiles[1].text; | ||
} | ||
var file = instance.files[filePath], langService = instance.languageService; | ||
file.text = contents; | ||
file.version++; | ||
var output = langService.getEmitOutput(filePath); | ||
var diagnostics = langService.getCompilerOptionsDiagnostics().concat(langService.getSyntacticDiagnostics(filePath)).concat(langService.getSemanticDiagnostics(filePath)).forEach(function (diagnostic) { | ||
if (diagnostic.file) { | ||
var lineChar = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start); | ||
_this.emitError(" " + diagnostic.file.filename.blue + " (" + lineChar.line.toString().cyan + "," + lineChar.character.toString().cyan + "): " + diagnostic.messageText.red); | ||
} | ||
else { | ||
contents = output.outputFiles[0].text; | ||
_this.emitError(" " + "unknown file".blue + ": " + diagnostic.messageText.red); | ||
} | ||
contents = contents.replace(/\r\n/g, os.EOL); | ||
return [contents, sourceMap]; | ||
}).done(function (contents) { return callback(null, contents[0], contents[1]); }, function (err) { return callback(err); }); | ||
}); | ||
if (output.outputFiles.length == 0) | ||
throw new Error("Typescript emitted no output for " + filePath); | ||
var sourceMap; | ||
if (options.sourceMap) { | ||
sourceMap = JSON.parse(output.outputFiles[0].text); | ||
sourceMap.sourcesContent = [contents]; | ||
contents = output.outputFiles[1].text; | ||
} | ||
else { | ||
contents = output.outputFiles[0].text; | ||
} | ||
contents = contents.replace(/\r\n/g, os.EOL); | ||
callback(null, contents, sourceMap); | ||
} | ||
module.exports = loader; |
{ | ||
"name": "ts-loader", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "TypeScript loader for webpack", | ||
@@ -33,3 +33,2 @@ "main": "index.js", | ||
"object-assign": "^2.0.0", | ||
"q": "^1.1.2", | ||
"typescript": "^1.4.1" | ||
@@ -36,0 +35,0 @@ }, |
@@ -13,2 +13,5 @@ # TypeScript loader for webpack | ||
1. Add `.ts` as a resolvable extension. | ||
2. Configure all files with a `.ts` extension to be handled by `ts-loader`. | ||
``` | ||
@@ -15,0 +18,0 @@ module.exports = { |
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
4
138
11543
118