niViz

niViz Git Source Tree

Root/assets.js

1/*
2* niViz -- snow profiles visualization
3* Copyright (C) 2015 WSL/SLF - Fluelastrasse 11 - 7260 Davos Dorf - Switzerland.
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU Affero General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU Affero General Public License for more details.
14*
15* You should have received a copy of the GNU Affero General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19'use strict';
20
21var assert = require('assert');
22var format = require('util').format;
23var assign = require('object-assign');
24
25var Mincer = require('mincer');
26var config = require('./assets.json');
27
28module.exports = function (env, options) {
29 env = env || new Mincer.Environment();
30 options = assign({}, config, options);
31
32 if (options.autoprefixer) env.enable('autoprefixer');
33 if (options.sourcemaps) env.enable('source_maps');
34
35 Mincer.Autoprefixer.configure(options.browserlist);
36 Mincer.SassEngine.configure(options.sass);
37
38 options.paths.forEach(env.appendPath, env);
39
40 var assets = [];
41
42 function find(path, params) {
43 if (params.type === 'font') {
44 path = path.replace(/[#?].*$/, '');
45 }
46
47 var asset = env.findAsset(path);
48 if (!asset) throw new Error('asset not found: ' + path);
49
50 return asset;
51 }
52
53 function resolve(path, params) {
54 if (!path) return null;
55 var asset = find(path, params || {});
56
57 if (assets.indexOf(asset.logicalPath) === -1) {
58 assets.push(asset.logicalPath);
59 }
60
61 return [
62 options.serve,
63 options.fingerprinting ? asset.digestPath : asset.logicalPath
64 ].join('/');
65 }
66
67 function template(path) {
68 var html = find(path, {});
69
70 assert((/html/).test(html.contentType), 'html file expected');
71
72 return html.toString();
73 }
74
75 function js(name) {
76 return format('<script src="%s"></script>', resolve(name + '.js'));
77 }
78
79 function css(name) {
80 return format('<link rel="stylesheet" href="%s"/>', resolve(name + '.css'));
81 }
82
83 env.ContextClass.defineAssetPath(resolve);
84
85 // Connect-Assets registers its helpers globally!
86 // Since this is just for development, let's do that, too.
87 global.asset_path = resolve;
88 global.template = template;
89 global.css = css;
90 global.js = js;
91
92 return { env: env, req: assets, options: options };
93};

Archive Download this file

Branches