forked from benchpressjs/benchpressjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
145 lines (124 loc) · 3.24 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
/* eslint
import/no-extraneous-dependencies: [error, { devDependencies: true }],
no-console: off,
global-require: off,
*/
const fs = require('fs');
const async = require('async');
const mkdirp = require('mkdirp');
const babel = require('babel-core');
const config = {
uglify: {
options: {
banner: '/*! <%= pkg.name %> by psychobunny, built on <%= grunt.template.today("yyyy-mm-dd") %> */\n',
},
build: {
files: {
'build/benchpress.min.js': ['build/benchpress.js'],
},
},
},
watch: {
scripts: {
files: ['**/*.js'],
tasks: ['babel', 'build', 'uglify', 'mochaTest'],
options: {
spawn: true,
},
},
},
mochaTest: {
test: {
options: {
reporter: 'spec',
clearRequireCache: true,
},
src: ['tests/*.js'],
},
},
babel: {
options: {
sourceMap: true,
plugins: [
'transform-class-properties',
],
},
dist: {
files: [
{
expand: true,
cwd: 'lib',
src: ['**/*.js'],
dest: 'build/lib',
},
],
},
},
};
function benchmark() {
const bench = require('./tests/bench');
const done = this.async();
bench((err, output) => {
if (err) {
done(err);
return;
}
output.forEach(x => console.log(x));
done();
});
}
const babelConfig = {
plugins: [
'check-es2015-constants',
'transform-es2015-arrow-functions',
'transform-es2015-block-scoped-functions',
'transform-es2015-block-scoping',
'transform-es2015-function-name',
'transform-es2015-shorthand-properties',
],
};
function wrap([shimFile, runtimeFile], next) {
const cutout = /\/\* build:SERVER-ONLY:open \*\/[\s\S]*?\/\* build:SERVER-ONLY:close \*\//g;
const shimSource = shimFile.toString().replace(cutout, '');
const runtimeSource = runtimeFile.toString().replace(cutout, '');
const wrapped = `(function (factory) {
if (typeof define === 'function' && define.amd) {
define('benchpress', factory);
}
})(function () {
const runtime = (function () {
${runtimeSource}
return runtime;
})();
${shimSource}
return Benchpress;
});`;
const transpiled = babel.transform(wrapped, babelConfig).code;
next(null, transpiled);
}
function build() {
const done = this.async();
async.waterfall([
next => async.parallel([
cb => fs.readFile('lib/benchpress.js', cb),
cb => fs.readFile('lib/runtime.js', cb),
], next),
wrap,
(file, next) => mkdirp('build', err => next(err, file)),
(file, next) => fs.writeFile('build/benchpress.js', file, next),
], done);
}
module.exports = function Gruntfile(grunt) {
config.pkg = grunt.file.readJSON('package.json');
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-babel');
grunt.registerTask('benchmark', 'Run benchmarks', benchmark);
grunt.registerTask('build', 'Stripping and wrapping shim', build);
grunt.registerTask('default', ['babel', 'build', 'uglify', 'mochaTest']);
grunt.registerTask('bench', ['default', 'benchmark']);
};