2
votes

I'm building Polymer.dart app that uses Django as a backend. And i want to keep all static files in "/static/" directory, or use cdn server for example. But "pub build" makes relative urls for scripts and other assets. Is there any way to inject absolute prefix for release in url during "pub build"?

UPD: So simplest solution was to make a transformer:

import 'dart:async';
import 'package:barback/barback.dart';

class UrlTransformer extends Transformer {
  UrlTransformer.asPlugin();

  String get allowedExtensions => '.html';

  Future apply(Transform transform) {
    return transform.primaryInput.readAsString().then((contents) {
      transform.addOutput(new Asset.fromString(
          transform.primaryInput.id,
          contents.replaceAllMapped(
              new RegExp(r'src="([^\{"]+)"'),
                  (Match m) => "src=\"/static/${m[1]}\"").replaceAll("href=\"", "href=\"/static/")
          )
      );
    });
  }
}

But i dont know how to put results of dart2js transformer to apropriate folder. Resulting JS files are in the same directory as *.html now. I tried to put my transformer after $dart2js in pubspec but it throws errors when pub build at transformer initialisation step. So i can't make the transformer for that.

1
You could create a transformer for pub build that adds a prefix / changes the urls in your HTML or CSS files. - Fox32
Do you mean your_package/static, your_package/web/static or your_package/lib/static. You can have absolute URLs for assets in lib/...` you reference using the src attribute. Imports are handled a bit differently. - Günter Zöchbauer
I mean link to assets in production html should be static, like src="http://eaxmple.com/static/js/my_app.dart.js" or relevant to the web root. - k-maks

1 Answers

0
votes

There is an open Polymer issue to support this http://dartbug.com/20691

The linked discussion mentions a workaround:

The server could redirect all requests containing [.../]package/...