1 | 'use strict';
|
2 |
|
3 | var PLUGIN_NAME = 'gulp-rollup';
|
4 |
|
5 | var util = require('util');
|
6 | var gutil = require('gulp-util');
|
7 | var PluginError = gutil.PluginError;
|
8 | var File = gutil.File;
|
9 | var Transform = require('readable-stream').Transform;
|
10 | var hypothetical = require('rollup-plugin-hypothetical');
|
11 | var path = require('path');
|
12 |
|
13 | function GulpRollup(options) {
|
14 | var self = this;
|
15 |
|
16 | Transform.call(self, { objectMode: true });
|
17 |
|
18 | var options0 = options || {};
|
19 | options = {};
|
20 | for (var key in options0) {
|
21 | if (key !== 'rollup' && key !== 'allowRealFiles' && key !== 'impliedExtensions') {
|
22 | options[key] = options0[key];
|
23 | }
|
24 | }
|
25 |
|
26 | var rollup = options0.rollup || require('rollup');
|
27 | var allowRealFiles = options0.allowRealFiles;
|
28 | var impliedExtensions = options0.impliedExtensions;
|
29 |
|
30 | var wonderland = {}, vinylFiles = {};
|
31 | var haveSourcemaps;
|
32 |
|
33 | var entryFiles = Promise.resolve(options.entry).then(function(entryFiles) {
|
34 | if (typeof entryFiles === 'string') {
|
35 | return [entryFiles];
|
36 | } else if (Array.isArray(entryFiles)) {
|
37 | if (entryFiles.some(function(entryFile) {
|
38 | return typeof entryFile !== 'string';
|
39 | })) {
|
40 | throw new Error('options.entry must include only strings!');
|
41 | }
|
42 | return entryFiles;
|
43 | } else {
|
44 | throw new Error('options.entry must be a string or array of strings!');
|
45 | }
|
46 | });
|
47 |
|
48 | self._transform = function(file, enc, cb) {
|
49 | if (!file.isBuffer()) {
|
50 | self.emit('error', new PluginError(PLUGIN_NAME, 'Input files must be buffered!'));
|
51 | return cb();
|
52 | }
|
53 |
|
54 | if (haveSourcemaps === undefined) {
|
55 | haveSourcemaps = file.sourceMap !== undefined;
|
56 | } else if (haveSourcemaps !== (file.sourceMap !== undefined)) {
|
57 | self.emit('error', new PluginError(PLUGIN_NAME, 'Mixing of sourcemapped and non-sourcemapped files!'));
|
58 | return cb();
|
59 | }
|
60 |
|
61 | if (haveSourcemaps) {
|
62 | wonderland[file.path] = {
|
63 | code: file.contents.toString(),
|
64 | map: file.sourceMap
|
65 | };
|
66 | } else {
|
67 | wonderland[file.path] = file.contents.toString();
|
68 | }
|
69 | vinylFiles[file.path] = file;
|
70 |
|
71 | cb();
|
72 | };
|
73 |
|
74 | self._flush = function(cb) {
|
75 | if (!options.plugins) {
|
76 | options.plugins = [];
|
77 | } else if (!Array.isArray(options.plugins)) {
|
78 | options.plugins = [options.plugins];
|
79 | }
|
80 | options.plugins = options.plugins.concat(hypothetical({
|
81 | files: wonderland,
|
82 | allowRealFiles: allowRealFiles,
|
83 | impliedExtensions: impliedExtensions
|
84 | }));
|
85 |
|
86 | var vinylSystem = hypothetical({ files: vinylFiles, allowRealFiles: true, impliedExtensions: impliedExtensions });
|
87 |
|
88 | entryFiles.then(function(entryFiles) {
|
89 | return Promise.all(entryFiles.map(function(entryFile) {
|
90 | options.entry = entryFile;
|
91 |
|
92 | options.sourceMap = haveSourcemaps;
|
93 |
|
94 | return rollup.rollup(options).then(function(bundle) {
|
95 | var result = bundle.generate(options);
|
96 |
|
97 |
|
98 |
|
99 | var file = vinylSystem.load(vinylSystem.resolveId(entryFile));
|
100 | if (file === undefined) {
|
101 | file = new File({
|
102 | path: entryFile,
|
103 | contents: new Buffer(result.code)
|
104 | });
|
105 | } else {
|
106 | file.contents = new Buffer(result.code);
|
107 | }
|
108 |
|
109 | var map = result.map;
|
110 | if (map) {
|
111 |
|
112 |
|
113 | map.file = unixStylePath(file.relative);
|
114 | map.sources = map.sources.map(function(fileName) {
|
115 | return unixStylePath(path.relative(file.base, fileName));
|
116 | });
|
117 | file.sourceMap = map;
|
118 | }
|
119 |
|
120 | self.push(file);
|
121 | });
|
122 | }));
|
123 | }).then(function() {
|
124 | cb();
|
125 | }).catch(function(err) {
|
126 | setImmediate(function() {
|
127 | self.emit('error', new PluginError(PLUGIN_NAME, err));
|
128 | cb();
|
129 | });
|
130 | });
|
131 | };
|
132 | }
|
133 | util.inherits(GulpRollup, Transform);
|
134 |
|
135 | function unixStylePath(filePath) {
|
136 | return filePath.split(path.sep).join('/');
|
137 | }
|
138 |
|
139 | module.exports = function(options) {
|
140 | return new GulpRollup(options);
|
141 | };
|