Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support more of module.parent #16

Open
goto-bus-stop opened this issue Aug 31, 2017 · 1 comment
Open

support more of module.parent #16

goto-bus-stop opened this issue Aug 31, 2017 · 1 comment

Comments

@goto-bus-stop
Copy link
Owner

goto-bus-stop commented Aug 31, 2017

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
Copy link
Owner Author

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 goto-bus-stop changed the title support module.parent support more of module.parent Sep 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant