-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
92 lines (81 loc) · 2.83 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
'use strict';
const Funnel = require('broccoli-funnel');
const Replace = require('broccoli-string-replace');
const optionsField = 'ember-power-calendar-date-fns';
const defaultOptions = {
includeLocales: true,
};
function localeVarName(locale) {
return locale.replace(/-/g, '_');
}
function buildLocaleImport(includeLocales) {
// include only configured locales
if (Array.isArray(includeLocales)) {
// build locale variable name pairs: Array<[string, string]>
const localePairs = includeLocales.map(locale => [locale, localeVarName(locale)]);
// build imports list
const localeImports = localePairs.map(([locale, varName]) => `import ${varName} from "date-fns/locale/${locale}";`);
// create locale lookup table that is later used: locales = {'en-GB': enGB, 'de': de}
const localesAlias = localePairs.map(([locale, varName]) => {
return `"${locale}": ${varName},`;
});
return `
${localeImports.join('\n')}
const locales = { ${localesAlias.join('\n')} };`;
} else {
// include all locales
return 'import locales from "date-fns/locale";';
}
}
module.exports = {
name: require('./package').name,
/**
* This method tries to rename the import path of the addon from
* `ember-power-calendar-date-fns` to `ember-power-calendar-util`.
* This "agnostic" import path should make easy to swap this addon
* by another one that exposes the same API from the same import path.
*/
treeForAddon(tree) {
const options = (this.parent && this.parent.options) || (this.app && this.app.options) || {};
const addonOptions = options[optionsField] || {};
const {includeLocales} = {
...defaultOptions,
...addonOptions,
};
const localeModuledTree = Replace(tree, {
files: ['localized.js'],
pattern: {
match: /\/\/ DATE_FNS_LOCALE_START.*DATE_FNS_LOCALE_END/gs,
replacement: buildLocaleImport(includeLocales),
}
});
let namespacedTree = new Funnel(localeModuledTree, {
srcDir: '/',
destDir: `/ember-power-calendar-utils`,
annotation: `Addon#treeForVendor (${this.name})`,
getDestinationPath(relativePath) {
// if we include locales, handle localized.js as index file, else unlocalized
if (includeLocales) {
if (relativePath === 'localized.js') {
return 'index.js';
}
} else {
if (relativePath === 'unlocalized.js') {
return 'index.js';
}
}
return relativePath;
},
exclude: [function (file) {
// we exclude localized.js if we don't want to include locale
if (file === 'localized.js' && !includeLocales) {
return true;
}
return false;
}]
});
return this.preprocessJs(namespacedTree, '/', 'ember-power-calendar-utils', {
registry: this.registry
});
}
};