Skip to content

Commit

Permalink
Finished es6 integration into build
Browse files Browse the repository at this point in the history
 - babel source maps appear to work (w00t!)
 - changed process so babel compile happens before copy and templating
 - changed process so that babel tranpiles unit tests as well! :-D
 - fixed problems with unit tests (mostly because of bad injectors with lazy DI syntax)
 - added babel polyfill so things like WeakSets are supported in old browsers
 - tested some es6 features in home.js
 - added a plugin that allowed karma to report errors in source maps.
  • Loading branch information
atruskie committed Jul 20, 2015
1 parent 3dc91cc commit 042fdbd
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ npm-debug.log
node_modules/
build/
bin/
es6/
vendor/
src/assets/temp
.sass-cache
62 changes: 31 additions & 31 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-bump");
grunt.loadNpmTasks("grunt-sass");
grunt.loadNpmTasks("grunt-karma");
grunt.loadNpmTasks("grunt-ngmin");
grunt.loadNpmTasks("grunt-html2js");
grunt.loadNpmTasks("grunt-contrib-connect");
grunt.loadNpmTasks("grunt-babel");
Expand Down Expand Up @@ -168,7 +167,8 @@ module.exports = function (grunt) {
others: {
src: [
'<%= build_dir %>',
'<%= compile_dir %>'
'<%= compile_dir %>',
'<%= es6_dir %>'
]
}
},
Expand Down Expand Up @@ -234,7 +234,7 @@ module.exports = function (grunt) {
// for now since the angular templates use tpl as well,
// we'll cheat and just use a direct file reference
var bc = grunt.config('build_configs');
if (srcPath === bc.configFile) {
if (srcPath.indexOf(bc.configFile) >= 0) {

// then process as template!
return grunt.template.process(content, {data: bc});
Expand All @@ -245,9 +245,10 @@ module.exports = function (grunt) {
},
files: [
{
src: ['<%= app_files.js %>'],
dest: '<%= build_dir %>/',
cwd: '.',
src: ['<%= app_files.js %>', "**/!(*.spec).js.map"],
// copy transpiled es6 JS into dest dir
dest: "<%= build_dir %>/",
cwd: "<%= es6_dir %>",
expand: true,
nonull: true
}
Expand Down Expand Up @@ -280,6 +281,24 @@ module.exports = function (grunt) {
}
},

babel: {
options: {
sourceMap: true,
optional: ["es7.comprehensions"]
},
transpile_appjs: {
files: [
{
src: [ "<%= app_files.js %>", "<%= app_files.jsunit %>"],
dest: "<%= es6_dir %>",
cwd: ".",
expand: true,
nonull: true
}
]
}
},

/**
* `grunt concat` concatenates multiple source files into a single file.
*/
Expand Down Expand Up @@ -324,23 +343,6 @@ module.exports = function (grunt) {
}
},

/**
* `ng-min` annotates the sources before minifying. That is, it allows us
* to code without the array syntax.
*/
ngmin: {
compile: {
files: [
{
src: ['<%= app_files.js %>'],
cwd: '<%= build_dir %>',
dest: '<%= build_dir %>',
expand: true
}
]
}
},

/**
* Minify the sources!
*/
Expand Down Expand Up @@ -413,6 +415,7 @@ module.exports = function (grunt) {
sub: true,
boss: true,
eqnull: true,
esnext: true,

/* HACK: At some point this should be turned off!" */
force: true,
Expand Down Expand Up @@ -492,7 +495,6 @@ module.exports = function (grunt) {
'<%= html2js.common.dest %>',
'<%= html2js.app.dest %>',
'<%= vendor_files.css %>',
/*'<%= recess.build.dest %>',*/
'<%= build_dir %>/assets/styles/*'
]
},
Expand All @@ -507,7 +509,6 @@ module.exports = function (grunt) {
src: [
'<%= concat.compile_js.dest %>',
'<%= vendor_files.css %>',
/*'<%= recess.compile.dest %>',*/
'<%= build_dir %>/assets/styles/*.css'
]
}
Expand Down Expand Up @@ -629,9 +630,8 @@ module.exports = function (grunt) {
files: [
'<%= app_files.js %>'
],
// karma:unit:run is disabled because it breaks watch by stopping it
// see https://github.com/karma-runner/grunt-karma/issues/30
tasks: ['jshint:src', 'karma:unit:run', 'copy:build_appjs']
// recent modification: files are copied before unit tests are run!
tasks: ['jshint:src', 'babel:transpile_appjs', 'copy:build_appjs', 'karma:unit:run' ]
},

/**
Expand Down Expand Up @@ -680,7 +680,7 @@ module.exports = function (grunt) {
files: [
'<%= app_files.jsunit %>'
],
tasks: ['jshint:test', 'karma:unit:run'],
tasks: ['babel:transpile_appjs', 'jshint:test', 'karma:unit:run'],
options: {
livereload: false
}
Expand Down Expand Up @@ -714,7 +714,7 @@ module.exports = function (grunt) {
grunt.registerTask('build', [
'clean', 'html2js', 'jshint', 'sass:build',
'concat:build_css', 'copy:build_app_assets', 'copy:build_vendor_assets',
'copy:build_appjs', 'copy:build_vendorjs', 'index:build', 'karmaconfig',
'babel:transpile_appjs', 'copy:build_appjs', 'copy:build_vendorjs', 'index:build', 'karmaconfig',
'karma:continuous'
]);

Expand All @@ -723,7 +723,7 @@ module.exports = function (grunt) {
* minifying your code.
*/
grunt.registerTask('compile', [
'sass:compile', 'concat:build_css', 'copy:compile_assets', 'ngmin', 'concat:compile_js', 'uglify',
'sass:compile', 'concat:build_css', 'copy:compile_assets', 'concat:compile_js', 'uglify',
'index:compile'
]);

Expand Down
2 changes: 2 additions & 0 deletions buildConfig/build.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
* development and the `compile_dir` folder is where our app resides once it's
* completely built.
*/
es6_dir: "es6",
build_dir: 'build',
compile_dir: 'bin',

Expand Down Expand Up @@ -77,6 +78,7 @@ module.exports = {
'vendor/round-date/roundDate.js'
],
js: [
"node_modules/babel/node_modules/babel-core/browser-polyfill.js",
'vendor/jquery/dist/jquery.js',
'vendor/angular/angular.js',

Expand Down
13 changes: 7 additions & 6 deletions buildConfig/karma-unit.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module.exports = function (config) {
"vendor/objectdiff/objectDiff.js",
"vendor/jasmine-expect/dist/jasmine-matchers.js"
].concat(JSON.parse(fileJson).concat([
'src/**/*.js',
'src/**/*.spec.js'
'es6/**/*.js',
'es6/**/*.spec.js'
]));

// HACK!: use vendor files out of the build directory since they undergo a transform on build
Expand All @@ -33,7 +33,7 @@ module.exports = function (config) {
});

configObject.exclude = [
'src/assets/**/*.js'
'es6/assets/**/*.js'
];

configObject.frameworks = [ 'jasmine' ];
Expand All @@ -42,12 +42,13 @@ module.exports = function (config) {
'karma-firefox-launcher',
/*'karma-chrome-launcher',*/
require('../node_modules/karma-chrome-launcher'),
'karma-phantomjs-launcher'
'karma-phantomjs-launcher',
/*require('../node_modules/karma-phantomjs-launcher'),*/
"karma-sourcemap-loader"
];

configObject.preprocessors = {

'**/*.js': ['sourcemap']
};

/**
Expand All @@ -66,7 +67,7 @@ module.exports = function (config) {

configObject.reporters.push('coverage');
configObject.plugins.push('karma-coverage');
configObject.preprocessors['src/**/!(*.spec)+(.js)'] = 'coverage';
configObject.preprocessors['es6/**/!(*.spec)+(.js)'] = 'coverage';
}

/**
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@
"grunt-conventional-changelog": "^1.2",
"grunt-html2js": "~0.3.2",
"grunt-karma": "^0.10",
"grunt-ngmin": "0.0.3",
"grunt-sass": "^0.18.1",
"karma-chrome-launcher": "~0.1.1",
"karma-coverage": "^0.3",
"karma-firefox-launcher": "~0.1.3",
"karma-jasmine": "^0.3.x",
"karma-phantomjs-launcher": "~0.1.1",
"karma-sourcemap-loader": "^0.3.5",
"lodash": "^3.7",
"slash": "^1.0.0"
},
"private": true,
"dependencies": {}
"dependencies": {
"babel": "^5.5.4"
}
}
33 changes: 33 additions & 0 deletions src/app/home/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ angular.module('bawApp.home', [])
.controller('HomeCtrl', ['$scope', '$resource', '$routeParams', 'Project',

function HomeCtrl($scope, $resource, $routeParams, Project) {
/* // testing some es6 features
var numbers = (new Array(100)).map((x,i) => i);
console.log(numbers);
var evenNumbers = numbers.filter(x => !(x & 1) );
console.log("even:", evenNumbers);
var newObj = {
numbers,
toString() {
"use strict";
console.warn(this.numbers);
return numbers;
},
[ "sum_" + evenNumbers.reduce( (a, x) => a + x, 0)]: " test!"
};
console.info(newObj);
var [a, b, c] = [1, 2, 3];
var {numbers: ns} = newObj;
console.log(a, b, c, ns);
var s = new Set();
evenNumbers.forEach( x => s.add(x));
numbers.forEach( x => s.add(x));
console.log(s.size);
var w = new WeakSet();
w.add(newObj);
console.warn(w);*/


//// TODO: disabled - authentication required for home page - BAD!
//// TODO: properly package this mess ... way too much in this controller
// // to get projects to display
Expand Down
2 changes: 1 addition & 1 deletion src/app/home/home.tpl.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="content" data-ng-controller="HomeCtrl" style="position:relative;">
<div id="content" style="position:relative;">
<p>
Welcome to a place where you can listen, learn, share your discoveries and help out the environment!
The Bioacoustic Workbench is a repository for audio recordings of the environment and tools that aid in
Expand Down
5 changes: 3 additions & 2 deletions src/components/directives/baw-toggle/bawToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ angular.module("bawApp.directives.toggleSwitch", ["ng"])
element.classList.add("toggle-switch-animate");
}, 0);
},
controller: function($scope, $element, $attrs) {
controller: ["$scope", "$element", "$attrs",
function($scope, $element, $attrs) {

console.debug("controller function", $scope.$id);

Expand All @@ -91,6 +92,6 @@ angular.module("bawApp.directives.toggleSwitch", ["ng"])
$scope.model = !$scope.model;
}
};
}
}]
};
}]);
15 changes: 8 additions & 7 deletions src/components/services/predictiveCache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ describe("The predictiveCache service", function () {
describe("The predictive cache http interceptor", function () {
var predictiveCacheInterceptor, $httpBackend;

beforeEach(inject(function (_predictiveCacheInterceptor_, $injector) {
beforeEach(inject(["predictiveCacheInterceptor", "$injector", function (_predictiveCacheInterceptor_, $injector) {
predictiveCacheInterceptor = _predictiveCacheInterceptor_;
$httpBackend = $injector.get("$httpBackend");
}));
}]));

it("ensure the interceptor implements the expected methods", function () {
expect(predictiveCacheInterceptor).toImplement({
Expand Down Expand Up @@ -129,7 +129,8 @@ describe("The predictiveCache service", function () {
$http,
exampleUrl = "http://www.google.com?page=1&size=10",
httpEvents = [],
predictiveCache;
predictiveCache,
$httpBackend;

beforeEach(function () {
module(function ($provide) {
Expand All @@ -149,7 +150,7 @@ describe("The predictiveCache service", function () {
$httpProvider.interceptors.push("unitTestInterceptor");
});

inject(function ($injector) {
inject(["$injector", function ($injector) {
$httpBackend = $injector.get("$httpBackend");

// create .when for requests common to all tests
Expand All @@ -160,7 +161,7 @@ describe("The predictiveCache service", function () {
// set up the cache service (register everything)
predictiveCache = $injector.get("predictiveCache");
configuredProfile = predictiveCache(testProfile);
});
}]);

httpEvents = [];
});
Expand Down Expand Up @@ -403,9 +404,9 @@ describe("The predictiveCache service", function () {

describe("Validating the input profile", function () {
var predictiveCache;
beforeEach(inject(function (_predictiveCache_) {
beforeEach(inject(["predictiveCache", function (_predictiveCache_) {
predictiveCache = _predictiveCache_;
}));
}]));


it("requires an object to function", function () {
Expand Down
2 changes: 1 addition & 1 deletion src/components/services/queryBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe("The QueryBuilder", function () {
};

// this essentially represents paging with the default options
actual = q.page.disable().page({});
var actual = q.page.disable().page({});
expect(actual.toJSON(spaces)).toBe(j(expected));
});

Expand Down

0 comments on commit 042fdbd

Please sign in to comment.