Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Make requires lazy to improve startup times
Browse files Browse the repository at this point in the history
  • Loading branch information
Trent Willis committed Jun 22, 2016
1 parent d7e9565 commit a35f4c9
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
var path = require('path');
var fs = require('fs');
var resolve = require('resolve');
var MergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
var BabelTranspiler = require('broccoli-babel-transpiler');
var Concat = require('broccoli-concat');
var VersionChecker = require('ember-cli-version-checker');

var requireCache = {};

module.exports = {
name: 'Ember CLI QUnit',
Expand All @@ -33,6 +30,7 @@ module.exports = {
init: function() {
this._super.init && this._super.init.apply(this, arguments);

var VersionChecker = this.require('ember-cli-version-checker');
var checker = new VersionChecker(this);
var dep = checker.for('ember-cli', 'npm');

Expand All @@ -48,8 +46,27 @@ module.exports = {
return path.join(__dirname, 'blueprints');
},

require: function(name) {
if (!requireCache) {
requireCache = {};
}

var requiredModule = requireCache[name];

if (!requiredModule) {
requiredModule = requireCache[name] = require(name);
}

return requiredModule;
},

mergeTrees: function() {
var mergeTrees = this.require('broccoli-merge-trees');
return mergeTrees.apply(null, arguments);
},

treeForAddonTestSupport: function() {
return new MergeTrees(this._getDependencyTrees());
return this.mergeTrees(this._getDependencyTrees());
},

treeForVendor: function(tree) {
Expand All @@ -66,14 +83,16 @@ module.exports = {

if (this._shouldImportEmberQUnit) {
// support for Ember CLI < 2.2.0-beta.1
var depTree = new MergeTrees(this._getDependencyTrees());
var depTree = this.mergeTrees(this._getDependencyTrees());

var BabelTranspiler = this.require('broccoli-babel-transpiler');
var transpiled = new BabelTranspiler(depTree, {
loose: true,
moduleIds: true,
modules: 'amdStrict'
});

var Concat = this.require('broccoli-concat');
var concattedTree = new Concat(transpiled, {
inputFiles: ['**/*.js'],
outputFile: '/ember-qunit/ember-qunit.js',
Expand All @@ -83,7 +102,7 @@ module.exports = {
trees.push(concattedTree);
}

return new MergeTrees(trees, {
return this.mergeTrees(trees, {
annotation: 'ember-cli-qunit: treeForVendor'
});
},
Expand Down Expand Up @@ -154,6 +173,7 @@ module.exports = {
},

_notificationsTree: function() {
var Funnel = this.require('broccoli-funnel');
var notificationsPath = path.dirname(resolve.sync('qunit-notifications'));
return new Funnel(notificationsPath, {
include: [ 'index.js' ],
Expand Down

0 comments on commit a35f4c9

Please sign in to comment.