-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate-raw.js
53 lines (45 loc) · 2 KB
/
generate-raw.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
import generate from "babel-generator";
import { assign } from "lodash";
import { pluggable } from "pluggable";
/**
* Given a compiled bundle object, return an array of one or more bundles with
* new `raw` property. This raw property should be generated from the bundle's
* AST or equivalent intermediate representation.
*
* This is a one-to-many transformation because it is quite possible for multiple
* output files to result from a single bundle object. The canonical example (and
* default behavior of this function, when sourcemaps are enabled) is for one
* bundle to result in a `.js` file and a `.map` file.
*
* @param {Object} bundle Bundle object without `raw` property.
*
* @return {Array} Array of bundle objects. At minimum, these bundle
* objects should have a `raw` property - a string
* representation of the file to be written to disk -
* and a `dest` property - the relative filepath
* of the file to be written to disk.
*/
export default pluggable(function generateRawBundles (bundle) {
if (bundle.type !== "javascript") {
throw new Error("Cannot generate JS source for non-JavaScript bundle.");
}
const bundleSources = bundle.modules.reduce((hash, module) => {
hash[module.sourcePath] = module.rawSource;
return hash;
}, {});
const ast = bundle.ast.type === "Program" || bundle.ast.type === "File" ?
bundle.ast :
{ type: "Program", body: [].concat(bundle.ast) };
const { code, map } = generate(ast, {
sourceMaps: !!this.opts.sourceMaps,
sourceMapTarget: this.opts.sourceMaps && bundle.dest,
comments: !!this.opts.includeComments,
compact: !this.opts.pretty,
quotes: "double"
}, bundleSources);
const outputBundle = assign({}, bundle, { raw: code });
const mapDest = bundle.dest + ".map";
return this.opts.sourceMaps ?
[ outputBundle, { raw: JSON.stringify(map), dest: mapDest } ] :
[ outputBundle ];
});