{"action":"closed","issue":{"url":"https://api.github.com/repos/strongloop/loopback-boot/issues/202","repository_url":"https://api.github.com/repos/strongloop/loopback-boot","labels_url":"https://api.github.com/repos/strongloop/loopback-boot/issues/202/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/loopback-boot/issues/202/comments","events_url":"https://api.github.com/repos/strongloop/loopback-boot/issues/202/events","html_url":"https://github.com/strongloop/loopback-boot/issues/202","id":170154753,"node_id":"MDU6SXNzdWUxNzAxNTQ3NTM=","number":202,"title":"The ability to use UMD modules for model code and boot scripts","user":{"login":"tonicblue","id":1927989,"node_id":"MDQ6VXNlcjE5Mjc5ODk=","avatar_url":"https://avatars1.githubusercontent.com/u/1927989?v=4","gravatar_id":"","url":"https://api.github.com/users/tonicblue","html_url":"https://github.com/tonicblue","followers_url":"https://api.github.com/users/tonicblue/followers","following_url":"https://api.github.com/users/tonicblue/following{/other_user}","gists_url":"https://api.github.com/users/tonicblue/gists{/gist_id}","starred_url":"https://api.github.com/users/tonicblue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tonicblue/subscriptions","organizations_url":"https://api.github.com/users/tonicblue/orgs","repos_url":"https://api.github.com/users/tonicblue/repos","events_url":"https://api.github.com/users/tonicblue/events{/privacy}","received_events_url":"https://api.github.com/users/tonicblue/received_events","type":"User","site_admin":false},"labels":[{"id":149103859,"node_id":"MDU6TGFiZWwxNDkxMDM4NTk=","url":"https://api.github.com/repos/strongloop/loopback-boot/labels/feature","name":"feature","color":"c2e0c6","default":false},{"id":552735926,"node_id":"MDU6TGFiZWw1NTI3MzU5MjY=","url":"https://api.github.com/repos/strongloop/loopback-boot/labels/needs-priority","name":"needs-priority","color":"eeeeee","default":false}],"state":"closed","locked":false,"assignee":{"login":"bajtos","id":1140553,"node_id":"MDQ6VXNlcjExNDA1NTM=","avatar_url":"https://avatars1.githubusercontent.com/u/1140553?v=4","gravatar_id":"","url":"https://api.github.com/users/bajtos","html_url":"https://github.com/bajtos","followers_url":"https://api.github.com/users/bajtos/followers","following_url":"https://api.github.com/users/bajtos/following{/other_user}","gists_url":"https://api.github.com/users/bajtos/gists{/gist_id}","starred_url":"https://api.github.com/users/bajtos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bajtos/subscriptions","organizations_url":"https://api.github.com/users/bajtos/orgs","repos_url":"https://api.github.com/users/bajtos/repos","events_url":"https://api.github.com/users/bajtos/events{/privacy}","received_events_url":"https://api.github.com/users/bajtos/received_events","type":"User","site_admin":false},"assignees":[{"login":"bajtos","id":1140553,"node_id":"MDQ6VXNlcjExNDA1NTM=","avatar_url":"https://avatars1.githubusercontent.com/u/1140553?v=4","gravatar_id":"","url":"https://api.github.com/users/bajtos","html_url":"https://github.com/bajtos","followers_url":"https://api.github.com/users/bajtos/followers","following_url":"https://api.github.com/users/bajtos/following{/other_user}","gists_url":"https://api.github.com/users/bajtos/gists{/gist_id}","starred_url":"https://api.github.com/users/bajtos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bajtos/subscriptions","organizations_url":"https://api.github.com/users/bajtos/orgs","repos_url":"https://api.github.com/users/bajtos/repos","events_url":"https://api.github.com/users/bajtos/events{/privacy}","received_events_url":"https://api.github.com/users/bajtos/received_events","type":"User","site_admin":false}],"milestone":null,"comments":1,"created_at":"2016-08-09T12:35:15Z","updated_at":"2018-10-11T14:27:46Z","closed_at":"2018-10-11T14:27:45Z","author_association":"NONE","body":"**Feature Request**\n\nI am using Loopback in a project where I'm using a lot of Typescript. I attempted to use the following Typescript boot script\n\n```\nexport default function (app) {\n    var User: any = app.models.User;\n    var Role: any = app.models.Role;\n    var RoleMapping: any = app.models.RoleMapping;\n\n    User.create([\n        { username: 'admin', email: 'admin@email.com', password: 'supersecret' }\n    ], (err, users) => {\n        if (err) {\n            console.error('User already exists');\n            return;\n        }\n\n        console.log('Created users:', users);\n\n        //create the admin role\n        Role.create({\n            name: 'administrators'\n        }, (err, role) => {\n            if (err) throw err;\n\n            console.log('Created role:', role);\n\n            //make our default user an Admin\n            role.principals.create({\n                principalType: RoleMapping.USER,\n                principalId: users[0].id\n            }, function (err, principal) {\n                if (err) throw err;\n\n                console.log('Created principal:', principal);\n            });\n        });\n    });\n};\n```\n\nBut 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. \n\n```\nvar exports = require(filepath);\nif (typeof exports === 'function') {\n    debug('Exported function detected %s', filepath);\n    functions.push({\n        path: filepath,\n        func: exports,\n    });\n}\n```\n\nAs 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.\n\n```\nvar exports = require(filepath);\nif (typeof exports === 'function') {\n    debug('Exported function detected %s', filepath);\n    functions.push({\n        path: filepath,\n        func: exports,\n    });\n// If the file contains a default UMD module, load it.\n} else if (exports.hasOwnProperty('default') && typeof exports.default === 'function') {\n    debug('Exported function detected %s', filepath);\n    functions.push({\n        path: filepath,\n        func: exports.default,\n    });\n}\n```\n\nThis 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:\n\n```\nvar code = require(data.sourceFile);\nif (typeof code === 'function') {\n    debug('Customizing model %s', name);\n    code(model);\n// If code has a UMD module, use it \n} else if (code.hasOwnProperty('default') && typeof code.default === 'function') {\n    debug('Customizing model %s', name);\n    code.default(model);\n} else {\n    debug('Skipping model file %s - `module.exports` is not a function',\n    data.sourceFile);\n}\n```\n\nWould this be an acceptable Feature Request? And if so, could I issue a Pull Request?\n"}}
{"actor":{"display_login":"bajtos"}}