Search code examples
requirejstypescriptamd

Output require() construct with AMD generation


I currently have a set of RequireJs modules defined as:

require(['dep1', 'dep2'], function(dep2, dep2) {
    ...
});

I'm looking to convert these into TypeScript modules, however the AMD generation outputs them with the define() construct:

define(['dep1', 'dep2'], function(dep2, dep2) {
    ...
});

I can see how they would essentially be equivalent. However I have always worked on the following basis:

  • Define: When you want to be able to pass the module into other modules.
  • Require: When the module just needs to be executed

Are these assumptions redundant? Or is there a way to instruct TypeScript to output the require() construct for a module?


Solution

  • Are these assumptions redundant?

    Yes. require should only be used when you want to lazy load.

    is there a way to instruct TypeScript to output the require() construct for a module

    Yes. Use the requirejs definition : https://github.com/borisyankov/DefinitelyTyped/blob/master/requirejs/require.d.ts and write code to call the require function manually.