Skip to content

support multistage sourcemap #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
node_modules
npm-debug.log
.idea/
.tscache/

tmp
demo/dest/

test/fixtures/**/coffee_*.js
test/fixtures/**/coffee_*.map
test/fixtures/**/typescript_*.js
test/fixtures/**/typescript_*.map
test/fixtures/*-ts/*.js
test/fixtures/*-ts/*.js.map
demo/src/**/coffee_*.js
demo/src/**/coffee_*.map
demo/src/**/typescript_*.js
demo/src/**/typescript_*.map
6 changes: 3 additions & 3 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"unused": "vars",
"strict": true,
"trailing": true,
"maxparams": 2,
"maxparams": 5,
"maxdepth": 2,
"maxstatements": 8,
"maxstatements": 22,
"maxcomplexity": 5,
"maxlen": 100,
"maxlen": 140,
"asi": false,
"boss": false,
"debug": false,
Expand Down
69 changes: 67 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,69 @@ module.exports = function(grunt) {
}
},

coffee: {
test: {
options: {
sourceMap: true,
bare: true
},
files: [
{
expand: true,
cwd: 'test/',
src: ['**/*.coffee'],
dest: 'test/',
ext: '.js'
}
]
},
demo: {
options: {
sourceMap: true,
bare: true
},
files: [
{
expand: true,
cwd: 'demo/src',
src: ['**/*.coffee'],
dest: 'demo/src',
ext: '.js'
}
]
}
},

ts: {
options: {
comments: true,
target: 'es5',
noImplicitAny: false,
sourceMap: true
},
test: {
options: {
module: 'commonjs'
},
src: [
'test/**/*.ts',
'!test/fixtures/multi-ts/**/*.ts'
]
},
testConcat: {
src: [
'test/fixtures/multi-ts/main.ts'
],
out: 'test/fixtures/multi-ts/concat.js'
},
demo: {
options: {
module: 'commonjs'
},
src: ['demo/src/**/*.ts']
}
},

clean: {
test: ['tmp'],
demo: ['demo/dest/']
Expand Down Expand Up @@ -62,10 +125,12 @@ module.exports = function(grunt) {
grunt.loadTasks('tasks');

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-ts');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

grunt.registerTask('test', ['jshint', 'clean:test', 'espower:test', 'nodeunit:test']);
grunt.registerTask('demo', ['clean:demo', 'espower:demo', 'nodeunit:demo']);
grunt.registerTask('test', ['jshint', 'clean:test', 'coffee:test', 'ts:test', 'ts:testConcat', 'espower:test', 'nodeunit:test']);
grunt.registerTask('demo', ['clean:demo', 'coffee:demo', 'ts:demo', 'espower:demo', 'nodeunit:demo']);
grunt.registerTask('default', ['jshint', 'test']);
};
49 changes: 49 additions & 0 deletions demo/src/tests/coffee_power_assert_demo_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

empower = require 'empower'
formatter = require('power-assert-formatter')()
empowerOptions = {destructive: false, modifyMessageOnRethrow: false, saveContextOnRethrow: false}
require('source-map-support').install()

exports.coffee =
setUp: (done) ->
done()

demo1: (test) ->
test = empower test, formatter, empowerOptions

test.expect 1

three = 3
five = 5
test.ok three == five, 'MESSAGE'

test.done()

demo2: (test) ->
test = empower test, formatter, empowerOptions

test.expect 1

one = 1
two = 2
three = 3
seven = 7
ten = 10
test.ok (three * (seven * ten)) == three

test.done()

demo3: (test) ->
test = empower test, formatter, empowerOptions

test.expect 1

one = 1
two = 2
three = 3
seven = 7
ten = 10
test.equal three * (seven * ten), three

test.done()
47 changes: 47 additions & 0 deletions demo/src/tests/typescript_power_assert_demo_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
declare var require:any;
declare var describe:any;
declare var it:any;
declare var beforeEach:any;

'use strict';

var empower = require('empower'),
formatter = require('power-assert-formatter')(),
empowerOptions = {destructive: false, modifyMessageOnRethrow: false, saveContextOnRethrow: false};

export var typescript = {
setUp: function(done) {
done();
},
demo1: function(test) {
test = empower(test, formatter, empowerOptions);

test.expect(1);

var three = 3,
five = 5;
test.ok(three == five, 'MESSAGE');

test.done();
},
demo2: function(test) {
test = empower(test, formatter, empowerOptions);

test.expect(1);

var one = 1, two = 2, three = 3, seven = 7, ten = 10;
test.ok((three * (seven * ten)) === three);

test.done();
},
demo3: function(test) {
test = empower(test, formatter, empowerOptions);

test.expect(1);

var one = 1, two = 2, three = 3, seven = 7, ten = 10;
test.equal(three * (seven * ten), three);

test.done();
}
};
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,27 @@
"test": "grunt test"
},
"dependencies": {
"espower-source": "~0.8.0"
"convert-source-map": "~0.4.0",
"escodegen": "~1.3.3",
"espower": "~0.9.0",
"esprima": "~1.2.2",
"multi-stage-sourcemap": "~0.2.1",
"xtend": "~4.0.0"
},
"devDependencies": {
"empower": "~0.8.0",
"power-assert-formatter": "~0.8.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt": "~0.4.2",
"grunt-contrib-clean": "~0.6.0",
"grunt-contrib-coffee": "^0.11.1",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt": "~0.4.2"
"grunt-ts": "^1.11.7",
"power-assert-formatter": "~0.8.0",
"source-map-support": "^0.2.7",
"typescript": "^1.0.1"
},
"peerDependencies": {
"grunt": "~0.4.1"
"grunt": "~0.4.2"
},
"author": {
"name": "Takuto Wada",
Expand Down
105 changes: 99 additions & 6 deletions tasks/espower.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,105 @@
* Licensed under the MIT license.
* https://github.com/twada/grunt-espower/blob/master/LICENSE-MIT
*/
var fs = require('fs'),
espowerSource = require('espower-source');
'use strict';

module.exports = function(grunt) {
'use strict';
var path = require('path'),
espower = require('espower'),
esprima = require('esprima'),
escodegen = require('escodegen'),
extend = require('xtend'),
convert = require('convert-source-map'),
transfer = require('multi-stage-sourcemap').transfer;

function mergeSourceMap(incomingSourceMap, outgoingSourceMap) {
if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) {
outgoingSourceMap = JSON.parse(outgoingSourceMap);
}
if (!incomingSourceMap) {
return outgoingSourceMap;
}
return JSON.parse(transfer({fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap}));
}

function resolveOutgoingSourcesInfo(grunt, filepath, dest, inMap) {
// NOTE https://gist.github.com/twada/103d34a3237cecd463a6#comment-1288208
var incomingWorkingDir = path.relative(path.dirname(inMap.sourcemap.file), path.dirname(filepath));
var destTo = path.resolve(process.cwd(), dest);

var sourceRoot = path.relative(path.dirname(destTo), incomingWorkingDir);
var sources = inMap.sourcemap.sources
.map(function (src) {
// to full path
return path.resolve(incomingWorkingDir, inMap.sourcemap.sourceRoot || '', src);
})
.map(function (src) {
var rootPath = path.resolve(path.dirname(destTo), sourceRoot);
return path.relative(rootPath, src);
});
var sourcesContent = inMap.sourcemap.sourcesContent || inMap.sourcemap.sources
.map(function (src) {
var fullpath = path.resolve(incomingWorkingDir, inMap.sourcemap.sourceRoot || '', src);
return grunt.file.read(fullpath);
});

return {
sourceRoot: sourceRoot,
sources: sources,
sourcesContent: sourcesContent
};
}

function espowerSource(grunt, jsCode, filepath, dest, options) {
var jsAst, espowerOptions, modifiedAst, escodegenOutput, resolved;

jsAst = esprima.parse(jsCode, {
tolerant: true,
loc: true,
tokens: true,
raw: true,
source: filepath
});
var inMap = convert.fromSource(jsCode) || convert.fromMapFileSource(jsCode, path.dirname(filepath));
espowerOptions = extend(espower.defaultOptions(), options, {
destructive: true,
path: filepath,
sourceMap: inMap ? inMap.sourcemap : void 0
});
modifiedAst = espower(jsAst, espowerOptions);
escodegenOutput = escodegen.generate(modifiedAst, {
sourceMap: true,
sourceMapWithCode: true
});
var outMap = convert.fromJSON(escodegenOutput.map.toString());
if (inMap) {
var mergedRawMap = mergeSourceMap(inMap.toObject(), outMap.toObject());
resolved = resolveOutgoingSourcesInfo(grunt, filepath, dest, inMap);

outMap = convert.fromObject(mergedRawMap);
outMap.sourcemap.file = path.basename(dest);
outMap.sourcemap.sourceRoot = resolved.sourceRoot;
outMap.sourcemap.sources = resolved.sources;
outMap.sourcemap.sourcesContent = resolved.sourcesContent;
} else {
inMap = {
sourcemap: {
file: path.basename(dest),
sources: [path.basename(filepath)],
sourceRoot: '',
sourcesContent: [jsCode]
}
};
resolved = resolveOutgoingSourcesInfo(grunt, filepath, dest, inMap);

outMap.sourcemap.file = path.basename(dest);
outMap.sourcemap.sourceRoot = resolved.sourceRoot;
outMap.sourcemap.sources = resolved.sources;
outMap.sourcemap.sourcesContent = resolved.sourcesContent;
}
return escodegenOutput.code + '\n' + outMap.toComment() + '\n';
}

module.exports = function(grunt) {
grunt.registerMultiTask('espower', 'instrument power assert feature into code.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options();
Expand All @@ -28,9 +121,9 @@ module.exports = function(grunt) {
return true;
}).forEach(function(filepath) {
var modifiedSource,
jsCode = fs.readFileSync(filepath, 'utf-8');
jsCode = grunt.file.read(filepath);
grunt.verbose.writeln('espower src: ' + f.src);
modifiedSource = espowerSource(jsCode, filepath, options);
modifiedSource = espowerSource(grunt, jsCode, filepath, f.dest, options);
grunt.file.write(f.dest, modifiedSource);
grunt.verbose.writeln('espower dest: ' + f.dest);
});
Expand Down
Loading