Typescript Compiler - Combining External Libraries

I am trying to bundle my TypeScript application "with a single javascript file.

I am not using any coherent but TSC (using TypeScript 2.2). Besides my own ts files, my application also uses external modules like immutablejs.

I read all possible streams, documentation, but I can't find a way to combine external modules (from node_modules) into my compiled / rewritten javascript file using only TSC.

Below you can find a sample of my latest / configu code as well as the results of my attempts.

tsconfig.json

{
    "compilerOptions": {
        "target":           "es5",
        "module":           "system",
        "removeComments":   true,
        "sourceMap":        true,
        "allowJs":          true
    }
}

      

app.ts - note: ./ something.ts added successfully.

/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />

import * as something from "./something";
import * as Immutable  from "immutable";

console.log( something.test );

const map1 = Immutable.Map( { a: 1, b: '2', c: 'cu' });
console.log( map1.get( 'a') )

      

1 #: using tsc-bundle ( https://www.npmjs.com/package/typescript-bundle )

Not only does this method not bind immutable Js, it also throws an error: require is undefined .

var dragonfly = (function () {
  var main = null;
  var modules = {
      "require": {
          factory: undefined,
          dependencies: [],
          exports: function (args, callback) { return require(args, callback); },
          resolved: true
      }
  };
  function define(id, dependencies, factory) {
      return main = modules[id] = {
          dependencies: dependencies,
          factory: factory,
          exports: {},
          resolved: false
      };
  }
  function resolve(definition) {
      if (definition.resolved === true)
          return;
      definition.resolved = true;
      var dependencies = definition.dependencies.map(function (id) {
          return (id === "exports")
              ? definition.exports
              : (function () {
                  if(modules[id] !== undefined) {
                    resolve(modules[id]);
                    return modules[id].exports;
                  } else return require(id)
              })();
      });
      definition.factory.apply(null, dependencies);
  }
  function collect() {
      Object.keys(modules).map(function (key) { return modules[key]; }).forEach(resolve);
      return (main !== null) 
        ? main.exports
        : undefined
  }

  define("something", ["require", "exports"], function (require, exports) {
      "use strict";
      Object.defineProperty(exports, "__esModule", { value: true });
      exports.test = "oie";
  });
  define("app", ["require", "exports", "something", "immutable"], function (require, exports, something, Immutable) {
      "use strict";
      Object.defineProperty(exports, "__esModule", { value: true });
      console.log(something.test);
      var map1 = Immutable.Map({ a: 1, b: '2', c: 'cu' });
      console.log(map1.get('a'));
  });
  //# sourceMappingURL=app.js.map
  return collect(); 
})();

      

# 2 - using TSC with module = system (tsc src / app.ts -m system --outfile build / app.js)

This method also does not bind immutable Js, but it also throws an error: system not defined

System.register("something", [], function (exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var test;
    return {
        setters: [],
        execute: function () {
            exports_1("test", test = "oie");
        }
    };
});
/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />
System.register("app", ["something", "immutable"], function (exports_2, context_2) {
    "use strict";
    var __moduleName = context_2 && context_2.id;
    var something, Immutable, map1;
    return {
        setters: [
            function (something_1) {
                something = something_1;
            },
            function (Immutable_1) {
                Immutable = Immutable_1;
            }
        ],
        execute: function () {/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />
            console.log(something.test);
            map1 = Immutable.Map({ a: 1, b: '2', c: 'cu' });
            console.log(map1.get('a'));
        }
    };
});

      

# 3 - using tsc with module = amd (tsc src / app.ts -m amd --outfile build / app.js)

This method not only fails to bind immutable Js, it also throws an error: define is not defined.

define("something", ["require", "exports"], function (require, exports) {
    "use strict";
    exports.__esModule = true;
    exports.test = "oie";
});
/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />
define("app", ["require", "exports", "something", "immutable"], function (require, exports, something, Immutable) {
    "use strict";
    exports.__esModule = true;
    console.log(something.test);
    var map1 = Immutable.Map({ a: 1, b: '2', c: 'cu' });
    console.log(map1.get('a'));
});

      

Conclusion . It is very important that in the future my project can bundle external libraries without the need for these popular bundles like webpack, browsify, gulp, etc.

Can anyone help me?

Thanks in advance,

TF!

+3


source to share


1 answer


Just noticed your post and disclaimer, I am the author of the typescript-bundle. You can link ImmutableJS with the following.

tsc-bundle --project ./tsconfig.json --importAs immutable=Immutable

      

Some documentation about this switch here

This will create an additional resolver that tries to eliminate the immutable from the window object. This usually happens if you include dependent scripts on your page (using a tag <script>

) and need a global name in this package (optional in this case) and you need to use the .d.ts files pulled from @types / * with a surrounding module called "immutable")



The above line leads to the following resolve () function.

  function resolve(definition) {
      if (definition.resolved === true)
          return;
      definition.resolved = true;
      var dependencies = definition.dependencies.map(function (id) {
          return (id === "exports")
              ? definition.exports
              : (function () {
                  if(modules[id] !== undefined) {
                    resolve(modules[id]);
                    return modules[id].exports;
                  } else if(id === "immutable") {
                    return window["Immutable"];
                  } else {
                    try {
                      return require(id);
                    } catch(e) {
                      throw Error("module '" + id + "' not found.");
                    }
                  }
              })();
      });
      definition.factory.apply(null, dependencies);
  }

      

This works against most of the declarations you'll find in the npm @ types / * repository (assuming UMD) and allows the package to efficiently pull the module out of the environment (global variable name).

Hope you find this useful

0


source







All Articles