Skip to content

Conversation

@goto-bus-stop
Copy link
Owner

if the module has a parent, rewrites module.parent to {}. else rewrites it to null.

this supports the case where module.parent is only used to check for whether the module is being invoked directly, or required by some other module.

@goto-bus-stop
Copy link
Owner Author

goto-bus-stop commented Sep 22, 2017

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

this is a good start and has parity with browser-pack so i'll just do this for now.

@goto-bus-stop goto-bus-stop merged commit ce18b3e into master Sep 25, 2017
@goto-bus-stop goto-bus-stop deleted the feature/module-parent branch October 4, 2017 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants