This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.js
executable file
·118 lines (112 loc) · 3.95 KB
/
builder.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
#!/usr/local/bin/node
"use strict";
const async = require("marcosc-async");
const fsp = require("fs-promise");
const pth = require("path");
const r = require("requirejs");
/**
* Finds the name of the map file generated by Requirejs, and replaces it
* with one that matches the filename of the ReSpec output file.
*
* @param {String} respecJs The source for ReSpec, as produced by Requirejs.
* @param {String} outPath The path for the ReSpec source output file.
* @return {Object} An object with a updated `source` and the new filename for
* the map file.
*/
function replaceMapFilename(respecJs, outPath){
const basename = pth.basename(outPath, ".js");
const currentMapFilename = respecJs.match(/\/\/# sourceMappingURL=(.+)/)[1];
const newMapFilename = basename + ".build.js.map";
const source = respecJs.replace(currentMapFilename, newMapFilename);
const mapPath = pth.resolve(outPath, `../${newMapFilename}`);
return {
source,
mapPath,
};
}
/**
* Async function that appends the boilerplate to the generated script
* and writes out the result. It also creates the source map file.
*
* @private
* @param {String} outPath Where to write the output to.
* @param {String} version The version of the script.
* @return {Promise} Resolves when done writing the files.
*/
function appendBoilerplate(outPath, version) {
return async(function*(optimizedJs, sourceMap) {
const respecJs = `"use strict";
/* ReSpec ${version}
Created by Robin Berjon, http://berjon.com/ (@robinberjon)
Documentation: http://w3.org/respec/.
See original source for licenses: https://github.com/w3c/respec */
window.respecVersion = "${version}";
${optimizedJs}
require(['profile-w3c-common']);`;
const newSource = replaceMapFilename(respecJs, outPath);
const promiseToWriteJs = fsp.writeFile(outPath, newSource.source, "utf-8");
const promiseToWriteMap = fsp.writeFile(newSource.mapPath, sourceMap, "utf-8");
yield Promise.all([promiseToWriteJs, promiseToWriteMap]);
}, Builder);
}
var Builder = {
/**
* Async function that gets the current version of ReSpec from package.json
*
* @returns {Promise<String>} The version string.
*/
getRespecVersion: async(function*() {
const path = pth.join(__dirname, "../package.json");
const content = yield fsp.readFile(path, "utf-8");
return JSON.parse(content).version;
}),
/**
* Async function runs Requirejs' optimizer to generate the output.
*
* using a custom configuration.
* @param {[type]} options [description]
* @return {[type]} [description]
*/
build(options) {
return async.task(function*() {
// optimisation settings
const version = options.version || (yield this.getRespecVersion());
const outputWritter = appendBoilerplate(options.out, version);
const config = {
generateSourceMaps: true,
baseUrl: pth.join(__dirname, "../js/"),
optimize: options.optimize || "uglify2",
paths: {
"require": "../node_modules/requirejs/require",
"jquery": "../node_modules/jquery/dist/jquery",
"handlebars": "../node_modules/handlebars/dist/handlebars",
"webidl2": "../node_modules/webidl2/lib/webidl2",
},
shim: {
"shortcut": {
exports: "shortcut"
}
},
name: "profile-w3c-common",
include: [
"../node_modules/requirejs/require",
"../node_modules/jquery/dist/jquery",
"../node_modules/handlebars/dist/handlebars",
"../node_modules/webidl2/lib/webidl2",
],
inlineText: true,
preserveLicenseComments: false,
};
const promiseToWrite = new Promise((resolve, reject)=>{
config.out = (optimizedJs, sourceMap) => {
outputWritter(optimizedJs, sourceMap)
.then(resolve)
.catch(reject);
};
});
r.optimize(config);
yield promiseToWrite;
}, this);
},
};
exports.Builder = Builder;