1 | 'use strict';
|
2 |
|
3 | const buble = require('buble');
|
4 | const composeSourceMap = require('compose-source-map');
|
5 | const path = require('path');
|
6 |
|
7 | module.exports = function(grunt) {
|
8 | grunt.registerMultiTask('buble', function() {
|
9 |
|
10 | this.files.forEach(file => {
|
11 | const options = this.options();
|
12 | if (file.src.length > 1) grunt.log.fatal('only one src file accepted per dest');
|
13 | const src = file.src[0];
|
14 | const dest = file.dest || src;
|
15 | const result = buble.transform(grunt.file.read(src), {
|
16 | file: path.basename(dest), source: path.basename(src), transforms: {dangerousForOf: true}
|
17 | });
|
18 | grunt.file.write(dest, result.code);
|
19 | if (options.sourceMap) {
|
20 | let map = result.map;
|
21 | delete map.sourcesContent;
|
22 | if (options.sourceMapIn) {
|
23 | const mapSrc = grunt.util.kindOf(options.sourceMapIn) === 'function' ?
|
24 | options.sourceMapIn(src) : options.sourceMapIn;
|
25 | map = composeSourceMap(grunt.file.readJSON(mapSrc), map);
|
26 | }
|
27 | const mapDest = (grunt.util.kindOf(options.sourceMapName) === 'function' ?
|
28 | options.sourceMapName(src) : options.sourceMapName) || (dest + '.map');
|
29 | grunt.file.write(mapDest, JSON.stringify(map));
|
30 | }
|
31 | });
|
32 | });
|
33 | };
|