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
·130 lines (123 loc) · 4.32 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
119
120
121
122
123
124
125
126
127
128
129
130
#!/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,
};
}
/**
* Replaces the version number in the boilerplate.
*
* @param {String} optimizedJs The optimized script.
* @param {String} version The version string.
*/
function replaceVersionString(optimizedJs, version){
return optimizedJs.replace("<<ReSpec-Version-Number>>", version);
}
/**
* 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 writeOutput(outPath, version) {
return async(function*(optimizedJs, sourceMap) {
let newSource = replaceMapFilename(optimizedJs, outPath);
newSource.source = replaceVersionString(newSource.source, version);
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 {Object} output The output options
* @param {String} output.out The path where to write the optimized file.
* @param {String} output.version The version number to use.
*/
build(options) {
return async.task(function*() {
// optimisation settings
const version = options.version || (yield this.getRespecVersion());
const outputWritter = writeOutput(options.out, version);
const config = {
insertRequire: ["profile-w3c-common"],
wrap: {
startFile: [
"tools/inserts/boilerplate.frag",
"tools/inserts/wrapperFunction.frag",
],
endFile: [
"tools/inserts/wrapperFunctionEnd.frag",
],
},
generateSourceMaps: true,
mainConfigFile: "js/profile-w3c-common.js",
baseUrl: pth.join(__dirname, "../js/"),
optimize: options.optimize || "uglify2",
paths: {
"handlebars": "../node_modules/handlebars/dist/handlebars",
"jquery": "../node_modules/jquery/dist/jquery",
"Promise": "../node_modules/promise-polyfill/Promise",
"requireLib": "../node_modules/requirejs/require",
"webidl2": "../node_modules/webidl2/lib/webidl2",
},
name: "profile-w3c-common",
deps: [
"core/jquery-enhanced",
"jquery",
"Promise",
"requireLib",
],
inlineText: true,
preserveLicenseComments: false,
useStrict: true,
};
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;