Code Monkey home page Code Monkey logo

typescript-loader's Introduction

TypeScript Webpack Loader

DEPRECATED: Use awesome-typescript-loader or ts-loader instead.

TypeScript loader for Webpack.

Example Configuration

webpack.config.js

module.exports = {

  // Currently we need to add '.ts' to resolve.extensions array.
  resolve: {
    extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
  },

  // Source maps support (or 'inline-source-map' also works)
  devtool: 'source-map',

  // Add loader for .ts files.
  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'typescript-loader'
      }
    ]
  }
};

After that, you would be able to write JSX in TypeScript!

Best Practices

Using with JSX-TypeScript compiler

You can use typescript-loader together with jsx-typescript compiler which has support for JSX syntax (used in React.js).

For that you need to install jsx-typescript:

% npm install jsx-typescript

And specify typescriptCompiler loader option:

module.exports = {

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'typescript-loader?typescriptCompiler=jsx-typescript'
      }
    ]
  }
};

External Modules

The most natural way to structure your code with TypeScript and webpack is to use external modules, and these work as you would expect.

npm install --save react
import React = require('react');

Internal Modules

TypeScript Loader will work with internal modules too, however acquiring a reference to modules declared this way requires some work using the exports-loader. This is required because webpack wraps every file in a closure and internal modules are meant to run in a global context.

foo.ts

module Foo {
  export var bar = 42;
}

main.ts

/// <reference path="foo.ts" />
var foo: typeof Foo = require('exports?Foo!./foo');
console.log(foo.bar) // 42

typescript-loader's People

Contributors

andreypopp avatar chimeracoder avatar jbrantly avatar jonjaques avatar s-panferov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-loader's Issues

Redundant SourceMap URL

Just to let you know that there is an additional problem with redundant sourceMappingURLs that Typescript places directly in the output Javascript file.

The fix is an additional line of code. Please see the ts-loader issue for the solution:

TypeStrong/ts-loader#12

SourceMap - Path is missing

Hello,

first, thanks for this loader! Well done!

I tried to integrate it into my project and found out that the path is missing from the source maps.

Lets assume that I have a file under

app/model/somedata.ts

After setting the option to generate the source map and opening Chrome devtools, I find the source under the root of "webpack://" instead of "webpack://app/model".

Peter

Doesn't work with TS 1.5 alpha, which is a required dependency

I'm seeing this when compiling TS files:

ERROR in [file]
Module build failed: TypeError: Cannot read property 'text' of undefined
    at Object.getLength (node_modules\typescript-loader\lib\TypeScriptWebpackHost.js:65:18)
    at createLanguageServiceSourceFile (node_modules\typescript-loader\node_modules\typescript\bin\typescript.js:30185:97)
    ....

The issue here seems to be that Typescript's LS host interface has changed between 1.4.1 and 1.5 alpha. typescript-loader probably shouldn't have a required dependency on pre-release versions of typescript; I suggest changing the dependency from ^1.4.1 to ~1.4.1 until TS 1.5 comes out and any required updates to integration with the language services host interface can be made safely.

Generate documentation

Hello all, maybe is not the best place to write, but I've a problem to generate the documentation from my code.
I've seen that your plugin do that:
ensure(ids: string[], WebpackRequireEnsureCallback) => any;
that permit to compile in ts when you write something like that:
var Controller = require("entry.Controller");

but how I can permit to compile the other things?
For example if you use http://typedoc.io/ it fail when it found:
var Controller = require("entry.Controller");
because it's not a valid ts code.
Same things for the other tools.

Module build failed: TypeError: Cannot read property 'Succeeded' of undefined on Typescript 1.5.3

Typescript Loader doesn't work on Typescript 1.5.3

[02:24:59] [webpack] Hash: f846dfff7e40e9e091ae
Version: webpack 1.10.5
Time: 2694ms
     Asset       Size  Chunks             Chunk Names
index.html  258 bytes          [emitted]  

ERROR in ./src/app/main.ts
Module build failed: TypeError: Cannot read property 'Succeeded' of undefined
    at TypeScriptWebpackHost.<anonymous> (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/lib/TypeScriptWebpackHost.js:210:61)
    at tryCatcher (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/promise.js:503:31)
    at Promise._settlePromiseAt (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/promise.js:577:18)
    at Promise._settlePromiseAtPostResolution (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/promise.js:244:10)
    at Async._drainQueue (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/async.js:128:12)
    at Async._drainQueues (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/thakerng/Desktop/nfsolution/phatra2/node_modules/typescript-loader/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:371:17)

Code splitting

I wasn't able to implement it via require.ensure:

require.ensure(['./module'], function(require) {
  import module = require('./module');
});

Because TypeScript can't parse it:

/Users/andreypopp/Workspace/typescript-loader/example/index.ts (28, 7) Statement expected.

Are import statements only allowed at top level?

Is this project abandoned?

@andreypopp @jgoz It seems like this project has been abandoned since there has been no activity in some time and it does not work with the last two stable TypeScript releases (1.5 and 1.6). However, due to the somewhat canonical name "typescript-loader" it's still first in Google results which causes confusion as people try it, find it doesn't work, and have to go find another loader.

ts-loader was created around the same time as this loader and has been actively maintained since. I recently moved the repository to the TypeStrong organization. TypeStrong consolidates various TypeScript tooling efforts under a community of maintainers so that there's less chance of projects going abandoned.

With this in mind, I wanted to reach out and see if you would be willing to:

  • Add a deprecation notice to the README and point to ts-loader
  • Possibly allow ts-loader to be renamed to typescript-loader by assigning the NPM package to me

If you would like to deprecate but still participate in development we would be happy to add you to the TypeStrong organization.

Thanks!

Cannot read property 'text' of undefined

I am working on a RTS game for the web but I am having an issue with webpack and the typescript-loader when running webpack. My project is here: https://github.com/QuantumInformation/Density-Wars

Here is the stacktrace:

nmac:Density-Wars nikos$ webpack 
Hash: 62190bf4c04297c7337c
Version: webpack 1.11.0
Time: 485ms
              Asset     Size  Chunks             Chunk Names
    density-wars.js  1.63 kB       0  [emitted]  main
density-wars.js.map  1.44 kB       0  [emitted]  main
   [0] multi main 28 bytes {0} [built] [1 error]
    + 1 hidden modules

ERROR in ./game.ts
Module build failed: TypeError: Cannot read property 'text' of undefined
    at Object.getLength (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/lib/TypeScriptWebpackHost.js:65:18)
    at createLanguageServiceSourceFile (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:36827:97)
    at acquireOrUpdateDocument (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:36938:34)
    at Object.acquireDocument (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:36927:20)
    at Object.getOrCreateSourceFile [as getSourceFile] (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:37482:41)
    at findSourceFile (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:30043:33)
    at processSourceFile (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:30004:27)
    at /Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:30084:17
    at Object.forEach (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:748:30)
    at processReferencedFiles (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:30082:16)
    at findSourceFile (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:30058:25)
    at processSourceFile (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:30004:27)
    at processRootFile (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:29990:13)
    at /Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:29869:56
    at Object.forEach (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:748:30)
    at Object.createProgram (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:29869:12)
    at synchronizeHostData (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:37411:33)
    at Object.getEmitOutput (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/node_modules/typescript/bin/typescript.js:40181:13)
    at TypeScriptWebpackHost.emit (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/lib/TypeScriptWebpackHost.js:203:20)
    at Object.typescriptLoader (/Users/nikos/WebstormProjects/Density-Wars/node_modules/typescript-loader/lib/index.js:36:40)
 @ multi main

Thanks for any advice.

Simpler implementation possible?

Howdy. I tried using this a couple days ago but immediately ran into issues with the way imports are found and resolved. For example, ambient interfaces like require('react/addons') resulted in the loader literally trying to load that module which didn't exist. I figured there had to be an interface exposed that would walk the dependency tree, so I went digging and eventually came up with this:

https://gist.github.com/jbrantly/7a374d27f329a651fbec

That is a completely functioning loader in 58 files which offloads most of the work to TypeScript. The key piece is typescript.createProgram() and program.getSourceFiles() which causes TypeScript to walk the dependency tree starting from one file. Each file that is found is added to the language service so that it can be emitted if the loader ever asks for it. I believe a simplistic approach like this is the way to go.

There are a couple of additional notes:

  1. Almost all file I/O is happening within TypeScript which does not have an async interface. Therefore everything is sync. This could potentially have some performance downsides but it does make the code significantly less complex.
  2. We don't need to do anything special to the files like adding References to the top as near as I can tell. I'm unsure why the webpack runtime is added to the top of every file.
  3. As near as I can tell the code is not making use of versioning which can allow for quick recompiles if just a single file changes, for example.

Are you open to some of the changes above? I wanted to check first before putting the work in on a PR. I believe implementing these changes would gut quite a bit of the existing codebase.

Throw error code when build fails

When using typescript-loader with webpack, failed compilations cause an exit code of 0 to be returned, even when the --bail flag is set on webpack (which is soon going to be switched to the default behavior: webpack/webpack#708).

This breaks build tools like make and Docker, which use exit codes to determine whether a process encountered an error or not.

screenshot - 04222015 - 10 28 26 am

Is it possible to debug Typescript bundled up by Webpack in Visual Studio?

hi @andreypopp ,

I'm trying to get the debugging working in Visual studio 2013 for Typescript bundled up by webpack but it doesn't work. I'm able to debug Typescript in browsers (IE,Firefox etc.). Before going any further with trying to solve this issue, I'd would like to ask if this is even possible? My apology if this is not a right place for this question.

cheers,

Problems in dependency resolution

Hello @andreypopp. Thanks for the great library! I have been using it a little and I have found several big problems. The main problem right now is that is doesn't collect all the files that TypeScript needs. Consider this code from your library:

https://github.com/andreypopp/typescript-loader/blob/master/lib/TypeScriptWebpackHost.js#L177-L193

TypeScriptWebpackHost.prototype._addDependencies = function(resolver, filename) {
  var dependencies = this._findImportDeclarations(filename).map(function(dep) {
    return this._resolve(resolver, filename, dep).then(function(filename) {
      var alreadyExists = this._files[filename];
      var added = this._readFileAndAdd(filename);
      // This is d.ts which doesn't go through typescript-loader separately so
      // we should take care of it by analyzing its dependencies here.
      if (/\.d.ts$/.exec(filename) && !alreadyExists) {
        added = added.then(function() {
          return this._addDependencies(resolver, filename);
        }.bind(this));
      }
      return added;
    }.bind(this));
  }.bind(this));
  return Promise.all(dependencies);
}

In this function you traverse only one level of the regular (non .d.ts) dependencies. But to compile the file TS needs the whole dependency tree to be loaded into the service.

We don't see any errors because here you collect only current file diagnostics, but the actual errors are in the file deps.

Several days ago I have forked your library to awesome-typescript-loader and completely rewritten all the things there.

I wrote this issue because:

  1. I want to warn you and your users about the problem.
  2. If you want I can explain all the changes I made there so you could port them to your library if you want.

I added the info about your authorship into the README, but your project doesn't contain the LICENSE file so just say if you want me to add some additional copyright notice.

How to gain a reference to an internal module?

Hello,

I'm having some trouble figuring out how to work with TypeScript's internal modules.

From what I can tell, you're supposed to just reference them using the /// syntax and with a normal tsc compile, they would be within the global or at least module level namespace.

I've done some debugging and found that typescript-loader is indeed picking up the reference and reading the file, but I believe what's happening is the file isn't actually exporting anything and therefore webpack doesn't have reason to include it in the build.
I've pushed a branch illustrating the issue here.

Likewise, if you try to import it like an external module, TypeScript will yell at you for doing so. However, as I was writing this out it occurred to me to try writing export = {moduleName}, along with an import - and it worked exactly as expected.

The problem still remains though of how not to modify vendor files? What would the webpack way of handling this be (and also what is feasible from the loader's point of view)?

Personally, I dislike the idea of internal modules altogether and would prefer to handle everything as an external module. That seems like most node/webpack like.

node.d.ts is unusable with this loader

Hey,

thanks for the great project. But unfortunately I have run into an issue. If you install the node.d.ts from definitelytyped.org the loader tries to resolve the require statements inside that file, even though they refer to definitions inside that file.

Excerpt from my config:

resolve: { extensions: ['', '.webpack.js', '.web.js', '.js', '.ts'] }
loaders: [
  {
    test: /\.ts?$/,
    exclude: /node_modules/,
    loader: 'babel!typescript-loader?noImplicitAny=true&target=es6!ts-jsx-loader'
  }
]

The error is

Module build failed: Error: Cannot resolve module 'net.ts' in path/to/typings/node

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.