forked from jasonmit/ember-cli-moment-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (101 loc) · 3.53 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var path = require('path');
var Funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var defaults = require('lodash.defaults');
var rename = require('broccoli-stew').rename;
var momentPath = path.dirname(require.resolve('moment'));
module.exports = {
name: 'moment',
included: function(app) {
this._super.included.apply(this, arguments);
// see: https://github.com/ember-cli/ember-cli/issues/3718
if (typeof app.import !== 'function' && app.app) {
app = app.app;
}
this.app = app;
this.importDependencies(app);
},
importDependencies: function(app) {
if (arguments.length < 1) {
throw new Error('Application instance must be passed to import');
}
var vendor = this.treePaths.vendor;
var options = this.getConfig();
if (typeof options.includeLocales === 'boolean' && options.includeLocales) {
app.import(path.join(vendor, 'moment', 'min', 'moment-with-locales.min.js'));
}
else {
app.import(path.join(vendor, 'moment', 'min', 'moment.min.js'));
if (Array.isArray(options.includeLocales)) {
options.includeLocales.map(function(locale) {
app.import(path.join(vendor, 'moment', 'locales', locale + '.js'))
});
}
}
if (options.includeTimezone) {
app.import(path.join(vendor, 'moment-timezone', 'tz.js'));
}
},
getConfig: function() {
var projectConfig = ((this.project.config(process.env.EMBER_ENV) || {}).moment || {});
var config = defaults(projectConfig, {
includeTimezone: null,
includeLocales: []
});
if (Array.isArray(config.includeLocales)) {
config.includeLocales = config.includeLocales.filter(function(locale) {
return typeof locale === 'string';
}).map(function(locale) {
return locale.replace('.js', '').trim().toLowerCase();
});
}
return config;
},
treeForVendor: function(vendorTree) {
var trees = [];
var options = this.getConfig();
if (vendorTree) {
trees.push(vendorTree);
}
trees.push(new Funnel(momentPath, {
destDir: 'moment',
include: [new RegExp(/\.js$/)],
exclude: ['tests', 'ender', 'package'].map(function(key) {
return new RegExp(key + '\.js$');
})
}));
if (Array.isArray(options.includeLocales) && options.includeLocales.length) {
trees.push(new Funnel(momentPath, {
srcDir: 'locale',
destDir: path.join('moment', 'locales'),
include: options.includeLocales.map(function(locale) {
return new RegExp(locale + '.js$');
})
}));
}
if (options.includeTimezone) {
var timezonePath = [this.project.bowerDirectory, 'moment-timezone', 'builds'];
switch(options.includeTimezone) {
case 'all':
timezonePath.push('moment-timezone-with-data.min.js');
break;
case '2010-2020':
timezonePath.push('moment-timezone-with-data-2010-2020.min.js');
break;
case 'none':
timezonePath.push('moment-timezone.min.js');
break;
default:
throw new Error("Ember Moment: Please specify the moment-timezone dataset to include as either 'all', '2010-2020', or 'none'.");
break;
}
trees.push(rename(new Funnel(path.join(this.project.bowerDirectory, 'moment-timezone', 'builds'), {
files: [timezonePath[timezonePath.length - 1]]
}), function(filepath) {
return path.join('moment-timezone', 'tz.js');
}));
}
return mergeTrees(trees);
}
};