1+ require ( './require-ts-babel' ) ( ) ; // Enable loading TS/TSX/JSX/ES2015 modules
12var url = require ( 'url' ) ;
2- var babelCore = require ( 'babel-core' ) ;
3- var babelConfig = {
4- presets : [ "es2015" , "react" ]
5- } ;
3+ var domainTasks = require ( './domain-tasks.ts' ) ;
64
7- var origJsLoader = require . extensions [ '.js' ] ;
8- require . extensions [ '.js' ] = loadViaBabel ;
9- require . extensions [ '.jsx' ] = loadViaBabel ;
10-
11- function loadViaBabel ( module , filename ) {
12- // Assume that all the app's own code is ES2015+ (optionally with JSX), but that none of the node_modules are.
13- // The distinction is important because ES2015+ forces strict mode, and it may break ES3/5 if you try to run it in strict
14- // mode when the developer didn't expect that (e.g., current versions of underscore.js can't be loaded in strict mode).
15- var useBabel = filename . indexOf ( 'node_modules' ) < 0 ;
16- if ( useBabel ) {
17- var transformedFile = babelCore . transformFileSync ( filename , babelConfig ) ;
18- return module . _compile ( transformedFile . code , filename ) ;
19- } else {
20- return origJsLoader . apply ( this , arguments ) ;
5+ function render ( bootModulePath , requestUrl , callback ) {
6+ var bootFunc = require ( bootModulePath ) ;
7+ if ( typeof bootFunc !== 'function' ) {
8+ bootFunc = bootFunc . default ;
219 }
22- }
23-
24- var domainTasks = require ( './domain-tasks.js' ) ;
25- var bootServer = require ( '../boot-server.jsx' ) . default ;
26-
27- function render ( requestUrl , callback ) {
10+ if ( typeof bootFunc !== 'function' ) {
11+ throw new Error ( 'The module at ' + bootModulePath + ' must export a default function, otherwise we don\'t know how to invoke it.' )
12+ }
13+
2814 var params = {
2915 location : url . parse ( requestUrl ) ,
3016 url : requestUrl ,
@@ -36,7 +22,7 @@ function render(requestUrl, callback) {
3622 // Since route matching is asynchronous, add the rendering itself to the list of tasks we're awaiting
3723 domainTasks . addTask ( new Promise ( function ( resolve , reject ) {
3824 // Now actually perform the first render that will match a route and commence associated tasks
39- bootServer ( params , function ( error , result ) {
25+ bootFunc ( params , function ( error , result ) {
4026 if ( error ) {
4127 reject ( error ) ;
4228 } else {
@@ -51,15 +37,15 @@ function render(requestUrl, callback) {
5137 // By now, all the data should be loaded, so we can render for real based on the state now
5238 // TODO: Add an optimisation where, if domain-tasks had no outstanding tasks at the end of
5339 // the previous render, we don't re-render (we can use the previous html and state).
54- bootServer ( params , callback ) ;
40+ bootFunc ( params , callback ) ;
5541 } ) . catch ( function ( error ) {
5642 process . nextTick ( ( ) => { // Because otherwise you can't throw from inside a catch
5743 callback ( error , null ) ;
5844 } ) ;
5945 } ) ;
6046}
6147
62- render ( '/' , ( err , html ) => {
48+ render ( '../boot-server.tsx' , ' /', ( err , html ) => {
6349 if ( err ) {
6450 throw err ;
6551 }
0 commit comments