ts-loader
Advanced tools
Comparing version 0.1.0 to 0.2.0
121
index.js
@@ -6,45 +6,70 @@ ///<reference path="node_modules/typescript/bin/typescript.d.ts" /> | ||
///<reference path="typings/q/Q.d.ts" /> | ||
///<reference path="typings/loaderUtils/loaderUtils.d.ts" /> | ||
///<reference path="typings/objectAssign/objectAssign.d.ts" /> | ||
var typescript = require('typescript'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var Q = require('q'); | ||
var loaderUtils = require('loader-utils'); | ||
var objectAssign = require('object-assign'); | ||
var readFile = Q.denodeify(fs.readFile); | ||
var compilerOptions = { | ||
target: 1 /* ES5 */, | ||
module: 1 /* CommonJS */, | ||
sourceMap: true | ||
}; | ||
var files = {}; | ||
var libPath = path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts'); | ||
files[libPath] = { | ||
text: fs.readFileSync(libPath, 'utf-8'), | ||
version: 0 | ||
}; | ||
var servicesHost = { | ||
getScriptFileNames: function () { return Object.keys(files); }, | ||
getScriptVersion: function (filename) { return files[filename] && files[filename].version.toString(); }, | ||
getScriptSnapshot: function (filename) { | ||
var file = files[filename]; | ||
return { | ||
getText: function (start, end) { return file.text.substring(start, end); }, | ||
getLength: function () { return file.text.length; }, | ||
getLineStartPositions: function () { return []; }, | ||
getChangeRange: function (oldSnapshot) { return undefined; } | ||
}; | ||
}, | ||
getCurrentDirectory: function () { return process.cwd(); }, | ||
getScriptIsOpen: function () { return true; }, | ||
getCompilationSettings: function () { return compilerOptions; }, | ||
getDefaultLibFilename: function (options) { return 'lib.d.ts'; }, | ||
log: function (message) { return console.log(message); } | ||
}; | ||
var langService = typescript.createLanguageService(servicesHost, typescript.createDocumentRegistry()); | ||
var instances = {}; | ||
function ensureTypeScriptInstance(options) { | ||
if (Object.prototype.hasOwnProperty.call(instances, options.instance)) | ||
return instances[options.instance]; | ||
var target; | ||
switch (options.target) { | ||
case "ES3": | ||
target = 0 /* ES3 */; | ||
break; | ||
case "ES6": | ||
target = 2 /* ES6 */; | ||
break; | ||
default: target = 1 /* ES5 */; | ||
} | ||
var compilerOptions = { | ||
target: target, | ||
module: options.module == "AMD" ? 2 /* AMD */ : 1 /* CommonJS */, | ||
sourceMap: !!options.sourceMap | ||
}; | ||
var files = {}; | ||
var libPath = path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts'); | ||
files[libPath] = { | ||
text: fs.readFileSync(libPath, 'utf-8'), | ||
version: 0 | ||
}; | ||
var servicesHost = { | ||
getScriptFileNames: function () { return Object.keys(files); }, | ||
getScriptVersion: function (filename) { return files[filename] && files[filename].version.toString(); }, | ||
getScriptSnapshot: function (filename) { | ||
var file = files[filename]; | ||
return { | ||
getText: function (start, end) { return file.text.substring(start, end); }, | ||
getLength: function () { return file.text.length; }, | ||
getLineStartPositions: function () { return []; }, | ||
getChangeRange: function (oldSnapshot) { return undefined; } | ||
}; | ||
}, | ||
getCurrentDirectory: function () { return process.cwd(); }, | ||
getScriptIsOpen: function () { return true; }, | ||
getCompilationSettings: function () { return compilerOptions; }, | ||
getDefaultLibFilename: function (options) { return 'lib.d.ts'; }, | ||
// getNewLine() should work in next version of TypeScript | ||
// see https://github.com/Microsoft/TypeScript/issues/1653 | ||
//getNewLine: () => { return os.EOL }, | ||
log: function (message) { return console.log(message); } | ||
}; | ||
var languageService = typescript.createLanguageService(servicesHost, typescript.createDocumentRegistry()); | ||
return instances[options.instance] = { | ||
files: files, | ||
languageService: languageService | ||
}; | ||
} | ||
function rootReferencePath(referencePath, dirname) { | ||
return typescript.isRootedDiskPath(referencePath) ? referencePath : typescript.combinePaths(dirname, referencePath); | ||
} | ||
function loadDependenciesAndTransform(filePath, contents) { | ||
var _this = this; | ||
function ensureDependencies(resolver, instance, filePath, contents) { | ||
filePath = path.normalize(filePath); | ||
var resolve = Q.denodeify(this.resolve.bind(this)); | ||
if (!Object.prototype.hasOwnProperty.call(files, filePath)) { | ||
if (!Object.prototype.hasOwnProperty.call(instance.files, filePath)) { | ||
var fileInfo = typescript.preProcessFile(contents); | ||
@@ -59,4 +84,4 @@ var dirname = path.dirname(filePath); | ||
}); }); | ||
files[filePath] = { version: 0, text: contents }; | ||
return Q.all(dependencies.map(function (f) { return resolve(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 loadDependenciesAndTransform.call(_this, f.resolved, fileContents); }); })); }).then(function () { return contents; }); | ||
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; }); | ||
} | ||
@@ -70,4 +95,11 @@ return Q(contents); | ||
var filePath = path.normalize(this.resourcePath); | ||
Q.fcall(loadDependenciesAndTransform.bind(this), filePath, contents).then(function (contents) { | ||
var file = files[filePath]; | ||
var options = loaderUtils.parseQuery(this.query); | ||
options = objectAssign({}, { | ||
instance: 'default', | ||
sourceMap: false | ||
}, options); | ||
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; | ||
@@ -87,5 +119,12 @@ file.version++; | ||
throw new Error("Typescript emitted no output for " + filePath); | ||
contents = output.outputFiles[1].text; | ||
var sourceMap = JSON.parse(output.outputFiles[0].text); | ||
sourceMap.sourcesContent = [contents]; | ||
var sourceMap; | ||
if (options.sourceMap) { | ||
contents = output.outputFiles[1].text; | ||
sourceMap = JSON.parse(output.outputFiles[0].text); | ||
sourceMap.sourcesContent = [contents]; | ||
} | ||
else { | ||
contents = output.outputFiles[0].text; | ||
} | ||
contents = contents.replace(/\r\n/g, os.EOL); | ||
return [contents, sourceMap]; | ||
@@ -92,0 +131,0 @@ }).done(function (contents) { return callback(null, contents[0], contents[1]); }, function (err) { return callback(err); }); |
{ | ||
"name": "ts-loader", | ||
"version": "0.1.0", | ||
"description": "TypeScript loader for webpack", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublish": "tsc index.ts --module commonjs" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jbrantly/ts-loader.git" | ||
}, | ||
"keywords": [ | ||
"ts-loader", | ||
"typescript-loader", | ||
"webpack", | ||
"loader", | ||
"typescript", | ||
"ts" | ||
], | ||
"author": "James Brantly <james@jbrantly.com> (http://www.jbrantly.com/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/jbrantly/ts-loader/issues" | ||
}, | ||
"homepage": "https://github.com/jbrantly/ts-loader", | ||
"dependencies": { | ||
"q": "^1.1.2", | ||
"typescript": "^1.4.1" | ||
} | ||
"name": "ts-loader", | ||
"version": "0.2.0", | ||
"description": "TypeScript loader for webpack", | ||
"main": "index.js", | ||
"scripts": { | ||
"pretest": "tsc index.ts --module commonjs", | ||
"test": "node ./node_modules/mocha/bin/mocha --reporter spec test/run.js", | ||
"prepublish": "tsc index.ts --module commonjs" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jbrantly/ts-loader.git" | ||
}, | ||
"keywords": [ | ||
"ts-loader", | ||
"typescript-loader", | ||
"webpack", | ||
"loader", | ||
"typescript", | ||
"ts" | ||
], | ||
"author": "James Brantly <james@jbrantly.com> (http://www.jbrantly.com/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/jbrantly/ts-loader/issues" | ||
}, | ||
"homepage": "https://github.com/jbrantly/ts-loader", | ||
"dependencies": { | ||
"loader-utils": "^0.2.6", | ||
"object-assign": "^2.0.0", | ||
"q": "^1.1.2", | ||
"typescript": "^1.4.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.1.0", | ||
"webpack": "^1.5.3" | ||
} | ||
} |
@@ -21,3 +21,3 @@ # TypeScript loader for webpack | ||
loaders: [ | ||
{ test: /\.ts$/, loader: 'ts-loader' } | ||
{ test: /\.ts$/, loader: 'ts-loader?sourceMap&target=ES3' } | ||
] | ||
@@ -27,3 +27,29 @@ } | ||
``` | ||
#### Options | ||
##### target | ||
Specify the TypeScript output target. | ||
- ES3 | ||
- **ES5 (default)** | ||
- ES6 | ||
##### module | ||
Specify the type of modules that TypeScript emits. | ||
- **CommonJS (default)** | ||
- AMD | ||
##### sourceMap *(boolean) (default=false)* | ||
Specify whether or not TypeScript emits source maps. | ||
##### instance *(string)* | ||
Advanced option to force files to go through different instances of the | ||
TypeScript compiler. Can be used to force segregation between different parts | ||
of your code. Can typically be ignored. | ||
### Declaration files | ||
@@ -73,4 +99,12 @@ | ||
Please see [selective-jsx-loader](https://github.com/jbrantly/selective-jsx-loader) | ||
Please see [ts-jsx-loader](https://github.com/jbrantly/ts-jsx-loader) | ||
## Building from source | ||
``` | ||
tsd reinstall | ||
npm install | ||
tsc index.ts --module commonjs | ||
``` | ||
## License | ||
@@ -77,0 +111,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
11762
6
131
131
4
2
+ Addedloader-utils@^0.2.6
+ Addedobject-assign@^2.0.0
+ Addedbig.js@3.2.0(transitive)
+ Addedemojis-list@2.1.0(transitive)
+ Addedjson5@0.5.1(transitive)
+ Addedloader-utils@0.2.17(transitive)
+ Addedobject-assign@2.1.14.1.1(transitive)