Web compilers for users of package:build.
Issues related to build_web_compilers Pub Package Version Latest Dartdocs Join the chat on Gitter

Installation

This package is intended to be used as a development dependency for users of package:build who want to run code in a browser. Simply add the following to your pubspec.yaml:

dev_dependencies:
  build_web_compilers:

Usage

If you are using the autogenerated build script (going through dart run build_runner <command> instead of handwriting a build.dart file), then all you need is the dev_dependency listed above.

Configuration

Configuring the default compiler

By default, the Dart development compiler (dartdevc, also known as DDC) will be used.

If you would like to opt into dart2js you will need to add a build.yaml file, which should look roughly like the following:

targets:
  $default:
    builders:
      build_web_compilers:entrypoint:
        # These are globs for the entrypoints you want to compile.
        generate_for:
        - test/**.browser_test.dart
        - web/**.dart
        options:
          compiler: dart2js
          # List any dart2js specific args here, or omit it.
          dart2js_args:
          - -O2

Configuring -D environment variables

dartdevc is a modular compiler, so in order to ensure consistent builds in every module environment variables must be configured globally. Configure with a mapping in YAML. Environment defined variables can be read with const String.fromEnvironment and const bool.fromEnvironment. For example:

global_options:
  build_web_compilers:ddc:
    options:
      environment:
        SOME_VAR: some value
        ANOTHER_VAR: false

For dart2js, use the dart2js_args option. This may be configured globally, or per target.

targets:
  $default:
    builders:
      build_web_compilers:entrypoint:
        options:
          dart2js_args:
          - -DSOME_VAR=some value
          - -DANOTHER_VAR=true

These may also be specified on the command line with a --define argument.

webdev serve -- '--define=build_web_compilers:ddc=environment={"SOME_VAR":"changed"}'

Manual Usage

If you are using a custom build script, you will need to add the following builder applications to what you already have, almost certainly at the end of the list (unless you need to post-process the js files).

[
    apply(
        'build_web_compilers:ddc',
        [
        (_) => new ModuleBuilder(),
        (_) => new UnlinkedSummaryBuilder(),
        (_) => new LinkedSummaryBuilder(),
        (_) => new DevCompilerBuilder()
        ],
        toAllPackages(),
        // Recommended, but not required. This makes it so only modules that are
        // imported by entrypoints get compiled.
        isOptional: true,
        hideOutput: true),
    apply('build_web_compilers:entrypoint',
        // You can also use `WebCompiler.Dart2Js`. If you don't care about
        // dartdevc at all you may also omit the previous builder application
        // entirely.
        [(_) => new WebEntrypointBuilder(WebCompiler.DartDevc)], toRoot(),
        hideOutput: true,
        // These globs should match your entrypoints only.
        defaultGenerateFor: const InputSet(
            include: const ['web/**', 'test/**.browser_test.dart'])),
]