-
Notifications
You must be signed in to change notification settings - Fork 17
/
Gruntfile.js
121 lines (101 loc) · 2.76 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
'use strict';
/* eslint
import/no-extraneous-dependencies: [error, { devDependencies: true }],
no-console: off,
global-require: off,
*/
const fs = require('fs').promises;
const mkdirp = require('mkdirp');
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'],
},
},
shell: {
compiler: 'wasm-pack build --target nodejs --out-dir ../build/compiler compiler',
docs: 'npm run docs',
},
};
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();
});
}
function wrap([shimFile, runtimeFile]) {
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;
});`;
return wrapped;
}
function client() {
const done = this.async();
(async () => {
const files = await Promise.all([
fs.readFile('lib/benchpress.js'),
fs.readFile('lib/runtime.js'),
mkdirp('build'),
]);
const wrapped = wrap(files);
await fs.writeFile('build/benchpress.js', wrapped);
})().then(() => done(), (err) => {
console.error(err);
done(false);
});
}
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-shell');
grunt.registerTask('benchmark', 'Run benchmarks', benchmark);
grunt.registerTask('client', 'Stripping and wrapping shim', client);
grunt.registerTask('build', ['client', 'uglify', 'shell:compiler', 'shell:docs']);
grunt.registerTask('default', ['build', 'mochaTest']);
grunt.registerTask('bench', ['default', 'benchmark']);
};