browser-pack-flat icon indicating copy to clipboard operation
browser-pack-flat copied to clipboard

support more of module.parent

Open goto-bus-stop opened this issue 8 years ago • 1 comments

properly supporting module.parent is probably not possible, since the module that required the current module is executed after the current one, so we don't have a reference to the "parent" module yet.


Maybe when module.parent is used in if checks and other boolean contexts, browser-pack-flat could replace it with true. Or replace it with an empty object in all contexts. this is done as of #17

goto-bus-stop avatar Aug 31 '17 07:08 goto-bus-stop

Maybe could detect more complex uses and do something similar to cyclical modules, with a lazy wrapper.

// xyz.js
module.exports = module.parent.exports
// app.js
exports.app = function () {
  console.log('app')
}
require('./xyz').app()

var _$withParent = function (factory) {
  var cache
  return function (parent) {
    if (!cache) {
      var exports = {}, module = { exports: exports }
      cache = factory(module, exports, parent)
    }
    return cache
  }
}
// xyz.js
var _$xyz_1 = _$withParent(function (module, exports, parent) {
  module.exports = parent.exports
})
// app.js
var _$app_2 = {}
_$app_2.app = function () {
  console.log('app')
}
_$xyz_1({ filename: 'app.js'/* maybe? */, exports: _$app_2 }).app()

goto-bus-stop avatar Sep 24 '17 20:09 goto-bus-stop