transformImport

  • function
steal-tools.transformImport  

Loads a module, and its dependencies, and provides a transform method, so they can be written out in another form.

stealTools.transformImport(config, transformOptions)

Loads a module, and all of its dependencies, and returns a function that can write out all, or parts, of the module and its dependency graph, so that they don't depend on steal.js.

Parameters

  1. config {StealConfig}

    Specifies configuration values to set on a Steal loader. The main option must be specified. Typically, configPath is also specified, as that is used to set baseURL. Any Steal configuration can be specified; however, most other build configuration values are specified by buildConfig in the config file.

    • main {String}

      The module whose dependencies should be built.

    • config {String}

      The path to a configuration file. This will also specify baseURL.

    • baseURL {String}

      If a configuration file is not used, the baseURL value must be set.

  2. transformOptions {Object}

    Configures the behavior of loading the modules, and acts as default values for the resulting transform function's options argument.

    • verbose {Boolean}

      Set to true to get a lot of warning messages.

    • quiet {Boolean}

      Set to true to log nothing.

Returns

{Promise<transform(moduleName, options)>}

A deferred, which resolves to a function that can write out all or part of the loaded dependency tree.

Use

stealTools.transformImport lets you transform modules to a different format or form. It's passed a StealConfig, which is used to load modules. Once all modules have been loaded, it provides a transform method that can write out modules:

  • in different formats (cjs, amd, global),
  • minified, or
  • with development code removed.

transformImport and transform are low-level functionality. For the majority of projects, export will be a better fit for the most common transformation behavior.

Like build, transformImport can be used from the command-line, from Grunt, or programmatically in Node.js. For this example, we're going to use transformImport programmatically, in order to showcase its more advanced functionality:

var transformImport = require("steal-tools").transformImport;
var fs = require("fs");

transformImport({
  config: __dirname + "/config.js",
  main: "main"
}).then(function(transform){
  // Get the main module and it's dependencies as a string
  var main = transform();

  // Get just a dependency and it's own dependencies as a string
  var myModule = transform("my_module");

  // Get the main module, ignoring a dependency we don't want.
  var mainAlone = transform("main", {
    ignore: ["my_module"]
  });

  // Now you can do whatever you want with the module.
  fs.writeFileSync("out_main.js", mainAlone, "utf8");
});

As you can see, transformImport takes an object containing the configuration and returns a Promise. The promise will return another function (named "transform" in this example) that can be used to generate a string containing a module and its dependencies. By default, the transform function will return the main module; but it can be used to generate any dependency in the graph.

Help us improve StealJS by taking our community survey