browser-pack-flat
browser-pack-flat copied to clipboard
support more of module.parent
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
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()