forked from rooseveltframework/roosevelt-closure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroosevelt-closure.js
49 lines (41 loc) · 1.37 KB
/
roosevelt-closure.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
const closureCompiler = require('google-closure-compiler-js').compile
const fs = require('fs')
const path = require('path')
module.exports = {
parse: function (app, fileName) {
let params = app.get('params').js.compiler.params || {}
let externs
const logger = app.get('logger')
// make a copy of the params so the originals aren't modified
params = JSON.parse(JSON.stringify(params))
if (params.externs) {
// process externs
if (typeof params.externs === 'string') {
params.externs = [{ src: fs.readFileSync(path.join(app.get('appDir'), params.externs), 'utf-8') }]
} else {
externs = []
params.externs.forEach((extern) => {
externs.push(
{
src: fs.readFileSync(path.join(app.get('appDir'), extern), 'utf-8')
}
)
})
params.externs = externs
}
}
params.jsCode = [{ src: fs.readFileSync(path.join(app.get('jsPath'), fileName), 'utf-8') }]
const compilerOut = closureCompiler(params)
const newJs = compilerOut.compiledCode
const errors = compilerOut.errors
const warnings = compilerOut.warnings
if (app.get('params').js.compiler.showWarnings === true && warnings[0]) {
logger.warn('⚠️ JS Compiler Warnings:')
logger.warn(warnings)
}
if (errors[0]) {
throw errors
}
return newJs
}
}