Skip to content

Commit

Permalink
chore(build): make build/tasks universal for all Aurelia packages
Browse files Browse the repository at this point in the history
  • Loading branch information
niieani committed Jun 8, 2016
1 parent 5287f11 commit 2824259
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 122 deletions.
21 changes: 17 additions & 4 deletions build/babel-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path');
var paths = require('./paths');

exports.base = function() {
return {
var config = {
filename: '',
filenameRelative: '',
sourceMap: true,
Expand All @@ -11,14 +11,27 @@ exports.base = function() {
moduleIds: false,
comments: false,
compact: false,
code:true,
code: true,
presets: [ 'es2015-loose', 'stage-1' ],
plugins: [
'syntax-flow',
'transform-decorators-legacy',
'transform-flow-strip-types'
]
};
if (!paths.useTypeScriptForDTS) {
config.plugins.push(
['babel-dts-generator', {
packageName: paths.packageName,
typings: '',
suppressModulePath: true,
suppressComments: false,
memberOutputFilter: /^_.*/,
suppressAmbientDeclaration: true
}]
);
};
config.plugins.push('transform-flow-strip-types');
return config;
}

exports.commonjs = function() {
Expand All @@ -45,7 +58,7 @@ exports.es2015 = function() {
return options;
};

exports.modules = function() {
exports['native-modules'] = function() {
var options = exports.base();
options.presets[0] = 'es2015-loose-native-modules';
return options;
Expand Down
11 changes: 9 additions & 2 deletions build/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs');
var appRoot = 'src/';
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));

module.exports = {
var paths = {
root: appRoot,
source: appRoot + '**/*.js',
html: appRoot + '**/*.html',
Expand All @@ -14,5 +14,12 @@ module.exports = {
unitTests: 'test/**/*.js',
e2eSpecsSrc: 'test/e2e/src/*.js',
e2eSpecsDist: 'test/e2e/dist/',
packageName: pkg.name
packageName: pkg.name,
useTypeScriptForDTS: true
};

paths.files = ['resolvers.js', 'invokers.js', 'registrations.js', 'container.js', 'injection.js'].map(function(file){
return paths.root + file;
});

module.exports = paths;
119 changes: 47 additions & 72 deletions build/tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ var rename = require('gulp-rename');
var tools = require('aurelia-tools');
var ts = require('gulp-typescript');
var jsName = paths.packageName + '.js';
var compileToModules = ['es2015', 'commonjs', 'amd', 'system', 'native-modules'];

function cleanGeneratedCode() {
return through2.obj(function(file, enc, callback) {
file.contents = new Buffer(tools.cleanGeneratedCode(file.contents.toString('utf8')));
this.push(file);
return callback();
});
}

gulp.task('build-index', function() {
var importsToAdd = [];
var files = ['resolvers.js', 'invokers.js', 'registrations.js', 'container.js', 'injection.js'].map(function(file){
return paths.root + file;
});

return gulp.src(files)
return gulp.src(paths.files)
.pipe(through2.obj(function(file, enc, callback) {
file.contents = new Buffer(tools.extractImports(file.contents.toString("utf8"), importsToAdd));
file.contents = new Buffer(tools.extractImports(file.contents.toString('utf8'), importsToAdd));
this.push(file);
return callback();
}))
Expand All @@ -32,84 +38,49 @@ gulp.task('build-index', function() {
.pipe(gulp.dest(paths.output));
});

var indexSrc = gulp
.src(paths.output + paths.packageName + '.js')
.pipe(rename(function (path) {
if (path.extname == '.js') {
path.extname = '.ts'
}
}))

gulp.task('build-ts-es2015', function () {
var tsProjectES2015 = ts.createProject(compilerTsOptions.es2015(), ts.reporter.defaultReporter());
var tsResult = indexSrc.pipe(ts(tsProjectES2015));
return tsResult.js
.pipe(gulp.dest(paths.output + 'es2015'));
});

gulp.task('build-ts-commonjs', function () {
var tsProjectCommonJS = ts.createProject(compilerTsOptions.commonjs(), ts.reporter.defaultReporter());
var tsResult = indexSrc.pipe(ts(tsProjectCommonJS));
return tsResult.js
.pipe(gulp.dest(paths.output + 'commonjs'));
});
function indexForTypeScript() {
return gulp
.src(paths.output + paths.packageName + '.js')
.pipe(rename(function (path) {
if (path.extname == '.js') {
path.extname = '.ts';
}
}));
}

gulp.task('build-ts-amd', function () {
var tsProjectAmd = ts.createProject(compilerTsOptions.amd(), ts.reporter.defaultReporter());
var tsResult = indexSrc.pipe(ts(tsProjectAmd));
return tsResult.js
.pipe(gulp.dest(paths.output + 'amd'));
});

gulp.task('build-ts-system', function () {
var tsProjectSystem = ts.createProject(compilerTsOptions.system(), ts.reporter.defaultReporter());
var tsResult = indexSrc.pipe(ts(tsProjectSystem));
return tsResult.js
.pipe(gulp.dest(paths.output + 'system'));
compileToModules.forEach(function(moduleType){
gulp.task('build-babel-' + moduleType, function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions[moduleType]())))
.pipe(cleanGeneratedCode())
.pipe(gulp.dest(paths.output + moduleType));
});

if (moduleType === 'native-modules') return; // typescript doesn't support the combination of: es5 + native modules
gulp.task('build-ts-' + moduleType, function () {
var tsProject = ts.createProject(
compilerTsOptions({ module: moduleType, target: moduleType == 'es2015' ? 'es2015' : 'es5' }), ts.reporter.defaultReporter());
var tsResult = indexForTypeScript().pipe(ts(tsProject));
return tsResult.js
.pipe(gulp.dest(paths.output + moduleType));
});
});

gulp.task('build-dts', function() {
var tsProjectDTS = ts.createProject(compilerTsOptions.dts(), ts.reporter.defaultReporter());
var tsResult = indexSrc.pipe(ts(tsProjectDTS));
var tsProject = ts.createProject(
compilerTsOptions({ removeComments: false, target: "es2015", module: "es2015" }), ts.reporter.defaultReporter());
var tsResult = indexForTypeScript().pipe(ts(tsProject));
return tsResult.dts
.pipe(gulp.dest(paths.output));
})

gulp.task('build-babel-es2015', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions.es2015())))
.pipe(gulp.dest(paths.output + 'es2015'));
});

gulp.task('build-babel-commonjs', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions.commonjs())))
.pipe(gulp.dest(paths.output + 'commonjs'));
});

gulp.task('build-babel-amd', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions.amd())))
.pipe(gulp.dest(paths.output + 'amd'));
});

gulp.task('build-babel-system', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions.system())))
.pipe(gulp.dest(paths.output + 'system'));
});

gulp.task('build-babel-modules', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions.modules())))
.pipe(gulp.dest(paths.output + 'modules'));
});

gulp.task('build', function(callback) {
return runSequence(
'clean',
'build-index',
['build-babel-es2015', 'build-babel-commonjs', 'build-babel-amd', 'build-babel-system', 'build-babel-modules', 'build-dts'],
compileToModules
.map(function(moduleType) { return 'build-babel-' + moduleType })
.concat(paths.useTypeScriptForDTS ? ['build-dts'] : []),
callback
);
});
Expand All @@ -118,7 +89,11 @@ gulp.task('build-ts', function(callback) {
return runSequence(
'clean',
'build-index',
['build-ts-es2015', 'build-ts-commonjs', 'build-ts-amd', 'build-ts-system', /** TypeScript cannot yet transpile to es5 with es2015 modules */ 'build-babel-modules', 'build-dts'],
'build-babel-native-modules',
compileToModules
.filter(function(moduleType) { return moduleType !== 'native-modules' })
.map(function(moduleType) { return 'build-ts-' + moduleType })
.concat(paths.useTypeScriptForDTS ? ['build-dts'] : []),
callback
);
});
51 changes: 7 additions & 44 deletions build/typescript-options.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,9 @@
exports.base = function() {
return {
"target": "es5",
"module": "es2015",
"experimentalDecorators": true,
"emitDecoratorMetadata": false,
"moduleResolution": "node",
"stripInternal": true,
"preserveConstEnums": true,
"declaration": true,
"noExternalResolve": false,
"removeComments": true,
"lib": ["es2015", "dom"],
"typescript": require('typescript')
}
}

exports.commonjs = function() {
var options = exports.base();
options.module = 'commonjs';
return options;
};

exports.amd = function() {
var options = exports.base();
options.module = 'amd';
return options;
};
var tsconfig = require('../tsconfig.json');
var assign = Object.assign || require('object.assign');

exports.system = function() {
var options = exports.base();
options.module = 'system';
return options;
};

exports.es2015 = function() {
var options = exports.base();
options.target = 'es2015';
return options;
};

exports.dts = function() {
var options = exports.base();
options.removeComments = false;
return options;
module.exports = function(override) {
return assign(tsconfig.compilerOptions, {
"target": override && override.target || "es5",
"typescript": require('typescript')
}, override || {});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"devDependencies": {
"aurelia-tools": "^0.2.1",
"babel-dts-generator": "^0.5.1",
"babel-eslint": "^4.1.1",
"babel-plugin-syntax-flow": "^6.5.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"stripInternal": true,
"preserveConstEnums": true,
"listFiles": true,
"declaration": true,
"removeComments": true,
"lib": ["es2015", "dom"]
},
"exclude": [
Expand Down

0 comments on commit 2824259

Please sign in to comment.