This repository has been archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ejs.js
104 lines (96 loc) · 3.96 KB
/
ejs.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* RequireJS loader plugin for CanJS 2.1 EJS templates.
*/
define([
'text'
], function(text) {
'use strict';
// File extension.
var extension = '.ejs';
// Stores raw source for a given moduleName.
var sourceMap = {};
// Store renderer function for a given moduleName.
var buildMap = {};
// Template of the compiled module, used for the optimizer build.
var buildTemplateSource = (
"define('{pluginName}!{moduleName}', " +
"['can/util/library', 'can/view/ejs'], " +
"function(can) { " +
"var template = '{content}'; " +
"return can.ejs('{canViewId}', template); " +
"});" +
"\n");
// Convert a module path to a can.view ID, so that can.view(id) works.
var toId = function(modulePath) {
var pathParts = modulePath.toString().split(/\/|\./g);
var filteredParts = [];
for (var i = 0; i < pathParts.length; i += 1) {
if (pathParts[i]) {
filteredParts.push(pathParts[i]);
}
}
return filteredParts.join('_');
};
return {
normalize: function(moduleName, normalize) {
// Add the file extension to the module name.
if (moduleName.slice(-extension.length) !== extension) {
moduleName += extension;
}
return normalize(moduleName);
},
load: function(moduleName, parentRequire, onload, config) {
// This path is the absolute path.
var fullPath = parentRequire.toUrl(moduleName);
// The can.view ID.
var canViewId = toId(moduleName);
// Have we already loaded this? Resolve with result of first load.
if (buildMap[moduleName]) {
onload(buildMap[moduleName]);
}
else {
// Use the text plugin to load the raw source.
text.load(fullPath, parentRequire, function(rawSource) {
// Are we executing inside the optimizer build?
if (config.isBuild) {
// Remember raw source for the write() call.
sourceMap[moduleName] = rawSource;
// Don't need to resolve with anything.
onload();
}
else {
// ... else we're in the normal load process.
// We want to resolve with the template renderer,
// so we require the template compiler first.
parentRequire(['can/util/library', 'can/view/ejs'], function(can) {
// Compile into a renderer.
var renderer = can.ejs(canViewId, rawSource);
// Cache the renderer.
buildMap[moduleName] = renderer;
// Resolve with the renderer.
onload(renderer);
});
}
}, config);
}
},
write: function(pluginName, moduleName, write, config) {
// The can.view ID.
var canViewId = toId(moduleName);
// Get the raw source we kept during load().
var rawSource = sourceMap[moduleName];
var escapedRawSource = rawSource && text.jsEscape(rawSource);
if (escapedRawSource) {
// Write out the compiled module, which behaves similar to the
// normal load process, but with an inline copy of rawSource
// instead of going through the text plugin.
write.asModule(pluginName + '!' + moduleName,
buildTemplateSource
.replace('{canViewId}', canViewId)
.replace('{pluginName}', pluginName)
.replace('{moduleName}', moduleName)
.replace('{content}', escapedRawSource));
}
}
};
});