Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Typescript 1.6 es6 with Decorator, Babel, SourceMap, throwing Exception #15

Closed
ghost opened this issue Oct 10, 2015 · 1 comment
Closed

Comments

@ghost
Copy link

ghost commented Oct 10, 2015

I'm using TS@1.6.2 configured to output ES6 and sourcemaps, outside of webpack.
Then using webpack@1.12.2 with source-map-loader@0.1.5 preloader, and babel-loader@5.3.1 loader.
If original TS source contains a Decorator, the following exception is thrown

c:\src\webpack\node_modules\webpack-core\lib\NormalModuleMixin.js:163
if(isError) throw e;
^

Error: No element indexed by 1
at ArraySet_at as at
at SourceMapConsumer. (c:\src\webpack\node_modules\webpack-core\node_modules\source-map\lib\source-map\source-map-consumer.js:147:69)
at Array.map (native)
at SourceMapConsumer_eachMapping as eachMapping
at Function.SourceNode_fromStringWithSourceMap as fromStringWithSourceMap
at SourceMapSource.node (c:\src\webpack\node_modules\webpack-core\lib\SourceMapSource.js:41:20)
at ReplaceSource.node (c:\src\webpack\node_modules\webpack-core\lib\ReplaceSource.js:55:29)
at CachedSource.node (c:\src\webpack\node_modules\webpack-core\lib\CachedSource.js:41:22)
at PrefixSource.node (c:\src\webpack\node_modules\webpack-core\lib\PrefixSource.js:27:26)
at c:\src\webpack\node_modules\webpack-core\lib\ConcatSource.js:38:49

If the babel-loader is disabled (simply by changing test pattern to non matching regex), no exceptions are thrown and output is as expected.

If the Decorator call is disabled (simply by removing the '@' before the call), no exceptions are thrown and output is as expected.

To Reproduce:

tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "sourceMap": true,
        "experimentalDecorators": true
    },
    "exclude": [
        "node_modules"
    ]
}

webpack.config.js

module.exports = {
    entry: [
        './decoratorChoking.js'
    ],
    devtool: 'source-map',
    output: {
        path: path.join(__dirname, '/'),
        filename: 'app.js'
    },
    module: {
        preLoaders: [{
            test: /\.js$/,
            loader: 'source-map-loader'
        }],
        loaders: [{
            test: /\.js$/,
            loader: 'babel',
            exclude: /node_modules/
        }]
    }
}

decoratorChoking.ts

function ChokingDecorator() {
    function(target: Function) {
    }
}

@ChokingDecorator
class Plain {}

It appears either source-map-loader, babel-loader, or webpack-core is generating a SourceMapSource object where its sourceMap.sources property contains 1 element set to "decoratorChoking.ts", but the sourceMap.sourcesContent contains 2 elements. The 1st element is the TypeScript transpiled Javascript; i.e. the contents of decoratorChoking.js, and the 2nd the original TypeScript source; i.e. the contents of decoratorChoking.ts. This is the cause of the above exception, although not the root cause of course.

Simply removing the @ symbol in front of @ChokingDecorator within decoratorChoking.ts results in a SourceMapSource object having only one element in sourceMap.sourcesContent, it being the contents of the original TypeScript file decoratorChoking.ts. This does not cause an exception to be thrown.

Kindest regards in helping locate and resolve this bug please.

@ghost
Copy link
Author

ghost commented Oct 10, 2015

This appears to be an issue in babel-core

@ghost ghost closed this as completed Oct 10, 2015
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants