forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemberHandlebars.js
More file actions
81 lines (71 loc) · 2.91 KB
/
Copy pathemberHandlebars.js
File metadata and controls
81 lines (71 loc) · 2.91 KB
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
/*
* Copyright (C) 2015 - present Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// This is basically one big port of what we do in ruby-land in the
// handlebars-tasks gem. We need to run handlebars source through basic
// compilation to extract i18nliner scopes, and then we wrap the resulting
// template in an AMD module, giving it dependencies on handlebars, it's scoped
// i18n object if it needs one, and any brandableCss variant stuff it needs.
const Handlebars = require('handlebars')
const EmberHandlebars = require('ember-template-compiler').EmberHandlebars
const ScopedHbsExtractor = require(__dirname + '/../gems/canvas_i18nliner/js/scoped_hbs_extractor')
const PreProcessor = require(__dirname + '/../gems/canvas_i18nliner/node_modules/i18nliner-handlebars/dist/lib/pre_processor').default
function compileHandlebars (data) {
const {path, source} = data
try {
let translationCount = 0
const ast = Handlebars.parse(source)
const extractor = new ScopedHbsExtractor(ast, {path})
const scope = extractor.scope
PreProcessor.scope = scope
PreProcessor.process(ast)
extractor.forEach(() => translationCount++)
const precompiler = data.ember ? EmberHandlebars : Handlebars
const template = precompiler.precompile(ast).toString()
const payload = {template, scope, translationCount}
return payload
} catch (e) {
e = e.message || e
console.log(e)
throw {error: e}
}
}
function resourceName (path) {
return path
.replace(/^.+?\/templates\//, '')
.replace(/\.hbs$/, '')
}
function emitTemplate (path, name, result, dependencies) {
return `define(${JSON.stringify(dependencies)}, function(Ember){
Ember.TEMPLATES['${name}'] = Ember.Handlebars.template(${result.template});
});`
}
module.exports = function (source) {
this.cacheable()
const name = resourceName(this.resourcePath)
const dependencies = ['ember', 'coffeescripts/ember/shared/helpers/common.coffee']
const result = compileHandlebars({path: this.resourcePath, source: source, ember: true})
if (result.error) {
console.log('THERE WAS AN ERROR IN PRECOMPILATION', result)
throw result
}
if (result.translationCount > 0) {
dependencies.push(`i18n!${result.scope}`)
}
const compiledTemplate = emitTemplate(this.resourcePath, name, result, dependencies)
return compiledTemplate
}