Skip to content

Commit c79f0c3

Browse files
committed
refactor: simplify and make tests work in JS and Dart
* 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
1 parent 817c005 commit c79f0c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+339
-509
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Build
2+
3+
### Prerequisites:
4+
5+
1. `npm install`
6+
2. `pub get`
7+
3. `install -g gulp`
8+
4. `install -g karma`
9+
10+
### Folder structure
11+
12+
* `modules/*`: modules that will be loaded in the browser
13+
* `tools/*`: tools that are needed to build Angular
14+
15+
### File endings
16+
17+
* `*.js`: javascript files that get transpiled to Dart and EcmaScript 5
18+
* `*.es6`: javascript files that get transpiled only to EcmaScript 5
19+
* `*.es5`: javascript files that don't get transpiled
20+
* `*.dart`: dart files that don't get transpiled
21+
22+
### Build:
23+
24+
1. `gulp build` -> result is in `build` folder
25+
26+
* will also to `pubg get` for the subfolders in `modules`
27+
and run `dartanalyzer` for every file that matches
28+
`<module>/src/<module>.dart`, e.g. `di/src/di.dart`
29+
30+
2. `gulp clean` -> cleans the `build` folder
31+
32+
### Tests:
33+
34+
1. `karma start karma-js.conf.js`: JS tests
35+
2. `karma start karma-dart.conf.js`: JS tests
36+
37+
Notes for all tests:
38+
39+
The karma preprocessor is setup in a way so that after every test run
40+
the transpiler is reloaded. With that it is possible to make changes
41+
to the preprocessor and run the tests without exiting karma
42+
(just touch a test file that you would like to run).
43+
44+
Restriction for Dart tests (for now):
45+
46+
* Due to a bug `karma-dart` plugin,
47+
this will use the files in the `build` folder for resolving
48+
`package:` dependencies (created e.g. for `import ... from 'di:di'`).
49+
So you need to execute `gulp build` before this.

TODO.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
## Setup
2-
- use package.json's out of the individual projects
3-
42
- auto start Chromium when start serving
53
- auto refresh Chromium when s/t changed
64
- transform index.html:

file2modulename.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function file2moduleName(filePath) {
2+
return filePath
3+
// module name should not include non word characters (e.g. '-')
4+
// -> important for Dart
5+
.replace(/[^\w.\/]/g, '_')
6+
// module name should be relative to `modules` and `tools` folder
7+
.replace(/.*\/modules\//, '')
8+
.replace(/.*\/tools\//, '')
9+
// module name should not include `src`, `test`, `lib`
10+
.replace(/\/src\//, '/')
11+
.replace(/\/lib\//, '/')
12+
.replace(/\/test\//, '/')
13+
// module name should not have a suffix
14+
.replace(/\.\w*$/, '');
15+
}
16+
if (typeof module !== 'undefined') {
17+
module.exports = file2moduleName;
18+
}

gulpfile.js

+15-46
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ var glob = require('glob');
1010
var ejs = require('gulp-ejs');
1111
var path = require('path');
1212
var through2 = require('through2');
13-
14-
// import transpiler build tasks
15-
var transpilerTasks = require('./tools/transpiler/gulp-tasks');
16-
transpilerTasks.install(gulp);
13+
var file2moduleName = require('./file2modulename');
1714

1815
var js2es5Options = {
1916
annotations: true, // parse annotations
@@ -24,7 +21,7 @@ var js2es5Options = {
2421
typeAssertions: true
2522
};
2623

27-
var transpilerOptions = {
24+
var js2dartOptions = {
2825
annotations: true, // parse annotations
2926
types: true, // parse types
3027
script: false, // parse as a module
@@ -33,37 +30,20 @@ var transpilerOptions = {
3330

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

36-
function resolveModuleName(fileName) {
37-
var moduleName = fileName
38-
.replace(/.*\/modules\//, '')
39-
.replace(/\/src\//, '/')
40-
.replace(/\/lib\//, '/')
41-
.replace(/\/test\//, '/');
42-
return moduleName;
43-
}
44-
45-
4633
// ---------
47-
// rtts-assert and traceur runtime
34+
// traceur runtime
4835

4936
gulp.task('jsRuntime/build', function() {
50-
return createJsRuntimeTask(false);
51-
});
52-
53-
function createJsRuntimeTask(isWatch) {
54-
var srcFn = isWatch ? watch : gulp.src.bind(gulp);
55-
var traceurRuntime = srcFn(gulpTraceur.RUNTIME_PATH)
37+
var traceurRuntime = gulp.src(gulpTraceur.RUNTIME_PATH)
5638
.pipe(gulp.dest('build/js'));
5739
return traceurRuntime;
58-
}
40+
});
5941

6042
// -----------------------
6143
// modules
6244
var sourceTypeConfigs = {
6345
dart: {
64-
compiler: function() {
65-
return gulpTraceur(transpilerOptions, resolveModuleName);
66-
},
46+
compilerOptions: js2dartOptions,
6747
transpileSrc: ['modules/**/*.js'],
6848
htmlSrc: ['modules/*/src/**/*.html'],
6949
copySrc: ['modules/**/*.dart', 'modules/**/*.yaml'],
@@ -82,9 +62,7 @@ var sourceTypeConfigs = {
8262
}
8363
},
8464
js: {
85-
compiler: function() {
86-
return gulpTraceur(js2es5Options, resolveModuleName);
87-
},
65+
compilerOptions: js2es5Options,
8866
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
8967
htmlSrc: ['modules/*/src/**/*.html'],
9068
copySrc: ['modules/**/*.es5'],
@@ -103,7 +81,7 @@ gulp.task('modules/clean', function() {
10381
});
10482

10583
gulp.task('modules/build.dart/src', function() {
106-
return createModuleTask(sourceTypeConfigs.dart, false);
84+
return createModuleTask(sourceTypeConfigs.dart);
10785
});
10886

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

127105
gulp.task('modules/build.js', function() {
128-
return createModuleTask(sourceTypeConfigs.js, false);
106+
return createModuleTask(sourceTypeConfigs.js);
129107
});
130108

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

135-
function createModuleTask(sourceTypeConfig, isWatch) {
136-
var start = isWatch ? watch : gulp.src.bind(gulp);
137-
var transpile = start(sourceTypeConfig.transpileSrc)
113+
function createModuleTask(sourceTypeConfig) {
114+
var transpile = gulp.src(sourceTypeConfig.transpileSrc)
138115
.pipe(rename({extname: '.'+sourceTypeConfig.outputExt}))
139116
.pipe(rename(renameSrcToLib))
140-
.pipe(sourceTypeConfig.compiler())
117+
.pipe(gulpTraceur(sourceTypeConfig.compilerOptions, file2moduleName))
141118
.pipe(gulp.dest(sourceTypeConfig.outputDir));
142-
var copy = start(sourceTypeConfig.copySrc)
119+
var copy = gulp.src(sourceTypeConfig.copySrc)
143120
.pipe(rename(renameSrcToLib))
144121
.pipe(gulp.dest(sourceTypeConfig.outputDir));
145122
// TODO: provide the list of files to the template
146123
// automatically!
147-
var html = start(sourceTypeConfig.htmlSrc)
124+
var html = gulp.src(sourceTypeConfig.htmlSrc)
148125
.pipe(rename(renameSrcToLib))
149126
.pipe(ejs({
150127
type: sourceTypeConfig.outputExt
@@ -177,14 +154,6 @@ gulp.task('serve', connect.server({
177154
// --------------
178155
// general targets
179156

180-
gulp.task('clean', ['transpiler/clean', 'modules/clean']);
157+
gulp.task('clean', ['modules/clean']);
181158

182159
gulp.task('build', ['jsRuntime/build', 'modules/build.dart', 'modules/build.js']);
183-
184-
gulp.task('watch', function() {
185-
// parallel is important as both streams are infinite!
186-
runSequence(['transpiler/test/watch', 'transpiler/src/watch']);
187-
var dartModuleWatch = createModuleTask(sourceTypeConfigs.dart, true);
188-
var jsModuleWatch = createModuleTask(sourceTypeConfigs.js, true);
189-
return mergeStreams(dartModuleWatch, jsModuleWatch, createJsRuntimeTask(true));
190-
});

karma-dart.conf.js

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
// Karma configuration
22
// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT)
3+
var file2moduleName = require('./file2modulename');
34

45
module.exports = function(config) {
56
config.set({
67

78
frameworks: ['dart-unittest'],
89

910
files: [
10-
{pattern: 'packages/**/*.dart', included: false},
11-
{pattern: 'modules/*/src/**/*.js', included: false},
12-
{pattern: 'modules/*/test/**/*.js', included: true},
13-
{pattern: 'modules/**/*.dart', included: false},
14-
'packages/browser/dart.js'
11+
{pattern: 'modules/**/*_spec.js', included: true},
12+
{pattern: 'modules/*/src/**/*', included: false},
13+
{pattern: 'modules/*/test/**/*', included: false},
14+
{pattern: 'tools/transpiler/spec/**/*_spec.js', included: true},
15+
{pattern: 'tools/transpiler/spec/**/*', included: false},
16+
'test-main.dart'
1517
],
1618

1719
karmaDartImports: {
1820
guinness: 'package:guinness/guinness_html.dart'
1921
},
2022

2123
preprocessors: {
22-
'modules/**/*.js': ['traceur']
24+
'modules/**/*.js': ['traceur'],
25+
'tools/**/*.js': ['traceur']
2326
},
24-
customFileHandlers: [{
25-
urlRegex: /.*\/packages\/.*$/,
26-
handler: function(request, response, fa, fb, basePath) {
27-
var url = request.url;
28-
var path = url.indexOf('?') > -1 ? url.substring(0, url.indexOf('?')) : url;
29-
var contets = fs.readFileSync(basePath + path);
30-
response.writeHead(200);
31-
response.end(contets);
32-
}
33-
}],
3427
traceurPreprocessor: {
3528
options: {
3629
outputLanguage: 'dart',
@@ -41,13 +34,7 @@ module.exports = function(config) {
4134
// typeAssertionModule: 'assert',
4235
annotations: true
4336
},
44-
resolveModuleName: function(fileName) {
45-
var moduleName = fileName
46-
.replace(/.*\/modules\//, '')
47-
.replace(/\/src\//, '/')
48-
.replace(/\/test\//, '/');
49-
return moduleName;
50-
},
37+
resolveModuleName: file2moduleName,
5138
transformPath: function(fileName) {
5239
return fileName.replace('.js', '.dart');
5340
}

karma-js.conf.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Karma configuration
22
// Generated on Thu Sep 25 2014 11:52:02 GMT-0700 (PDT)
3+
var file2moduleName = require('./file2modulename');
34

45
module.exports = function(config) {
56
config.set({
@@ -8,16 +9,20 @@ module.exports = function(config) {
89

910
files: [
1011
'node_modules/traceur/bin/traceur-runtime.js',
11-
'./karma-mock-annotations.js',
1212
'modules/**/test_lib/**/*.es6',
1313
'modules/**/*.js',
1414
'modules/**/*.es6',
15+
'tools/transpiler/spec/**/*.js',
16+
'tools/transpiler/spec/**/*.es6',
17+
'file2modulename.js',
1518
'test-main.js'
1619
],
1720

1821
preprocessors: {
1922
'modules/**/*.js': ['traceur'],
20-
'modules/**/*.es6': ['traceur']
23+
'modules/**/*.es6': ['traceur'],
24+
'tools/transpiler/**/*.js': ['traceur'],
25+
'tools/transpiler/**/*.es6': ['traceur'],
2126
},
2227

2328
traceurPreprocessor: {
@@ -30,13 +35,7 @@ module.exports = function(config) {
3035
typeAssertionModule: 'rtts_assert/rtts_assert',
3136
annotations: true
3237
},
33-
resolveModuleName: function(fileName) {
34-
var moduleName = fileName
35-
.replace(/.*\/modules\//, '')
36-
.replace(/\/src\//, '/')
37-
.replace(/\/test\//, '/');
38-
return moduleName;
39-
},
38+
resolveModuleName: file2moduleName,
4039
transformPath: function(fileName) {
4140
return fileName.replace('.es6', '');
4241
}

karma-mock-annotations.js

-6
This file was deleted.

modules/change_detection/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ dependencies:
55
dev_dependencies:
66
test_lib:
77
path: ../test_lib
8+
facade:
9+
path: ../facade

modules/change_detection/src/change_detection.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {WatchGroup} from './watch_group';
22
import {Record} from './record';
3+
import {FIELD} from 'facade/lang';
34

45
export class ChangeDetection {
56

0 commit comments

Comments
 (0)