Skip to content

Commit

Permalink
Merge pull request #28 from Workiva/updates
Browse files Browse the repository at this point in the history
CP-1328 Move dart_to_js_script_rewriter to workiva ownership
  • Loading branch information
jayudey-wf committed Feb 4, 2016
2 parents ed91d12 + cf4d139 commit 72943dd
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.pub/
.settings/
build
coverage/
packages
pubspec.lock
.packages
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: dart
dart:
- stable
script:
- pub run dart_dev format --check
- pub run dart_dev analyze
- pub run dart_dev test
- pub run dart_dev coverage --no-html
- bash <(curl -s https://codecov.io/bash) -f coverage/coverage.lcov
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# dart_to_js_script_rewriter

<a href="https://pub.dartlang.org/packages/dart_to_js_script_rewriter"><img src="https://img.shields.io/pub/v/dart_to_js_script_rewriter.svg" alt="Pub" /></a>
<a href="https://travis-ci.org/Workiva/dart_to_js_script_rewriter"><img src="https://travis-ci.org/Workiva/dart_to_js_script_rewriter.svg?branch=travis-ci" alt="Build Status" /></a>
<a href="http://codecov.io/github/Workiva/dart_to_js_script_rewriter?branch=master"><img src="http://codecov.io/github/Workiva/dart_to_js_script_rewriter/coverage.svg?branch=master" alt="codecov.io" /></a>


A pub transformer that rewrites Dart script tags to
JavaScript script tags, eliminating
404s and speeding up initial loads.
Expand Down Expand Up @@ -71,5 +76,7 @@ See the [pub docs][pubdocs] for more on modes.

Please use the [issue tracker][issues].

[issues]: https://github.com/sethladd/dart_to_js_script_rewriter/issues
[issues]: https://github.com/Workiva/dart_to_js_script_rewriter/issues
[pubdocs]: https://www.dartlang.org/tools/pub/

Thanks to Seth Ladd, <[email protected]>, for creating the original version of this [library](https://github.com/sethladd/dart_to_js_script_rewriter).
File renamed without changes.
18 changes: 16 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ name: dart_to_js_script_rewriter
description: Replaces Dart script tags with JavaScript script tags,
speeding up initial loads and reducing 404s. Use when
ready to deploy.
author: Seth Ladd <[email protected]>
author:
- Seth Ladd <[email protected]>
- Workiva Client Platform Team <[email protected]>
- Dustin Lessard <[email protected]>
- Evan Weible <[email protected]>
- Jay Udey <[email protected]>
- Max Peterson <[email protected]>
- Trent Grover <[email protected]>
version: 0.1.0+4
homepage: https://github.com/sethladd/dart_to_js_script_rewriter
homepage: https://github.com/Workiva/dart_to_js_script_rewriter
dependencies:
barback: ">=0.15.0 <0.16.0"
html: ">=0.12.0 <0.13.0"
dev_dependencies:
browser: any
coverage: "^0.7.2"
dart_style: ">=0.1.8 <0.3.0"
dartdoc: "^0.4.0"
dart_dev: "^1.0.6"
mockito: any
test: any
transformers:
- dart_to_js_script_rewriter
- test/pub_serve:
$include: test/**_test{.*,}.dart
66 changes: 0 additions & 66 deletions test/all.dart

This file was deleted.

146 changes: 146 additions & 0 deletions test/dart_to_js_script_rewriter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
@TestOn('vm')
library dart_to_js_script_rewriter.test.dart_to_js_script_rewriter_test;

import 'dart:io';

import 'package:barback/barback.dart'
show Asset, AssetId, BarbackMode, BarbackSettings;
import 'package:html/dom.dart' show Document;
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'package:dart_to_js_script_rewriter/dart_to_js_script_rewriter.dart';
import 'transformer_mocks.dart';

void main() {
group('dart_to_js_script_rewriter', () {
test('removeDartDotJsTags', () {
DartToJsScriptRewriter transformer = _transformer();
Document document = new Document.html(_html);
transformer.removeDartDotJsTags(document);
var dartJsScripts = document.querySelectorAll('script').where((tag) {
Map attrs = tag.attributes;
return attrs['src'] != null && attrs['src'].endsWith('/dart.js');
});
expect(dartJsScripts, isEmpty);
});

test('rewriteDartTags', () {
DartToJsScriptRewriter transformer = _transformer();
Document document = new Document.html(_html);
transformer.rewriteDartTags(document);
var dartJsScripts = document.querySelectorAll('script').where((tag) {
Map attrs = tag.attributes;
return attrs['type'] == 'application/dart' && attrs['src'] != null;
});
expect(dartJsScripts, isEmpty);
});

test('allowedExtensions', () {
expect(
new DartToJsScriptRewriter.asPlugin(
new BarbackSettings(const {}, BarbackMode.RELEASE))
.allowedExtensions,
equals('.html'));
});

group('apply()', () {
BarbackSettings configuration;
final Matcher isHtmlFile =
predicate((Asset asset) => asset.id.extension == '.html');
DartToJsScriptRewriter transformer;

test('when run in release mode', () async {
configuration = new BarbackSettings(const {}, BarbackMode.RELEASE);
transformer = new DartToJsScriptRewriter.asPlugin(configuration);
AssetId fakeInputFileAssetId =
new AssetId('testid', 'test/test_data/test_file.html');

MockAsset inputFile;
MockTransform mockTransform;

String transformedFile;

inputFile = new MockAsset();
mockTransform = new MockTransform();

when(inputFile.id).thenReturn(fakeInputFileAssetId);
when(inputFile.readAsString()).thenReturn(
new File.fromUri(Uri.parse('test/test_data/test_file.html'))
.readAsString());

when(mockTransform.primaryInput).thenReturn(inputFile);
when(mockTransform.readInputAsString(fakeInputFileAssetId))
.thenAnswer((_) {
return new File.fromUri(Uri.parse('test/test_data/test_file.html'))
.readAsString();
});

await transformer.apply(mockTransform);

Asset fileAsset =
verify(mockTransform.addOutput(captureThat(isHtmlFile)))
.captured
.first;

transformedFile = await fileAsset.readAsString();
expect(
transformedFile.contains(
'<script async type="application/dart" src="test.dart"></script>'),
isFalse);
expect(
transformedFile.contains(
'<script async src="packages/browser/dart.js"></script>'),
isFalse);
expect(
transformedFile
.contains('<script async="" src="test.dart.js"></script>'),
isTrue);
});

test('when run in debug mode', () async {
configuration = new BarbackSettings(const {}, BarbackMode.DEBUG);
transformer = new DartToJsScriptRewriter.asPlugin(configuration);
AssetId fakeInputFileAssetId =
new AssetId('testid', 'test/test_data/test_file.html');

MockAsset inputFile;
MockTransform mockTransform;

inputFile = new MockAsset();
mockTransform = new MockTransform();

when(inputFile.id).thenReturn(fakeInputFileAssetId);
when(inputFile.readAsString()).thenReturn(
new File.fromUri(Uri.parse('test/test_data/test_file.html'))
.readAsString());

when(mockTransform.primaryInput).thenReturn(inputFile);
when(mockTransform.readInputAsString(fakeInputFileAssetId))
.thenAnswer((_) {
return new File.fromUri(Uri.parse('test/test_data/test_file.html'))
.readAsString();
});

await transformer.apply(mockTransform);

verifyNever(mockTransform.addOutput(any));
});
});
});
}

DartToJsScriptRewriter _transformer() {
return new DartToJsScriptRewriter.asPlugin(
new BarbackSettings({}, BarbackMode.RELEASE));
}

final String _html = """
<!DOCTYPE html>
<html>
<head>
<script async type="application/dart" src="test.dart"></script>
<script async src="packages/browser/dart.js"></script>
</head>
</html>
""";
7 changes: 7 additions & 0 deletions test/test_data/test_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script async type="application/dart" src="test.dart"></script>
<script async src="packages/browser/dart.js"></script>
</head>
</html>
8 changes: 8 additions & 0 deletions test/transformer_mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library dart_to_js_script_rewriter.test.transformer_mocks;

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

class MockAsset extends Mock implements Asset {}

class MockTransform extends Mock implements Transform {}
18 changes: 18 additions & 0 deletions tool/dev.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
library tool.dev;

import 'package:dart_dev/dart_dev.dart' show dev, config;

main(List<String> args) async {
List<String> directories = ['example/', 'lib/', 'test/', 'tool/'];

config.analyze.entryPoints = directories;
config.format.directories = directories;
config.test
..platforms = ['vm']
..pubServe = true
..unitTests = ['test/'];

config.coverage.pubServe = true;

await dev(args);
}

0 comments on commit 72943dd

Please sign in to comment.