UNPKG

unbundle

Version:

`require()` and `import`/`export` in the browser, without the bundling

96 lines (73 loc) 3.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var _streamToPromise = require('stream-to-promise'); var _streamToPromise2 = _interopRequireDefault(_streamToPromise); var _path = require('path'); var _moduleDeps = require('module-deps'); var _moduleDeps2 = _interopRequireDefault(_moduleDeps); var _findOutputPath = require('./findOutputPath'); var _processFile = require('./processFile'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } exports.default = function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(entry, destination) { var log, processors, dependencies; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: log = (0, _debug2.default)('unbundle'); entry = (0, _path.resolve)(process.cwd(), entry); destination = (0, _path.resolve)(process.cwd(), destination); log('Entry: ' + entry); log('Destination: ' + destination); processors = []; dependencies = (0, _moduleDeps2.default)({ ignoreMissing: true, transform: [['babelify', {/* babelrc options */}]] }); dependencies.on('file', function (file) { processors.push(_asyncToGenerator(regeneratorRuntime.mark(function _callee() { var output; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: output = (0, _findOutputPath.findOutputPath)(file, entry, destination); log((0, _path.relative)(process.cwd(), output)); _context.next = 4; return (0, _processFile.processFile)(file, output, destination); case 4: case 'end': return _context.stop(); } } }, _callee, undefined); }))()); }); dependencies.on('missing', function (id, parent) { return log('Missing: ' + id); }); dependencies.end({ file: entry }); _context2.next = 12; return (0, _streamToPromise2.default)(dependencies); case 12: _context2.next = 14; return Promise.all(processors); case 14: return _context2.abrupt('return', _context2.sent); case 15: case 'end': return _context2.stop(); } } }, _callee2, undefined); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }();