Skip to content

CompilerPass

Mauro Gadaleta edited this page Mar 22, 2017 · 8 revisions

Compiler Pass

Sometimes, you need to do more than one thing during compilation, want to use compiler passes without an extension or you need to execute some code at another step in the compilation process. In these cases, you can create a new class with a process method

class CustomPass {
    /**
     * @param {ContainerBuilder} container
     */
    process (container) {
       // ... do something during the compilation
    }
}

You then need to register your custom pass with the container:

import {ContainerBuilder, JsFileLoader} from 'node-dependency-injection'

let container = new ContainerBuilder()
container.addCompilerPass(new CustomPass())

Controlling the pass ordering

The optimisation passes run first and include tasks such as resolving references within the definitions. When registering compiler passes using addCompilerPass(), you can configure when your compiler pass is run. By default, they are run before the optimisation passes.

You can use the following constants to determine when your pass is executed:

  • PassConfig.TYPE_BEFORE_OPTIMIZATION
  • PassConfig.TYPE_OPTIMIZE

For example, to run your custom pass after the default removal passes have been run, use:

import {PassConfig} from 'node-dependency-injection'

container.addCompilerPass(new CustomPass(), PassConfig.TYPE_OPTIMIZE)

The default pass config type is PassConfig.TYPE_BEFORE_OPTIMIZATION. PassConfig.TYPE_OPTIMIZE will override the actual container optimization on compile

container.compile()


#### You must compile the container in order to use your compiler pass