forked from jasonmit/ember-cli-moment-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
296 lines (254 loc) · 7.9 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* globals require, module, process */
/* eslint no-fallthrough: 0, no-duplicate-case: 0 */
'use strict';
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
const mergeTrees = require('broccoli-merge-trees');
const defaults = require('lodash.defaults');
const funnel = require('broccoli-funnel');
const fs = require('fs');
const stew = require('broccoli-stew');
const concat = require('broccoli-concat');
const chalk = require('chalk');
const path = require('path');
const rename = stew.rename;
const map = stew.map;
function getLocaleDefinitionModules(requestingTree) {
let options = this._options;
const singleModule = options.singleModule;
if (requestingTree === 'addon' && !singleModule) {
return;
}
if (requestingTree === 'vendor' && singleModule) {
return;
}
let localeTree;
if (
Array.isArray(options.includeLocales) &&
options.includeLocales.length
) {
localeTree = funnel(this.momentNode, {
srcDir: 'locale',
destDir: 'moment/locales',
include: options.includeLocales.map(
locale => new RegExp(locale + '.js$')
)
});
}
if (singleModule) {
// Turn the locales into a hash key/value pairs.
const keyValuePairs = map(localeTree, function(content, relativePath) {
const locale = path.basename(relativePath, '.js');
return `'${locale}': function() { ${content} },`;
});
// This function becomes defineLocale(locale);
const header = `
export default function(locale) {
if (typeof FastBoot === 'undefined') {
const locales = {
`;
const footer = `
};
if (locales[locale]) {
locales[locale]();
}
}
}
`;
const concatenatedDefinitionIIFEs = concat(keyValuePairs, {
header,
footer,
inputFiles: ['**/*'],
outputFile: 'ember-cli-moment-shim/define-locale.js'
});
let babelAddon = this.addons.find(addon => addon.name === 'ember-cli-babel');
return babelAddon.transpileTree(concatenatedDefinitionIIFEs);
} else {
return map(
localeTree,
content => `if (typeof FastBoot === 'undefined') { ${content} }`
);
}
}
module.exports = {
name: 'moment',
included() {
this._super.included.apply(this, arguments);
this._options = this.getOptions();
this.momentNode = new UnwatchedDir(this._options.momentPath);
this.importDependencies();
},
updateFastBootManifest(manifest) {
let target = 'fastboot-moment.js';
if (this._options.includeTimezone) {
target = 'fastboot-moment-timezone.js';
}
manifest.vendorFiles.push('moment/' + target);
return manifest;
},
importDependencies() {
let options = this._options;
if (options.includeTimezone) {
this.import(
{
development: 'vendor/moment-timezone/tz.js',
production: 'vendor/moment-timezone/tz.min.js'
},
{ prepend: true }
);
}
if (typeof options.includeLocales === 'boolean' && options.includeLocales) {
this.import(
{
development: 'vendor/moment/min/moment-with-locales.js',
production: 'vendor/moment/min/moment-with-locales.min.js'
},
{ prepend: true }
);
} else {
if (Array.isArray(options.includeLocales) && !options.singleModule) {
options.includeLocales.forEach(locale => {
this.import('vendor/moment/locales/' + locale + '.js', {
prepend: true
});
});
}
this.import(
{
development: 'vendor/moment/moment.js',
production: 'vendor/moment/min/moment.min.js'
},
{ prepend: true }
);
}
},
getOptions() {
let projectConfig =
(this.project.config(process.env.EMBER_ENV) || {}).moment || {};
let momentPath = path.dirname(require.resolve('moment'));
let config = defaults(projectConfig, {
singleModule: false,
momentPath: momentPath,
includeTimezone: null,
includeLocales: []
});
if (Array.isArray(config.includeLocales)) {
config.includeLocales = config.includeLocales
.filter(locale => typeof locale === 'string')
.map(locale =>
locale
.replace('.js', '')
.trim()
.toLowerCase()
)
.filter(locale => {
if (locale === 'en') {
// `en` is included by default. quietly ignore if user specifies it in the list
return false;
}
if (!fs.existsSync(momentPath + '/locale/' + locale + '.js')) {
this.ui.writeLine(
chalk.red(
'ember-cli-moment-shim: Specified locale `' +
locale +
'` but could not find in moment/locale.\nVisit https://github.com/moment/moment/tree/master/locale to view the full list of supported locales.'
)
);
return false;
}
return true;
});
}
return config;
},
treeForPublic() {
let hasFastBoot = this.project.addons.some(
addon => addon.name === 'ember-cli-fastboot'
);
let publicTree = this._super.treeForPublic.apply(this, arguments);
let options = this._options;
let trees = [];
if (publicTree && hasFastBoot) {
trees.push(publicTree);
}
if (options.localeOutputPath) {
trees.push(
funnel(this.momentNode, {
srcDir: 'locale',
destDir: options.localeOutputPath
})
);
}
return mergeTrees(trees);
},
treeForAddon() {
const superValue = this._super.apply(this, arguments);
const definitionModules = getLocaleDefinitionModules.call(this, 'addon');
return mergeTrees([superValue, definitionModules].filter(Boolean));
},
treeForVendor() {
let trees = [];
let options = this._options;
const superValue = this._super.apply(this, arguments);
trees.push(superValue);
trees.push(
funnel(this.momentNode, {
destDir: 'moment',
include: [new RegExp(/\.js$/)],
exclude: ['tests', 'ender', 'package'].map(
key => new RegExp(key + '.js$')
)
})
);
if (options.includeTimezone) {
let timezonePath;
let timezoneMinPath;
switch (options.includeTimezone) {
case 'all':
timezonePath = 'builds/moment-timezone-with-data.js';
timezoneMinPath = 'builds/moment-timezone-with-data.min.js';
break;
case '2010-2020':
this.ui.writeLine(
chalk.yellow(
'[ember-cli-moment-shim] "2010-2020" is deprecated, use "subset" within config/environment\nDiscussion: https://github.com/jasonmit/ember-cli-moment-shim/issues/121'
)
);
case 'subset':
case '2012-2022':
case '2010-2020':
timezonePath = 'builds/moment-timezone-with-data-*.js';
timezoneMinPath = 'builds/moment-timezone-with-data-*.min.js';
break;
case 'none':
timezonePath = 'moment-timezone.js';
timezoneMinPath = 'builds/moment-timezone.min.js';
break;
default:
throw new Error(
'ember-cli-moment-shim: Please specify the moment-timezone dataset to include as either "all", "subset", or "none".'
);
}
const timezoneNode = new UnwatchedDir(
path.dirname(require.resolve('moment-timezone'))
);
trees.push(
rename(
funnel(timezoneNode, { include: [timezonePath] }),
() => 'moment-timezone/tz.js'
)
);
trees.push(
rename(
funnel(timezoneNode, { include: [timezoneMinPath] }),
() => 'moment-timezone/tz.min.js'
)
);
}
const definitionModules = getLocaleDefinitionModules.call(this, 'vendor');
trees.push(definitionModules);
return map(
mergeTrees(trees.filter(Boolean)),
content => `if (typeof FastBoot === 'undefined') { ${content} }`
);
}
};