|
| 1 | +const path = require('path'); |
| 2 | +const vinyl = require('vinyl'); |
| 3 | +const vinylSa = require('vinyl-sourcemaps-apply'); |
| 4 | +const through = require('through2'); |
| 5 | +const rollup = require('rollup'); |
| 6 | +const hash = require('object-hash'); |
| 7 | +const root = require('njfs').root; |
| 8 | + |
| 9 | +const PLUGIN_NAME = 'gulp-rollup-2'; |
| 10 | + |
| 11 | +let cache = {}; |
| 12 | + |
| 13 | +const modules = ['es', 'amd', 'cjs', 'iife', 'umd', 'system']; |
| 14 | + |
| 15 | +const isInMod = (format) => modules.indexOf(format) > -1; |
| 16 | + |
| 17 | +const isArray = arg => Object.prototype.toString.call(arg) === '[object Array]'; |
| 18 | + |
| 19 | +const unique = (obj) => Array.prototype.filter.call(obj, (v, i) => obj.indexOf(v) === i); |
| 20 | + |
| 21 | +const isEqual = (a, b) => { |
| 22 | + |
| 23 | + const oka = Object.keys(a); |
| 24 | + const okb = Object.keys(b); |
| 25 | + const ola = oka.length; |
| 26 | + const olb = okb.length; |
| 27 | + |
| 28 | + if (ola !== olb) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + let i = 0; |
| 33 | + for (; i < ola; i += 1) { |
| 34 | + |
| 35 | + const ka = oka[i]; |
| 36 | + |
| 37 | + if (b[ka] === undefined) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + const va = a[ka]; |
| 42 | + const vb = b[ka]; |
| 43 | + |
| 44 | + if (va == null || typeof va === 'string' || typeof va === 'number') { |
| 45 | + if (va !== vb) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + } else if (typeof va === 'function') { |
| 49 | + if (va.name !== vb.name) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + } else if (va.constructor !== vb.constructor) { |
| 53 | + return false; |
| 54 | + } else { |
| 55 | + return isEqual(va, vb); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return true; |
| 60 | +}; |
| 61 | + |
| 62 | +const sanitize = (opts, out) => { |
| 63 | + |
| 64 | + if (!opts) { |
| 65 | + throw new Error(`${PLUGIN_NAME}: Missing rollup config options!`); |
| 66 | + } |
| 67 | + |
| 68 | + if (typeof opts === 'string') { |
| 69 | + opts = [{ |
| 70 | + output: { |
| 71 | + format: opts |
| 72 | + } |
| 73 | + }]; |
| 74 | + } else if (!isArray(opts)) { |
| 75 | + opts = [opts]; |
| 76 | + } |
| 77 | + |
| 78 | + opts.filter(e => { |
| 79 | + if (out && !e.input) { |
| 80 | + throw new Error(`${PLUGIN_NAME}: Input option required!`); |
| 81 | + } else if (!e.output) { |
| 82 | + throw new Error(`${PLUGIN_NAME}: Output option required!`); |
| 83 | + } else if (isArray(e.output)) { |
| 84 | + if (e.output.filter((i) => !i.file || !i.format || !isInMod(i.format)).length) { |
| 85 | + throw new Error(`${PLUGIN_NAME}: Missing options file, format or unknown format type!`); |
| 86 | + } |
| 87 | + } else if (!e.output.file || !e.output.format || !isInMod(e.output.format)) { |
| 88 | + throw new Error(`${PLUGIN_NAME}: Missing options file, format or unknown format type!`); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + const options = []; |
| 93 | + |
| 94 | + opts.forEach((opt, i) => { |
| 95 | + |
| 96 | + const io = Object.assign({}, opt); |
| 97 | + const oo = io.output; |
| 98 | + |
| 99 | + delete io.output; |
| 100 | + |
| 101 | + options[i] = {}; |
| 102 | + options[i]['io'] = io; |
| 103 | + options[i]['oo'] = isArray(oo) ? oo : [oo]; |
| 104 | + }); |
| 105 | + |
| 106 | + opts = options; |
| 107 | + |
| 108 | + opts.forEach((opt, i) => { |
| 109 | + const currI = opt.io; |
| 110 | + opts.forEach((e, j) => { |
| 111 | + if (i !== j && isEqual(currI, e.io)) { |
| 112 | + throw new Error(`${PLUGIN_NAME}: Repetitive input options!`); |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + const of = []; |
| 118 | + |
| 119 | + opts.forEach((opt) => { |
| 120 | + const oo = opt.oo; |
| 121 | + oo.forEach((o, i) => { |
| 122 | + const curr = o; |
| 123 | + oo.forEach((e, j) => { |
| 124 | + if (i !== j) { |
| 125 | + if (isEqual(curr, e) || curr.file === e.file) { |
| 126 | + throw new Error(`${PLUGIN_NAME}: Two or more output have same file option!`); |
| 127 | + } else { |
| 128 | + of .push(curr.file); |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + if ( of .length !== unique( of ).length) { |
| 136 | + throw new Error(`${PLUGIN_NAME}: Two or more output have same file option!`); |
| 137 | + } |
| 138 | + |
| 139 | + return opts; |
| 140 | +}; |
| 141 | + |
| 142 | +const inside = (opts) => { |
| 143 | + |
| 144 | + opts = sanitize(opts); |
| 145 | + |
| 146 | + return through({ |
| 147 | + objectMode: true |
| 148 | + }, |
| 149 | + async (file, encoding, callback) => { |
| 150 | + |
| 151 | + if (file.isStream()) { |
| 152 | + this.emit('Error', Error(`${PLUGIN_NAME}: Unsupported file type: Stream!`)); |
| 153 | + callback(); |
| 154 | + } |
| 155 | + |
| 156 | + const _map = !!file.sourceMap; |
| 157 | + const _dir = file.cwd; |
| 158 | + const _pat = file.path; |
| 159 | + const _inp = path.relative(_dir, _pat); |
| 160 | + |
| 161 | + const bundles = await Promise.all(opts.map(async opt => { |
| 162 | + |
| 163 | + const io = opt.io; |
| 164 | + const oo = opt.oo; |
| 165 | + |
| 166 | + const ch = hash(io); |
| 167 | + cache[ch] = {}; |
| 168 | + |
| 169 | + io.input = io.input ? path.relative(_dir, io.input) : _inp; |
| 170 | + io.cache = io.cache ? cache[ch] : false; |
| 171 | + |
| 172 | + const bundle = await rollup.rollup(io); |
| 173 | + cache[ch] = bundle.cache; |
| 174 | + |
| 175 | + return { |
| 176 | + bundle, |
| 177 | + oo, |
| 178 | + input: io.input |
| 179 | + }; |
| 180 | + })); |
| 181 | + |
| 182 | + let start = 2; |
| 183 | + |
| 184 | + await Promise.all(bundles.map(async bundle => { |
| 185 | + |
| 186 | + const oo = bundle.oo; |
| 187 | + const input = bundle.input; |
| 188 | + bundle = bundle.bundle; |
| 189 | + |
| 190 | + await Promise.all(oo.map(async o => { |
| 191 | + |
| 192 | + start--; |
| 193 | + |
| 194 | + const f = o.format; |
| 195 | + const s = start > 0; |
| 196 | + |
| 197 | + if ((f === 'umd' || f === 'iife') && !o.name) { |
| 198 | + o.name = input; |
| 199 | + } |
| 200 | + |
| 201 | + if ((f === 'umd' || f === 'amd') && !o.amd || !!o.amd && !o.amd.id) { |
| 202 | + o.amd = Object.assign({}, o.amd, { |
| 203 | + id: o.name |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + const build = await bundle.generate(o); |
| 208 | + const [output] = build.output; |
| 209 | + const { |
| 210 | + code |
| 211 | + } = output; |
| 212 | + const buffer = Buffer.from(code, encoding); |
| 213 | + |
| 214 | + const oBase = path.dirname(path.join(_dir, input)); |
| 215 | + const oName = path.basename(o.file); |
| 216 | + const oPath = path.resolve(oBase, oName); |
| 217 | + let oFile = s ? file : new vinyl(); |
| 218 | + let map = output.map; |
| 219 | + |
| 220 | + oFile.path = oPath; |
| 221 | + oFile.contents = buffer; |
| 222 | + oFile.base = oBase; |
| 223 | + |
| 224 | + if (map !== null) { |
| 225 | + vinylSa(oFile, map); |
| 226 | + } else if (_map && s) { |
| 227 | + map = file.sourceMap; |
| 228 | + map.file = input; |
| 229 | + map.sources = map.sources.map(src => path.relative(_dir, path.join(oBase, src))); |
| 230 | + vinylSa(oFile, map); |
| 231 | + } |
| 232 | + |
| 233 | + return s ? oFile : this.push(oFile); |
| 234 | + })); |
| 235 | + |
| 236 | + })); |
| 237 | + |
| 238 | + callback(null, file); |
| 239 | + }); |
| 240 | +}; |
| 241 | + |
| 242 | +const outside = async (opts) => { |
| 243 | + |
| 244 | + opts = sanitize(opts, true); |
| 245 | + |
| 246 | + const _dir = root(); |
| 247 | + const _obj = through.obj(); |
| 248 | + |
| 249 | + const bundles = await Promise.all(opts.map(async (opt) => { |
| 250 | + |
| 251 | + const io = opt.io; |
| 252 | + const oo = opt.oo; |
| 253 | + |
| 254 | + const ch = hash(io); |
| 255 | + cache[ch] = {}; |
| 256 | + |
| 257 | + io.input = path.relative(_dir, io.input); |
| 258 | + io.cache = io.cache ? cache[ch] : false; |
| 259 | + |
| 260 | + const bundle = await rollup.rollup(io); |
| 261 | + cache[ch] = bundle.cache; |
| 262 | + |
| 263 | + return { |
| 264 | + bundle, |
| 265 | + oo, |
| 266 | + input: io.input |
| 267 | + }; |
| 268 | + })); |
| 269 | + |
| 270 | + await Promise.all(bundles.map(async bundle => { |
| 271 | + |
| 272 | + const oo = bundle.oo; |
| 273 | + const input = bundle.input; |
| 274 | + bundle = bundle.bundle; |
| 275 | + |
| 276 | + await Promise.all(oo.map(async o => { |
| 277 | + |
| 278 | + const f = o.format; |
| 279 | + |
| 280 | + if ((f === 'umd' || f === 'iife') && !o.name) { |
| 281 | + o.name = input; |
| 282 | + } |
| 283 | + |
| 284 | + if ((f === 'umd' || f === 'amd') && !o.amd || !!o.amd && !o.amd.id) { |
| 285 | + o.amd = Object.assign({}, o.amd, { |
| 286 | + id: o.name |
| 287 | + }); |
| 288 | + } |
| 289 | + |
| 290 | + const build = await bundle.generate(o); |
| 291 | + const [output] = build.output; |
| 292 | + const { |
| 293 | + code, |
| 294 | + map |
| 295 | + } = output; |
| 296 | + const buffer = Buffer.from(code, "utf8"); |
| 297 | + |
| 298 | + const oBase = path.dirname(path.join(_dir, input)); |
| 299 | + const oName = path.basename(o.file); |
| 300 | + const oPath = path.resolve(oBase, oName); |
| 301 | + const oFile = new vinyl({ |
| 302 | + base: oBase, |
| 303 | + path: oPath, |
| 304 | + contents: buffer |
| 305 | + }); |
| 306 | + |
| 307 | + if (map !== null) { |
| 308 | + vinylSa(oFile, map); |
| 309 | + } |
| 310 | + |
| 311 | + _obj.push(oFile); |
| 312 | + })); |
| 313 | + })); |
| 314 | + |
| 315 | + _obj.push(null); |
| 316 | + |
| 317 | + return _obj; |
| 318 | +}; |
| 319 | + |
| 320 | +module.exports = { |
| 321 | + rollup: inside, |
| 322 | + src: outside |
| 323 | +}; |
0 commit comments