-
Notifications
You must be signed in to change notification settings - Fork 7
/
solidify.js
35 lines (31 loc) · 1.28 KB
/
solidify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var path = require('path');
var SolidusServer = require('./lib/server');
var transformTools = require('browserify-transform-tools');
var solidus_server;
// Browserify transform that inlines requires to Solidus JS views.
// For example, this code:
// var view = require('solidus/views/some/view'); // The path can end with .js too
// Becomes something like:
// var view = {template:require("../../views/some/view.hbs"),template_options:{helpers:require("../../helpers.js")}};
module.exports = transformTools.makeRequireTransform(
'solidus/solidify',
{excludeExtensions: ['.' + SolidusServer.extensions.template]},
function(args, opts, callback) {
var view_name = args[0].match(/^solidus\/views\/(.*?)(\.js)?$/);
if (!view_name) return callback();
view_name = view_name[1];
var waitForSolidus = function() {
if (!solidus_server) {
solidus_server = new SolidusServer({start_server: false});
solidus_server.on('ready', function() {solidus_server.ready = true});
}
if (solidus_server.ready) {
var view = solidus_server.views[solidus_server.pathFromPartialName(view_name)];
callback(null, view ? view.toObjectString(opts.file) : null);
} else {
setImmediate(waitForSolidus);
}
};
waitForSolidus();
}
);