matthewrobb / es6-module-transpiler-resolver-factory

ES6 Module Transpiler - Extendable multi-step FileResolver
MIT License
4 stars 0 forks source link

es6-module-transpiler-resolver-factory

ES6 Module Transpiler - Extendable multi-step FileResolver

Overview

ES6 Module Transpiler es6-module-transpiler is an experimental compiler that allows you to write your JavaScript using a subset of the current ES6 module syntax, and compile it into various formats.

When used as a library in an importing node module it is required that one or more FileResolvers be provided. es6-module-transpiler-resolver-factory makes it easier to extend FileResolver and perform custom logic for doing things such as resolving paths and manipulating ASTs.

Usage

Build tools

Since this resolver is a plugin for es6-module-transpiler, you can use it with any existing build tool that supports es6-module-transpiler as the underlying engine to transpile the ES6 modules.

You just need to make sure that es6-module-transpiler-resolver-factory is accessible for those tools, and pass the proper resolvers option thru the es6-module-transpiler's configuration.

Library

You should use the hooked resolver with the transpiler as a library:

var transpiler      = require("es6-module-transpiler");
var BundleFormatter = transpiler.formatters.bundle;
var Container       = transpiler.Container;
var ResolverFactory  = require("es6-module-transpiler-resolver-factory");

var container = new Container({
  resolvers: [new ResolverFactory(["lib/"])],
  formatter: new BundleFormatter()
});

container.getModule("index");
container.write("out/mylib.js");

Extending

You can also extend the ResolverFactory and provide overrides for each step:

var ResolverFactory = require("es6-module-transpiler-resolver-factory");
var ESNextResolver  = ResolverFactory.extend({
    translate : function(source, load) {
        var ast = recast.parse(source, {
            sourceFileName : path.basename(load.resolvedPath)
        });

        // Run the ast through the esnext transformer before the module transformer
        return esnext.transform(ast);
    }
});

var container = new Container({
  resolvers: [new ESNextResolver(["lib/"])],
  formatter: new BundleFormatter()
});

Resolver Steps

es6-module-transpiler-resolver-factory will go through a series of steps to resolve to a module instance. These steps are named such as to parallel the ES6 Loader hooks.

Each step function will always receive a load object as it's last parameter. This object tracks meta data about the module as it progresses from step to step.

load object

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks, and enjoy living in the ES6 future!