Skip to content

Commit

Permalink
Cleanup module.children after evaluating
Browse files Browse the repository at this point in the history
Without this, required-from-string module never gets garbage collected,
even when exports value is no longer used.
  • Loading branch information
Sergey Tatarintsev committed Sep 15, 2017
1 parent 3cf2b66 commit ca2b81f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Expand Up @@ -21,10 +21,14 @@ module.exports = function requireFromString(code, filename, opts) {

var paths = Module._nodeModulePaths(path.dirname(filename));

var m = new Module(filename, module.parent);
var parent = module.parent;
var m = new Module(filename, parent);
m.filename = filename;
m.paths = [].concat(opts.prependPaths).concat(paths).concat(opts.appendPaths);
m._compile(code, filename);

return m.exports;
var exports = m.exports;
parent.children.splice(parent.children.indexOf(m), 1);

return exports;
};
8 changes: 8 additions & 0 deletions test/index.js
Expand Up @@ -58,3 +58,11 @@ it('should have meaningful error message', function () {
assert.ok(/\(<anonymous>:1:69\)/.test(err.stack), 'should contain (<anonymous>:1:69) in stack');
}
});

it('should cleanup parent.children', function() {
var file = path.join(__dirname, '/fixture/submodule.js');
var code = fs.readFileSync(file, 'utf8');
var result = requireFromString(code, file);

assert.ok(module.children.indexOf(result) === -1);
});

0 comments on commit ca2b81f

Please sign in to comment.