Skip to content

Commit

Permalink
Merge pull request #614 from w3c/fix_source_maps
Browse files Browse the repository at this point in the history
Fix source maps
  • Loading branch information
Marcos Caceres committed Mar 8, 2016
2 parents b4bbe8f + 6dbb53a commit 2d9e7f8
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
@@ -1,3 +1,3 @@
{
"strict": "global"
"globalstrict": true
}
1 change: 1 addition & 0 deletions builds/profile-w3c-common.build.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.100.js.map

This file was deleted.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.101.js.map

This file was deleted.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.102.js.map

This file was deleted.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.103.js.map

This file was deleted.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.104.js.map

This file was deleted.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.105.js.map

This file was deleted.

1 change: 0 additions & 1 deletion builds/respec-w3c-common-3.2.99.js.map

This file was deleted.

1 change: 1 addition & 0 deletions builds/respec-w3c-common.build.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion builds/respec-w3c-common.js.map

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -13,6 +13,7 @@
"author": "Robin Berjon",
"devDependencies": {
"async": "^1.5.0",
"chai": "^3.5.0",
"colors": "^1.1.2",
"express": "^4.13.4",
"fs-extra": "^0.26.5",
Expand Down Expand Up @@ -49,11 +50,12 @@
"karma": "karma start --single-run",
"test:karma": "npm run karma",
"test:headless": "node ./tests/headless.js",
"test:build": "mocha ./tests/test-build.js",
"test": "npm run test:headless; npm run test:karma",
"jshint": "jshint karma.conf.js tests tools",
"jscs": "jscs --esnext tests tools",
"jscs:fix": "jscs --esnext --fix tests",
"pretest": "npm run jshint && npm run jscs",
"test:travis": "npm run pretest; karma start --single-run --reporters progress karma.conf.js; npm run test:headless"
"test:travis": "npm run pretest; npm run test:build; karma start --single-run --reporters progress karma.conf.js; npm run test:headless"
}
}
7 changes: 4 additions & 3 deletions tests/.jshintrc
Expand Up @@ -17,7 +17,6 @@
"plusplus": false,
"quotmark": "double",
"unused": "strict",
"strict": true,
"maxparams": false,
"maxdepth": 3,
"maxlen": false,
Expand All @@ -30,7 +29,6 @@
"evil": false,
"expr": false,
"funcscope": false,
"globalstrict": true,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
Expand All @@ -54,7 +52,7 @@
"jquery": true,
"mocha": true,
"mootools": false,
"node": false,
"node": true,
"nonstandard": false,
"phantom": false,
"prototypejs": false,
Expand All @@ -66,5 +64,8 @@
"wsh": false,
"yui": false,
"predef": [ "makeStandardOps", "makeDefaultBody", "makeRSDoc", "flushIframes", "isPhantom", "makeBasicConfig", "require", "pickRandomsFromList" ],
"globals": {
"expect": true
},
"esnext": true
}
3 changes: 0 additions & 3 deletions tests/headless.js
Expand Up @@ -4,7 +4,6 @@
"use strict";
const fs = require("fs");
const async = require("marcosc-async");
const builder = require("../tools/build-w3c-common");
const colors = require("colors");
const exec = require("child_process").exec;
const express = require("express");
Expand Down Expand Up @@ -89,8 +88,6 @@ async.task(function*() {
const dir = require("path").join(__dirname, "..");
app.use(express.static(dir));
app.listen(port);
debug(" ⏲ Building ReSpec...");
yield builder.buildW3C("latest");
debug(" ⏲ Running ReSpec2html tests...");
yield runRespec2html(server);
})
Expand Down
71 changes: 71 additions & 0 deletions tests/test-build.js
@@ -0,0 +1,71 @@
#!/usr/local/bin/mocha

"use strict";
const async = require("marcosc-async");
const colors = require("colors");
const fsp = require("fs-promise");
const path = require("path");
const expect = require("chai").expect;
const builder = require("../tools/build-w3c-common");

colors.setTheme({
data: "grey",
debug: "cyan",
error: "red",
help: "cyan",
info: "green",
input: "grey",
prompt: "grey",
verbose: "cyan",
warn: "yellow",
});

function checkIfFileExists(filePath) {
return async.task(function*() {
const stats = yield fsp.lstat(filePath);
return stats.isFile();
});
}

describe("build-w3c-common.js (tool)", function() {
// Generating respec + maps takes time.
this.timeout(60000);

// These files get deleted in after().
let customPath = "";
let customMapPath = "";

it("should have built default respec", async(function* () {
const latest = path.join(__dirname, "../builds/respec-w3c-common.js");
const latestMap = path.join(__dirname, "../builds/respec-w3c-common.build.js.map");
yield builder.buildW3C();
expect(yield checkIfFileExists(latest)).to.equal(true);
expect(yield checkIfFileExists(latestMap)).to.equal(true);
}));

it("should have built a custom version respec", async(function* () {
const randomName = "test-" + Math.round(Math.random() * 10000000);
customPath = path.join(__dirname, `../builds/respec-w3c-common-${randomName}.js`);
customMapPath = path.join(__dirname, `../builds/respec-w3c-common-${randomName}.build.js.map`);
yield builder.buildW3C(randomName);
expect(yield checkIfFileExists(customPath)).to.equal(true);
expect(yield checkIfFileExists(customMapPath)).to.equal(true);
}));

describe("respec-w3c-common.build.js", function(){
it("should include the link to the sourcemap", async(function*() {
var source = yield fsp.readFile(customPath, "utf-8");
var mapFilename = path.basename(customMapPath);
expect(source.includes(mapFilename)).to.equal(true);
}));
});

after(async(function*(){
yield Promise.all([fsp.remove(customPath), fsp.remove(customMapPath)]);
const msg = ` Deleted test files:
${colors.input(path.basename(customPath))}
${colors.input(path.basename(customMapPath))}`;
console.log(colors.info(msg));
}));
});

5 changes: 2 additions & 3 deletions tools/.jshintrc
Expand Up @@ -17,7 +17,6 @@
"plusplus": false,
"quotmark": "double",
"unused": "strict",
"strict": true,
"maxparams": false,
"maxdepth": 3,
"maxlen": false,
Expand All @@ -31,7 +30,6 @@
"evil": false,
"expr": false,
"funcscope": false,
"globalstrict": true,
"iterator": false,
"lastsemic": false,
"laxbreak": false,
Expand Down Expand Up @@ -65,5 +63,6 @@
"typed": false,
"worker": false,
"wsh": false,
"yui": false
"yui": false,
"strict": "global"
}
73 changes: 58 additions & 15 deletions tools/builder.js
Expand Up @@ -6,34 +6,77 @@ const fsp = require("fs-promise");
const pth = require("path");
const r = require("requirejs");

var Builder = {
getRespecVersion: async(function*() {
const path = pth.join(__dirname, "../package.json");
const content = yield fsp.readFile(path, "utf-8");
return JSON.parse(content).version;
}),
/**
* 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,
};
}

appendBoilerplate(outPath, version) {
return async(function*(optimizedJs, sourceMap) {
const respecJs = `"use strict";
/**
* 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 promiseToWriteJs = fsp.writeFile(outPath, respecJs, "utf-8");
const promiseToWriteSourceMap = fsp.writeFile(`${outPath}.map`, sourceMap, "utf-8");
yield Promise.all([promiseToWriteJs, promiseToWriteSourceMap]);
}, Builder);
},
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 = this.appendBoilerplate(options.out, version);
const outputWritter = appendBoilerplate(options.out, version);
const config = {
generateSourceMaps: true,
baseUrl: pth.join(__dirname, "../js/"),
Expand Down
46 changes: 0 additions & 46 deletions tools/test-build.js

This file was deleted.

0 comments on commit 2d9e7f8

Please sign in to comment.