Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #112 from arthirm/memory-leak
Browse files Browse the repository at this point in the history
Fix for Memory leak
  • Loading branch information
rwjblue authored Oct 4, 2018
2 parents 53b149e + 1d22ac8 commit a6227ca
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 46 deletions.
28 changes: 26 additions & 2 deletions lib/ast-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ const HTMLBarsInlinePrecompilePlugin = require('babel-plugin-htmlbars-inline-pre
const pluginCache = {};

module.exports = {
purgeModule(templateCompilerPath) {
// ensure we get a fresh templateCompilerModuleInstance per ember-addon
// instance NOTE: this is a quick hack, and will only work as long as
// templateCompilerPath is a single file bundle
//
// (╯°□°)╯︵ ɹǝqɯǝ
//
// we will also fix this in ember for future releases

// Module will be cached in .parent.children as well. So deleting from require.cache alone is not sufficient.
let mod = require.cache[templateCompilerPath];
if (mod && mod.parent) {
let index = mod.parent.children.indexOf(mod);
if (index >= 0) {
mod.parent.children.splice(index, 1);
} else {
throw new TypeError(`ember-cli-htmlbars-inline-precompile attempted to purge '${templateCompilerPath}' but something went wrong.`);
}
}

delete require.cache[templateCompilerPath];
},

setup(pluginInfo, options) {
// borrowed from ember-cli-htmlbars http://git.io/vJDrW
let projectConfig = options.projectConfig || {};
Expand All @@ -23,7 +46,7 @@ module.exports = {
// (╯°□°)╯︵ ɹǝqɯǝ
//
// we will also fix this in ember for future releases
delete require.cache[templateCompilerPath];
this.purgeModule(templateCompilerPath);

// do a full clone of the EmberENV (it is guaranteed to be structured
// cloneable) to prevent ember-template-compiler.js from mutating
Expand Down Expand Up @@ -53,7 +76,8 @@ module.exports = {
pluginCache[cacheKey] = precompileInlineHTMLBarsPlugin;
}

delete require.cache[templateCompilerPath];
this.purgeModule(templateCompilerPath);

delete global.Ember;
delete global.EmberENV;

Expand Down
5 changes: 5 additions & 0 deletions node-tests/fixtures/compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function() {
return 'I AM MODULE OF COMPILER';
};
38 changes: 38 additions & 0 deletions node-tests/purge-module-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const purgeModule = require('../lib/ast-plugins').purgeModule;
const expect = require('chai').expect;

describe('purgeModule', function() {
const FIXTURE_COMPILER_PATH = require.resolve('./fixtures/compiler');

it('it works correctly', function() {
expect(purgeModule('asdfasdfasdfaf-unknown-file')).to.eql(undefined);

expect(require.cache[FIXTURE_COMPILER_PATH]).to.eql(undefined);

require(FIXTURE_COMPILER_PATH);

const mod = require.cache[FIXTURE_COMPILER_PATH];

expect(mod.parent).to.eql(module);
expect(mod.parent.children).to.include(mod);

purgeModule(FIXTURE_COMPILER_PATH);

expect(require.cache[FIXTURE_COMPILER_PATH]).to.eql(undefined);
expect(mod.parent.children).to.not.include(mod);

require(FIXTURE_COMPILER_PATH);

const freshModule = require.cache[FIXTURE_COMPILER_PATH];

expect(freshModule.parent).to.eql(module);
expect(freshModule.parent.children).to.include(freshModule);

purgeModule(FIXTURE_COMPILER_PATH);

expect(require.cache[FIXTURE_COMPILER_PATH]).to.eql(undefined);
expect(freshModule.parent.children).to.not.include(mod);
});
});
File renamed without changes.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"build": "ember build",
"lint:js": "eslint ./*.js addon addon-test-support app config lib server test-support tests",
"start": "ember server",
"test": "mocha && ember try:each",
"test:unit": "mocha"
"test": "mocha node-tests && ember try:each",
"test:unit": "mocha node-tests"
},
"repository": "https://github.com/ember-cli/ember-cli-htmlbars-inline-precompile",
"engines": {
Expand Down
42 changes: 0 additions & 42 deletions test/.jshintrc

This file was deleted.

0 comments on commit a6227ca

Please sign in to comment.