Skip to content

The ability to use UMD modules for model code and boot scripts #202

@tonicblue

Description

@tonicblue

Feature Request

I am using Loopback in a project where I'm using a lot of Typescript. I attempted to use the following Typescript boot script

export default function (app) {
    var User: any = app.models.User;
    var Role: any = app.models.Role;
    var RoleMapping: any = app.models.RoleMapping;

    User.create([
        { username: 'admin', email: 'admin@email.com', password: 'supersecret' }
    ], (err, users) => {
        if (err) {
            console.error('User already exists');
            return;
        }

        console.log('Created users:', users);

        //create the admin role
        Role.create({
            name: 'administrators'
        }, (err, role) => {
            if (err) throw err;

            console.log('Created role:', role);

            //make our default user an Admin
            role.principals.create({
                principalType: RoleMapping.USER,
                principalId: users[0].id
            }, function (err, principal) {
                if (err) throw err;

                console.log('Created principal:', principal);
            });
        });
    });
};

But it didn't load. After some trying every different module format in my Typescript configuration I settled on UMD and stepped through loopback-boot/lib/executor.js and identified the following block of code (Line: 289) that was not allowing other module types.

var exports = require(filepath);
if (typeof exports === 'function') {
    debug('Exported function detected %s', filepath);
    functions.push({
        path: filepath,
        func: exports,
    });
}

As there isn't any way of getting isn't any way (as far as I know) of getting Typescript exported functions to follow the format module.exports = function(){ } I tried adding my own else if statement.

var exports = require(filepath);
if (typeof exports === 'function') {
    debug('Exported function detected %s', filepath);
    functions.push({
        path: filepath,
        func: exports,
    });
// If the file contains a default UMD module, load it.
} else if (exports.hasOwnProperty('default') && typeof exports.default === 'function') {
    debug('Exported function detected %s', filepath);
    functions.push({
        path: filepath,
        func: exports.default,
    });
}

This worked a treat. I then looked to see how I could do the same for model code and found successfully extended the following on line 245:

var code = require(data.sourceFile);
if (typeof code === 'function') {
    debug('Customizing model %s', name);
    code(model);
// If code has a UMD module, use it 
} else if (code.hasOwnProperty('default') && typeof code.default === 'function') {
    debug('Customizing model %s', name);
    code.default(model);
} else {
    debug('Skipping model file %s - `module.exports` is not a function',
    data.sourceFile);
}

Would this be an acceptable Feature Request? And if so, could I issue a Pull Request?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions