Search code examples
node.jstypescriptgruntjsuglifyjs

Grunt a TypeScript to JavaScript with uglify


I have 4 TypeScript files under the ts directory. I can compile them all into one file (main.js) with a source map (main.js.map) using the typescript:base task.

However, trying to uglify those files is not working when compiling more than one TypeScript file. It's as if uglify is getting confused when the sourceMapIn was made with more than one file.

How would you compile a TypeScript project with more than one file, into one file with a sourcemap (Back to the original ts files)

Here's the grunt file:

module.exports = function (grunt) {
    grunt.initConfig({
        uglify: {
            dist: {
                options: {
                    sourceMap: '../js/main.min.map',
                    sourceMapIn: 'main.js.map',
                    sourceMapRoot: '../ts/'
                },
                files: {
                    '../js/main.min.js': ['main.js']
                }
            }
        },
        typescript: {
            base: {
                 src: ['**/*.ts'],
                 dest: '../js/main.js',
                 options: {
                     module: 'amd',
                     sourcemap: true,
                     declaration: false
                 }
             }

        }
    });

    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-typescript');

    grunt.file.setBase('../ts');

    grunt.registerTask('default', ['typescript:base', 'uglify:dist']);
};

Thanks!


Solution

  • I tried to reproduce your problem with the following environment:

    grunt: 0.4.1
    grunt-contrib-uglify: 0.2.2
    grunt-typescript: 0.2.4
    nodejs: 0.10.15
    

    I had to change uglify.dist.options.sourceMapIn to '../js/main.js.map' and uglify.dist.files['../js/main.min.js'] to ['../js/main.js'], i. e. make the paths relative to the gruntfile location. Afterwards, compilation worked flawlessly and both ../js/main.min.js and ../js/main.min.map looked correct.