Group typescript files into multiple external files

Is it possible to group typescript files into more than one outFile? I still want to link my Typescript, but not into one JS file, I would like to group my TS into several JS files, for example. controllers.js, plugins.js. The typescript project options seem to provide only one outfile option.

+4
source share
1 answer

Unfortunately, this is not the default behavior of the TypeScript compiler. I myself ran into this problem when I tried to modulate a TypeScript application.

The solution I decided to use for this problem:

  • Start using the old old _references.ts files.
  • Do not mess with TSC (just compile with JavaScript).
  • _references.ts / Gulp. :

var fs = require('fs');

// Consts
var PLUGIN_NAME = 'reference-parser';

// Plugin level function (dealing with files)
function referenceParser(fileName, prefix, filterParentReferences) {
    var references = [];

    var content = fs.readFileSync(fileName, 'utf8');
    content = content.replace(/\/\/\/ <reference path=("|')/g, prefix);
    content = content.replace(/.ts("|')\s*\/>,?/g, '.js');

    function readLines(input) {
        if (input.length === 0)
            return;

        var newLineIndex = input.indexOf('\r\n');
        if (newLineIndex >= 0) {
            var line = input.substring(0, newLineIndex).trim();

            readLine(line);

            if (input.length > (newLineIndex + 2))
                readLines(input.substring(newLineIndex + 2));
        } else {
            readLine(input);
        }
    }

    function readLine(line) {
        if (line && line.length > 0) {
            //console.log('Line: ' + line);

            if (line.startsWith('//')) {
                //console.log('Comment line, ignored.');
            } else if (line.indexOf('_references.ts') >= 0) {
                //console.log('External reference line, ignored.'); // TODO Support this?
            } else if (filterParentReferences && line.startsWith('../')) {
                //console.log('Parent reference, ignored.');
            } else {
                references.push(line);
            }
        }
    }

    readLines(content);
    return references;
}

// Exporting the plugin main function
module.exports = referenceParser;
Hide result

. Gulp:

//...

        // Get module source files by parsing the module _references.ts file.
        var sourceFiles = referenceParser(sourceRoot + '_references.ts', buildJsRoot, true);
        // console.log(sourceFiles);
        var sourcesStream = gulp
            .src(sourceFiles)
            .pipe(plugins.sourcemaps.init({ loadMaps: true })) // Load TypeScript generated source maps
            .pipe(plugins.concat(module.name + '.Bundle.js'))
            .pipe(plugins.uglify({ mangle: false })) // Minify the resulting bundle
            .pipe(plugins.sourcemaps.write('.')) // Write the (merged) bundle source maps
            .pipe(gulp.dest(destinationRoot));
        moduleGulpStreams.add(sourcesStream); // Add the source stream to the stream merger

//...
Hide result
0

All Articles