Skip to content

Commit

Permalink
refactor: simplify and make tests work in JS and Dart
Browse files Browse the repository at this point in the history
* remove `wraps` syntax enhancements for imports
  and support new `import * as module from ...` syntax

  - default imports are the wrong construct for importing
    everything from a module

* moved tests from transpiler to jasmine and karma

  - transpiler tests are included when running karma in main project folder
  - transpiler is reloaded after every test run in karma,
    so no need to restart karma when the transpiler has been changed.
  - removed own gulp build for transpiler and `postinstall.sh`
    as they are no more needed.
  - transpiler tests are now executed in Dart AND JavaScript (used to be executed
    only in Dart), which allowed to catch some bugs (see the bug with the
    import specification above).

* made tests work in dart as well by using the following hack:

  - dependencies are loaded from the `build` folder, which makes
    running `gulp build` necessary before running karma for dart
  - for this to work,
    the dependencies are included in main `pubspec.yaml` of project
  - reason for the hack: `karma-dart` loads all `packages` urls
    directly from disc (should rather use the karma file list)

* added explicit annotations `FIELD`, `ABSTRACT`, ... to `facade/lang.*`

  - needed for now that we can run tests and don't get errors for undefined
    annotations.

* added `README.md` with details about the build and tests
  • Loading branch information
tbosch committed Sep 29, 2014
1 parent 817c005 commit c79f0c3
Show file tree
Hide file tree
Showing 47 changed files with 339 additions and 509 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Build

### Prerequisites:

1. `npm install`
2. `pub get`
3. `install -g gulp`
4. `install -g karma`

### Folder structure

* `modules/*`: modules that will be loaded in the browser
* `tools/*`: tools that are needed to build Angular

### File endings

* `*.js`: javascript files that get transpiled to Dart and EcmaScript 5
* `*.es6`: javascript files that get transpiled only to EcmaScript 5
* `*.es5`: javascript files that don't get transpiled
* `*.dart`: dart files that don't get transpiled

### Build:

1. `gulp build` -> result is in `build` folder

* will also to `pubg get` for the subfolders in `modules`
and run `dartanalyzer` for every file that matches
`<module>/src/<module>.dart`, e.g. `di/src/di.dart`

2. `gulp clean` -> cleans the `build` folder

### Tests:

1. `karma start karma-js.conf.js`: JS tests
2. `karma start karma-dart.conf.js`: JS tests

Notes for all tests:

The karma preprocessor is setup in a way so that after every test run
the transpiler is reloaded. With that it is possible to make changes
to the preprocessor and run the tests without exiting karma
(just touch a test file that you would like to run).

Restriction for Dart tests (for now):

* Due to a bug `karma-dart` plugin,
this will use the files in the `build` folder for resolving
`package:` dependencies (created e.g. for `import ... from 'di:di'`).
So you need to execute `gulp build` before this.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
## Setup
- use package.json's out of the individual projects

- auto start Chromium when start serving
- auto refresh Chromium when s/t changed
- transform index.html:
Expand Down
18 changes: 18 additions & 0 deletions file2modulename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function file2moduleName(filePath) {
return filePath
// module name should not include non word characters (e.g. '-')
// -> important for Dart
.replace(/[^\w.\/]/g, '_')
// module name should be relative to `modules` and `tools` folder
.replace(/.*\/modules\//, '')
.replace(/.*\/tools\//, '')
// module name should not include `src`, `test`, `lib`
.replace(/\/src\//, '/')
.replace(/\/lib\//, '/')
.replace(/\/test\//, '/')
// module name should not have a suffix
.replace(/\.\w*$/, '');
}
if (typeof module !== 'undefined') {
module.exports = file2moduleName;
}
61 changes: 15 additions & 46 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ var glob = require('glob');
var ejs = require('gulp-ejs');
var path = require('path');
var through2 = require('through2');

// import transpiler build tasks
var transpilerTasks = require('./tools/transpiler/gulp-tasks');
transpilerTasks.install(gulp);
var file2moduleName = require('./file2modulename');

var js2es5Options = {
annotations: true, // parse annotations
Expand All @@ -24,7 +21,7 @@ var js2es5Options = {
typeAssertions: true
};

var transpilerOptions = {
var js2dartOptions = {
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
Expand All @@ -33,37 +30,20 @@ var transpilerOptions = {

var gulpTraceur = require('./tools/transpiler/gulp-traceur');

function resolveModuleName(fileName) {
var moduleName = fileName
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/lib\//, '/')
.replace(/\/test\//, '/');
return moduleName;
}


// ---------
// rtts-assert and traceur runtime
// traceur runtime

gulp.task('jsRuntime/build', function() {
return createJsRuntimeTask(false);
});

function createJsRuntimeTask(isWatch) {
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
var traceurRuntime = srcFn(gulpTraceur.RUNTIME_PATH)
var traceurRuntime = gulp.src(gulpTraceur.RUNTIME_PATH)
.pipe(gulp.dest('build/js'));
return traceurRuntime;
}
});

// -----------------------
// modules
var sourceTypeConfigs = {
dart: {
compiler: function() {
return gulpTraceur(transpilerOptions, resolveModuleName);
},
compilerOptions: js2dartOptions,
transpileSrc: ['modules/**/*.js'],
htmlSrc: ['modules/*/src/**/*.html'],
copySrc: ['modules/**/*.dart', 'modules/**/*.yaml'],
Expand All @@ -82,9 +62,7 @@ var sourceTypeConfigs = {
}
},
js: {
compiler: function() {
return gulpTraceur(js2es5Options, resolveModuleName);
},
compilerOptions: js2es5Options,
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
htmlSrc: ['modules/*/src/**/*.html'],
copySrc: ['modules/**/*.es5'],
Expand All @@ -103,7 +81,7 @@ gulp.task('modules/clean', function() {
});

gulp.task('modules/build.dart/src', function() {
return createModuleTask(sourceTypeConfigs.dart, false);
return createModuleTask(sourceTypeConfigs.dart);
});

gulp.task('modules/build.dart/analyzer', function() {
Expand All @@ -125,26 +103,25 @@ gulp.task('modules/build.dart', function(done) {
});

gulp.task('modules/build.js', function() {
return createModuleTask(sourceTypeConfigs.js, false);
return createModuleTask(sourceTypeConfigs.js);
});

function renameSrcToLib(file) {
file.dirname = file.dirname.replace(/\bsrc\b/, 'lib');
}

function createModuleTask(sourceTypeConfig, isWatch) {
var start = isWatch ? watch : gulp.src.bind(gulp);
var transpile = start(sourceTypeConfig.transpileSrc)
function createModuleTask(sourceTypeConfig) {
var transpile = gulp.src(sourceTypeConfig.transpileSrc)
.pipe(rename({extname: '.'+sourceTypeConfig.outputExt}))
.pipe(rename(renameSrcToLib))
.pipe(sourceTypeConfig.compiler())
.pipe(gulpTraceur(sourceTypeConfig.compilerOptions, file2moduleName))
.pipe(gulp.dest(sourceTypeConfig.outputDir));
var copy = start(sourceTypeConfig.copySrc)
var copy = gulp.src(sourceTypeConfig.copySrc)
.pipe(rename(renameSrcToLib))
.pipe(gulp.dest(sourceTypeConfig.outputDir));
// TODO: provide the list of files to the template
// automatically!
var html = start(sourceTypeConfig.htmlSrc)
var html = gulp.src(sourceTypeConfig.htmlSrc)
.pipe(rename(renameSrcToLib))
.pipe(ejs({
type: sourceTypeConfig.outputExt
Expand Down Expand Up @@ -177,14 +154,6 @@ gulp.task('serve', connect.server({
// --------------
// general targets

gulp.task('clean', ['transpiler/clean', 'modules/clean']);
gulp.task('clean', ['modules/clean']);

gulp.task('build', ['jsRuntime/build', 'modules/build.dart', 'modules/build.js']);

gulp.task('watch', function() {
// parallel is important as both streams are infinite!
runSequence(['transpiler/test/watch', 'transpiler/src/watch']);
var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true);
var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true);
return mergeStreams(dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
});
33 changes: 10 additions & 23 deletions karma-dart.conf.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
// Karma configuration
// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT)
var file2moduleName = require('./file2modulename');

module.exports = function(config) {
config.set({

frameworks: ['dart-unittest'],

files: [
{pattern: 'packages/**/*.dart', included: false},
{pattern: 'modules/*/src/**/*.js', included: false},
{pattern: 'modules/*/test/**/*.js', included: true},
{pattern: 'modules/**/*.dart', included: false},
'packages/browser/dart.js'
{pattern: 'modules/**/*_spec.js', included: true},
{pattern: 'modules/*/src/**/*', included: false},
{pattern: 'modules/*/test/**/*', included: false},
{pattern: 'tools/transpiler/spec/**/*_spec.js', included: true},
{pattern: 'tools/transpiler/spec/**/*', included: false},
'test-main.dart'
],

karmaDartImports: {
guinness: 'package:guinness/guinness_html.dart'
},

preprocessors: {
'modules/**/*.js': ['traceur']
'modules/**/*.js': ['traceur'],
'tools/**/*.js': ['traceur']
},
customFileHandlers: [{
urlRegex: /.*\/packages\/.*$/,
handler: function(request, response, fa, fb, basePath) {
var url = request.url;
var path = url.indexOf('?') > -1 ? url.substring(0, url.indexOf('?')) : url;
var contets = fs.readFileSync(basePath + path);
response.writeHead(200);
response.end(contets);
}
}],
traceurPreprocessor: {
options: {
outputLanguage: 'dart',
Expand All @@ -41,13 +34,7 @@ module.exports = function(config) {
// typeAssertionModule: 'assert',
annotations: true
},
resolveModuleName: function(fileName) {
var moduleName = fileName
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/test\//, '/');
return moduleName;
},
resolveModuleName: file2moduleName,
transformPath: function(fileName) {
return fileName.replace('.js', '.dart');
}
Expand Down
17 changes: 8 additions & 9 deletions karma-js.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Karma configuration
// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT)
var file2moduleName = require('./file2modulename');

module.exports = function(config) {
config.set({
Expand All @@ -8,16 +9,20 @@ module.exports = function(config) {

files: [
'node_modules/traceur/bin/traceur-runtime.js',
'./karma-mock-annotations.js',
'modules/**/test_lib/**/*.es6',
'modules/**/*.js',
'modules/**/*.es6',
'tools/transpiler/spec/**/*.js',
'tools/transpiler/spec/**/*.es6',
'file2modulename.js',
'test-main.js'
],

preprocessors: {
'modules/**/*.js': ['traceur'],
'modules/**/*.es6': ['traceur']
'modules/**/*.es6': ['traceur'],
'tools/transpiler/**/*.js': ['traceur'],
'tools/transpiler/**/*.es6': ['traceur'],
},

traceurPreprocessor: {
Expand All @@ -30,13 +35,7 @@ module.exports = function(config) {
typeAssertionModule: 'rtts_assert/rtts_assert',
annotations: true
},
resolveModuleName: function(fileName) {
var moduleName = fileName
.replace(/.*\/modules\//, '')
.replace(/\/src\//, '/')
.replace(/\/test\//, '/');
return moduleName;
},
resolveModuleName: file2moduleName,
transformPath: function(fileName) {
return fileName.replace('.es6', '');
}
Expand Down
6 changes: 0 additions & 6 deletions karma-mock-annotations.js

This file was deleted.

2 changes: 2 additions & 0 deletions modules/change_detection/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dependencies:
dev_dependencies:
test_lib:
path: ../test_lib
facade:
path: ../facade
1 change: 1 addition & 0 deletions modules/change_detection/src/change_detection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {WatchGroup} from './watch_group';
import {Record} from './record';
import {FIELD} from 'facade/lang';

export class ChangeDetection {

Expand Down
Loading

0 comments on commit c79f0c3

Please sign in to comment.