Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using require to load initializers, pass the modules instead #305

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions addon/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
declare global {
var requirejs: {
_eak_seen: Object;
};
}

import Engine from '@ember/engine';
import require from 'require';

function resolveInitializer(moduleName: string) {
var module = require(moduleName, null, null, true);
function resolveInitializer(moduleName: string, compatModules: any) {
let module = compatModules[moduleName];
if (!module) {
throw new Error(moduleName + ' must export an initializer.');
}
var initializer = module['default'];
let initializer = module['default'];
if (!initializer) {
throw new Error(moduleName + ' must have a default export');
}
Expand All @@ -22,15 +15,15 @@ function resolveInitializer(moduleName: string) {
return initializer;
}

function registerInitializers(app: typeof Engine, moduleNames: string[]) {
for (var i = 0; i < moduleNames.length; i++) {
app.initializer(resolveInitializer(moduleNames[i]));
function registerInitializers(app: typeof Engine, moduleNames: string[], compatModules: any) {
for (let i = 0; i < moduleNames.length; i++) {
app.initializer(resolveInitializer(moduleNames[i], compatModules));
}
}

function registerInstanceInitializers(app: typeof Engine, moduleNames: string[]) {
for (var i = 0; i < moduleNames.length; i++) {
app.instanceInitializer(resolveInitializer(moduleNames[i]));
function registerInstanceInitializers(app: typeof Engine, moduleNames: string[], compatModules: any) {
for (let i = 0; i < moduleNames.length; i++) {
app.instanceInitializer(resolveInitializer(moduleNames[i], compatModules));
}
}

Expand All @@ -41,16 +34,16 @@ function _endsWith(str: string, suffix: string): boolean {
/**
* Configure your application as it boots
*/
export default function loadInitializers(app: typeof Engine, prefix: string): void {
var initializerPrefix = prefix + '/initializers/';
var instanceInitializerPrefix = prefix + '/instance-initializers/';
var initializers = [];
var instanceInitializers = [];
export default function loadInitializers(app: typeof Engine, prefix: string, compatModules: any): void {
const initializerPrefix = prefix + '/initializers/';
const instanceInitializerPrefix = prefix + '/instance-initializers/';
let initializers = [];
let instanceInitializers = [];
// this is 2 pass because generally the first pass is the problem
// and is reduced, and resolveInitializer has potential to deopt
var moduleNames = Object.keys(requirejs._eak_seen);
for (var i = 0; i < moduleNames.length; i++) {
var moduleName = moduleNames[i];
const moduleNames = Object.keys(compatModules);
for (let i = 0; i < moduleNames.length; i++) {
const moduleName = moduleNames[i];
if (moduleName.lastIndexOf(initializerPrefix, 0) === 0) {
if (!_endsWith(moduleName, '-test')) {
initializers.push(moduleName);
Expand All @@ -61,6 +54,6 @@ export default function loadInitializers(app: typeof Engine, prefix: string): vo
}
}
}
registerInitializers(app, initializers);
registerInstanceInitializers(app, instanceInitializers);
registerInitializers(app, initializers, compatModules);
registerInstanceInitializers(app, instanceInitializers, compatModules);
}