-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (178 loc) · 6.28 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
/* eslint-env node */
'use strict';
const path = require('path');
const get = require('lodash.get');
const funnel = require('broccoli-funnel');
const existsSync = require('exists-sync');
const mergeTrees = require('broccoli-merge-trees');
const lowercaseTree = require('./lib/lowercase-tree');
const debug = require('debug')('@ember-intl/polyfill');
const strategies = require('./lib/strategies');
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
const isArray = Array.isArray;
const assign = Object.assign;
const VENDOR = strategies.VENDOR;
const SCRIPT_TAGS = strategies.SCRIPT_TAGS;
module.exports = {
name: '@ember-intl/polyfill',
intlPlugin: true,
addonConfig: null,
included() {
this._super.included.apply(this, arguments);
let host = (this.app = this._findHost());
this.addonConfig = assign({}, this.getConfig(host.env), this.addonConfig);
this._nodeModulePath = this.intlRelativeToProject(this.project.root);
this.importPolyfill(this.app);
},
/* NOTE: initializePlugin will get called before `included` when consumed as a plugin */
initializePlugin(parentOptions) {
this.addonConfig = assign({}, this.addonConfig, parentOptions);
},
getConfig(env) {
let configPath = path.join(this.project.configPath(), '..', 'ember-intl.js');
let config = {};
if (existsSync(configPath)) {
config = assign({}, require(configPath)(env));
if (isArray(config.locales)) {
config.locales = config.locales.map(this.normalizeLocale, this);
}
if (isArray(get(config, 'autoPolyfill.locales'))) {
config.autoPolyfill.locales = config.autoPolyfill.locales.map(this.normalizeLocale, this);
}
}
let optionalAssetPath = get(this, 'app.options.app.intl');
if (optionalAssetPath) {
config.assetPath = optionalAssetPath;
}
return assign({
autoPolyfill: false,
disablePolyfill: false,
assetPath: 'assets/intl',
locales: [],
}, config);
},
normalizeLocale(localeName) {
return localeName.toLowerCase().replace(/\_/g, '-');
},
contentFor(name, config) {
let addonConfig = this.addonConfig;
if (name === 'head' && !addonConfig.disablePolyfill) {
let autoPolyfill = addonConfig.autoPolyfill;
if (autoPolyfill === true || (typeof autoPolyfill === 'object' && get(autoPolyfill, 'strategy') === SCRIPT_TAGS)) {
let prefix = '';
if (config.rootURL) {
prefix += config.rootURL;
}
if (addonConfig.assetPath) {
prefix += addonConfig.assetPath;
}
if (autoPolyfill.complete) {
return `<script src="${prefix}/intl.complete.js"></script>`;
}
let locales = get(autoPolyfill, 'locales') || get(addonConfig, 'locales') || [];
if (locales.length > 0) {
debug(`inserting ${locales.join(',')}`);
let scripts = locales.map(locale => `<script src="${prefix}/locales/${locale}.js"></script>`);
return [`<script src="${prefix}/intl.min.js"></script>`].concat(scripts).join('\n');
}
}
}
},
intlRelativeToProject(basedir) {
try {
/* project with intl as a dependency takes priority */
let resolve = require('resolve');
return path.dirname(resolve.sync('intl', { basedir: basedir }));
} catch(_) {
try {
return path.dirname(require.resolve('intl'));
} catch(e) {
if (e.code === 'MODULE_NOT_FOUND') {
this.ui.writeLine(`@ember-intl/polyfill: intl polyfill could not be found. Try "npm install" to ensure your dependencies are fully installed.`)
return;
}
throw e;
}
}
},
importPolyfill(app) {
let addonConfig = this.addonConfig;
if (addonConfig.disablePolyfill) {
return;
}
if (get(addonConfig, 'autoPolyfill.strategy') === VENDOR) {
let locales = get(addonConfig, 'autoPolyfill.locales');
if (!locales || (locales && locales.length === 0)) {
locales = get(addonConfig, 'locales') || [];
}
if (get(addonConfig, 'autoPolyfill.complete')) {
this.appImport(app, 'vendor/intl/intl.complete.js');
}
else if (typeof get(addonConfig, 'autoPolyfill.complete') === 'undefined' && locales.length === 0) {
this.ui.writeLine('@ember-intl/polyfill: no locales were provided or discovered. Defaulting to the complete locale dataset for Intl polyfill.');
this.ui.writeLine('To silence this error, either set `autoPolyfill.complete = true` or specify `locales` within config/ember-intl.config');
this.appImport(app, 'vendor/intl/intl.complete.js');
}
else {
this.appImport(app, 'vendor/intl/intl.min.js');
locales.forEach(locale => this.appImport(app, `vendor/intl/locales/${locale}.js`));
}
}
if (addonConfig.forcePolyfill) {
this.appImport(app, 'vendor/intl/force-polyfill.js');
}
},
appImport(app, filename, options) {
debug(`importing: "${filename}"`);
app.import(filename, options);
},
createPolyfillTree() {
let addonConfig = this.addonConfig;
let locales = this.addonConfig.locales || [];
let tree = new UnwatchedDir(this._nodeModulePath);
let trees = [];
trees.push(
funnel(tree, {
srcDir: 'dist',
include: ['*.map']
})
);
let polyfillTree = funnel(tree, {
srcDir: 'dist',
include: ['*.js']
});
let localeFunnel = {
srcDir: 'locale-data/jsonp',
destDir: `/locales`
};
/* if empty, all locales are included within the tree */
if (locales.length > 0) {
localeFunnel.include = addonConfig.locales.map(locale => new RegExp(`^${locale}.js$`, 'i'));
}
let localesTree = funnel(tree, localeFunnel);
trees.push(lowercaseTree(mergeTrees([polyfillTree, localesTree])));
return mergeTrees(trees);
},
treeForVendor(tree) {
if (this.addonConfig.disablePolyfill) {
return;
}
let trees = [];
if (tree && this.addonConfig.forcePolyfill) {
trees.push(tree);
}
trees.push(funnel(this.createPolyfillTree(), {
destDir: 'intl'
}));
return mergeTrees(trees);
},
treeForPublic() {
if (this.addonConfig.disablePolyfill) {
return;
}
let tree = this.createPolyfillTree();
return funnel(tree, {
destDir: this.addonConfig.assetPath
});
}
};