Skip to content
This repository has been archived by the owner on Jul 20, 2018. It is now read-only.

Transformer weird behavior on Windows #71

Open
kzhdev opened this issue Mar 22, 2016 · 0 comments
Open

Transformer weird behavior on Windows #71

kzhdev opened this issue Mar 22, 2016 · 0 comments

Comments

@kzhdev
Copy link

kzhdev commented Mar 22, 2016

I have a transformer which reads angularjs template html files and generate an angular template cache and write it into a .js file. The transformer works fine on Mac, but generates a problematic file on Windows.

Here is an example of generated content:

angular.module('directives/app-status/app-status.tpl.html', []).run(['$templateCache', function($templateCache) {
  $templateCache.put('directives/app-status/app-status.tpl.html',
    '<i class="app-status icon-circle"
' +
    '   ng-class="{\'offline\': status === 0, \'partial-ready\': status > 0 && status < 7, \'ready\': status === 7}"
' +
    '   tooltip-placement="bottom" tooltip="{{statusString}}">
' +
    '</i>');
}]);

Notice that: on Windows, it put ' + onto a separate line which is not a valid JavaScript Syntax.

Here is my transformer:

library adl_client.lib.adljs_transformer;

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

class ADLJsBuildTransformer extends AggregateTransformer {

  ADLJsBuildTransformer.asPlugin();

  classifyPrimary(AssetId id) {
    if (id.path.startsWith('lib/js_src/directives') && id.path.endsWith('tpl.html')) {
      return 'template';
    } else if ((id.path == 'lib/js_src/app.js'
        || id.path.startsWith('lib/js_src/directives')
        || id.path.startsWith('lib/js_src/filters')
        || id.path.startsWith('lib/js_src/services'))
        && id.path.endsWith('.js')) {
      return 'js';
    }
    return null;
  }

  Future apply(AggregateTransform transform) async {

    var assets = await transform.primaryInputs.toList();

    var content = new StringBuffer();

    AssetId id;

    switch(transform.key)
    {
      case 'template':
        var buffer = new StringBuffer();

        content.write("angular.module('templates', [\n");
        for (var asset in assets) {
          var template_name = asset.id.path.substring(11);
          content.write("  '${template_name}'");
          if (asset != assets.last) {
            content.write(',\n');
          }

          buffer.write("angular.module('${template_name}', []).run(['\$templateCache', function(\$templateCache) {\n");
          buffer.write("  \$templateCache.put('${template_name}',\n");

          var template = await asset.readAsString();
          var lines = template.split('\n').where((line) => line.isNotEmpty);
          for (var line in lines) {
            buffer.write('    \'${line.replaceAll('\'', '\\\'')}\'');

            if (line != lines.last) {
              buffer.write(' +\n');
            } else {
              buffer.write(');\n');
            }
          }

          buffer.write("}]);\n\n");
        }

        content.write(']);\n\n');
        content.write(buffer.toString());

        id = new AssetId(transform.package, 'web/js/templates.js');

        break;

      case 'js':
        for (var asset in assets) {
          content.write(await asset.readAsString());
          content.write("\n");
        }

        id = new AssetId(transform.package, 'web/js/adl.js');
        break;
    }

    transform.addOutput(new Asset.fromString(id, content.toString()));
  }
}

If I add a print(buffer.toString() between the content.write(']);\n\n'); and content.write(buffer.toString()), it prints out:

angular.module('directives/app-status/app-status.tpl.html', []).run(['$templateCache', function($templateCache) {
  $templateCache.put('directives/app-status/app-status.tpl.html',
' +
' +
' +
');
}]);

If I print out each line, the content is correct. The symptom looks like the await call var template = await asset.readAsString(); does not block at all, but somehow the line content is injected before each ' +.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant