dart_to_js_script_rewriter 1.0.3

dart_to_js_script_rewriter #

Pub Build Status codecov.io

A pub transformer that rewrites Dart script tags to JavaScript script tags, eliminating 404s and speeding up initial loads. Useful when building for deployment.

Why? Load your app quicker! #

A traditional Dart application is deployed with HTML that looks something like this:

<script async type="application/dart" src="test.dart"></script>
<script async src="packages/browser/dart.js"></script>

This is a performance problem for initial startup, because:

  • Some browsers will attempt to download test.dart, but that file is not deployed. This causes unnecessary server strain and noisy 404s.
  • The browser needs to run dart.js to replace a script tag in the DOM, so that the actual JavaScript version of the app can be downloaded. This is an unnecessary delay, since today no production browser includes Dart VM and thus only the JavaScript version is required.

With this transformer, you can address the above issues, speed up the load time of your apps, and make happier users.

Configuring #

Add the transformer to your pubspec.yaml:

- dart_to_js_script_rewriter

(Assuming you already added this package to your pubspec.yaml file.)

How it works #

When run in "release" mode, this transformer does two things:

  • Removes script tags that point to browser/dart.js.
  • Rewrites a Dart script tag to a JavaScript script tag.

For example, this code:

<script async type="application/dart" src="test.dart"></script>
<script async src="packages/browser/dart.js"></script>

is turned into this code:

<script async src="test.dart.js"></script>

Pub, modes, and this transformer #

This transformer only runs when pub is running in release mode.

This transformer only makes sense when you want to build your app for a production deployment. You probably do not want to run this transformer during the normal develop/reload cycles.

Pub can run in different modes, which have different semantics. The debug mode, for example, can disable minification. The release mode can turn on optimizations.

By default, pub serve runs in debug mode. By default, pub build runs in release mode.

See the pub docs for more on modes.

Reporting issues #

Please use the issue tracker.

Thanks to Seth Ladd, sethladd@gmail.com, for creating the original version of this library.

1.0.2 #

  • Updated dependencies on html and dartdoc.

0.1.0+4 #

  • [fix] only rewrite dart.js from browser package

0.1.0+3 #

  • Update dependencies

0.1.0+2 #

  • Removed special handling of the csp mode - dart2js no longer uses a special output file name

0.1.0+1 #

  • Added tests

0.0.2 #

  • CSP support (thanks to claudiodangelis)

0.0.1+3 #

  • Bump dependency versions (thanks to kaendfinger)

0.0.1+2 #

  • Add a "why?" section to the README.

0.0.1+1 #

  • Tweak readme.

0.0.1 #

  • Initial version.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  dart_to_js_script_rewriter: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:dart_to_js_script_rewriter/dart_to_js_script_rewriter.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.12.0 <2.0.0