Skip to content

Commit c9fac75

Browse files
committed
Added support for source maps
Fixes #6
1 parent 1493e14 commit c9fac75

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ A small, simple, very easy wrapper around the [require.js optimizer](https://git
1818
</tr>
1919
<tr>
2020
<td>Node Version</td>
21-
<td>>= 4</td>
21+
<td> 4</td>
2222
</tr>
2323
</table>
2424

2525

26-
##Installation
26+
## Installation
2727

2828
Simply add `gulp-requirejs` as a dev-dependency in your package.json or run
2929

@@ -64,6 +64,33 @@ gulp-requirejs will emit errors when you don't pass an options object and if the
6464

6565
The require.js optimizer itself might also emit errors; unfortunately there's no way of catching them elegantly at the moment.
6666

67+
### Source maps
68+
69+
When source maps are enabled via the r.js `generateSourceMaps` option the stream returned by `rjs()` contains an additional file named the same as the `out` option with `.map` appended.
70+
71+
Use [gulp-sourcemaps](https://www.npmjs.com/package/gulp-sourcemaps) to use this file
72+
in your gulp configuration:
73+
74+
```javascript
75+
var gulp = require('gulp'),
76+
rjs = require('gulp-requirejs')
77+
sourcemaps = require('gulp-sourcemaps');
78+
79+
gulp.task('requirejsBuild', function() {
80+
return rjs({
81+
baseUrl: 'path/to/your/base/file.js',
82+
out: 'FILENAME_TO_BE_OUTPUTTED',
83+
generateSourceMaps: true,
84+
shim: {
85+
// standard require.js shim options
86+
},
87+
// ... more require.js options
88+
})
89+
.pipe(sourcemaps.init({loadMaps: true})) // initialize gulp-sourcemaps with the existing map
90+
.pipe(sourcemaps.write()) // write the source maps
91+
.pipe(gulp.dest('./deploy/')); // pipe it to the output DIR
92+
});
93+
```
6794

6895
## Options
6996

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = function(opts) {
2727
// create the stream and save the file name (opts.out will be replaced by a callback function later)
2828
var _s = es.pause(),
2929
_fName = opts.out,
30-
_success = function(text, buildResponse) {
30+
_success = function(text, buildResponse, sourceMap) {
3131
var newFile = new File({
3232
path: _fName,
3333
contents: new Buffer(text)
@@ -36,6 +36,12 @@ module.exports = function(opts) {
3636
// debugging purposes.
3737
newFile.buildResponse = buildResponse.replace('FUNCTION', _fName);
3838
_s.write(newFile);
39+
if (sourceMap) {
40+
_s.write(new File({
41+
path: _fName + '.map',
42+
contents: new Buffer(sourceMap)
43+
}));
44+
}
3945
_s.resume();
4046
_s.end();
4147
},
@@ -55,9 +61,11 @@ module.exports = function(opts) {
5561
// a small wrapper around the r.js optimizer
5662
function optimize(opts, successFn, errorFn) {
5763
var output = null;
58-
opts.out = function(text) {
64+
var sourceMapOutput = null;
65+
opts.out = function(text, sourceMap) {
5966
output = text;
67+
sourceMapOutput = sourceMap;
6068
}
6169
opts.optimize = opts.optimize || 'none';
62-
requirejs.optimize(opts, function(buildResponse) { successFn(output, buildResponse); }, errorFn);
70+
requirejs.optimize(opts, function(buildResponse) { successFn(output, buildResponse, sourceMapOutput); }, errorFn);
6371
}

test/main.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ describe('gulp-requirejs', function() {
2727
stream.on('end', done);
2828
});
2929

30+
it('should emit source map file when configured', function(done) {
31+
var filesInPipe = [];
32+
33+
var stream = grjs({
34+
out: 'simple_init.js',
35+
36+
baseUrl: 'test/fixtures/',
37+
38+
findNestedDependencies: true,
39+
skipPragmas: true,
40+
generateSourceMaps: true,
41+
42+
name: 'simple_init',
43+
44+
include: ['simple_init'],
45+
46+
create: true
47+
});
48+
49+
stream.on('data', (chunk) => {
50+
filesInPipe.push(chunk.path);
51+
});
52+
53+
stream.on('end', function() {
54+
try {
55+
filesInPipe.length.should.equal(2);
56+
filesInPipe.should.containEql('simple_init.js');
57+
filesInPipe.should.containEql('simple_init.js.map');
58+
done();
59+
} catch(e) {
60+
done(e);
61+
}
62+
});
63+
});
64+
3065
describe('simple AMD file', function() {
3166

3267
it('should concat the files in the correct order', function(done) {

0 commit comments

Comments
 (0)