ember-cli-moment-shim
Advanced tools
Comparing version 3.6.0 to 3.7.0
107
index.js
@@ -10,6 +10,7 @@ /* globals require, module, process */ | ||
const funnel = require('broccoli-funnel'); | ||
const existsSync = require('exists-sync'); | ||
const concat = require('broccoli-concat'); | ||
const stew = require('broccoli-stew'); | ||
const chalk = require('chalk'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
@@ -19,2 +20,68 @@ const rename = stew.rename; | ||
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 = { | ||
@@ -64,3 +131,3 @@ name: 'moment', | ||
} else { | ||
if (Array.isArray(options.includeLocales)) { | ||
if (Array.isArray(options.includeLocales) && !options.singleModule) { | ||
options.includeLocales.forEach(locale => { | ||
@@ -88,2 +155,3 @@ this.import('vendor/moment/locales/' + locale + '.js', { | ||
let config = defaults(projectConfig, { | ||
singleModule: false, | ||
momentPath: momentPath, | ||
@@ -109,3 +177,3 @@ includeTimezone: null, | ||
if (!existsSync(momentPath + '/locale/' + locale + '.js')) { | ||
if (!fs.existsSync(momentPath + '/locale/' + locale + '.js')) { | ||
this.ui.writeLine( | ||
@@ -152,9 +220,14 @@ chalk.red( | ||
treeForVendor(vendorTree) { | ||
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; | ||
if (vendorTree) { | ||
trees.push(vendorTree); | ||
} | ||
const superValue = this._super.apply(this, arguments); | ||
trees.push(superValue); | ||
@@ -171,17 +244,2 @@ trees.push( | ||
if ( | ||
Array.isArray(options.includeLocales) && | ||
options.includeLocales.length | ||
) { | ||
let localeTree = funnel(this.momentNode, { | ||
srcDir: 'locale', | ||
destDir: 'moment/locales', | ||
include: options.includeLocales.map( | ||
locale => new RegExp(locale + '.js$') | ||
) | ||
}); | ||
trees.push(localeTree); | ||
} | ||
if (options.includeTimezone) { | ||
@@ -237,4 +295,7 @@ let timezonePath; | ||
const definitionModules = getLocaleDefinitionModules.call(this, 'vendor'); | ||
trees.push(definitionModules); | ||
return map( | ||
mergeTrees(trees), | ||
mergeTrees(trees.filter(Boolean)), | ||
content => `if (typeof FastBoot === 'undefined') { ${content} }` | ||
@@ -241,0 +302,0 @@ ); |
{ | ||
"name": "ember-cli-moment-shim", | ||
"version": "3.6.0", | ||
"version": "3.7.0", | ||
"description": "Brings moment and moment-timezone into your Ember applications", | ||
@@ -33,2 +33,3 @@ "keywords": [ | ||
"ember-cli-babel": "^6.6.0", | ||
"broccoli-concat": "^3.2.2", | ||
"broccoli-funnel": "^2.0.0", | ||
@@ -40,3 +41,2 @@ "broccoli-merge-trees": "^2.0.0", | ||
"ember-cli-import-polyfill": "^0.2.0", | ||
"exists-sync": "^0.0.4", | ||
"lodash.defaults": "^4.2.0", | ||
@@ -43,0 +43,0 @@ "moment": "^2.19.3", |
@@ -5,3 +5,2 @@ # ember-cli-moment-shim | ||
[![Ember Observer Score](http://emberobserver.com/badges/ember-cli-moment-shim.svg)](http://emberobserver.com/addons/ember-cli-moment-shim) | ||
[![Ember badge][ember-badge]][embadge] | ||
@@ -85,2 +84,56 @@ ember-cli ES6 module shim for [momentjs](https://momentjs.com) and [moment timezone](https://momentjs.com/timezone/) within your Ember applications. It will also conditionally bundle in specific locale/timezone data for those concerned about payload size. | ||
### Single module use | ||
The default behavior for loading moment locales is an IIFE which is added into | ||
the `vendor.js` file. The consequence of that is that you have no control over | ||
when that code is run. That code also triggers [setting of the moment locale](https://github.com/moment/moment/blob/f6c7069/src/lib/locale/locales.js#L132), | ||
which means that at arbitrary times in your application you don't know what the | ||
locale is. | ||
Included in `ember-cli-moment-shim` is a module that allows you to specify which | ||
localization you would like to load, and when you would like it invoked. This | ||
enables you to configure timing based upon application constraints. (With the | ||
initial state being `en`.) | ||
```js | ||
// config.environment.js | ||
module.exports = function(environment) { | ||
return { | ||
moment: { | ||
includeLocales: ['es', 'fr-ca'], | ||
singleModule: true | ||
} | ||
}; | ||
``` | ||
```js | ||
// app/routes/applicaton.js | ||
import Route from '@ember/routing/route'; | ||
import defineLocale from 'ember-cli-moment-shim/define-locale'; | ||
import moment from 'moment'; | ||
import { inject as service } from '@ember/service'; | ||
export default Route.extend({ | ||
moment: service(), | ||
beforeModel() { | ||
const desiredLocale = 'en-us'; | ||
// This adds the configuration for *just* the locale you want. | ||
// All locales specified as bundled by `includeLocales` will be available. | ||
defineLocale(desiredLocale); | ||
// Set the locale: | ||
moment.locale(desiredLocale); | ||
// It is possible for the locale in the `ember-moment`-supplied service to | ||
// get out of sync with the locale set in moment itself: | ||
// https://github.com/stefanpenner/ember-moment/blob/b8a262b/addon/services/moment.js#L50-L53 | ||
// If using both the service and ES6 approach be sure to keep those values | ||
// consistent. | ||
this.get('moment').setLocale(desiredLocale); | ||
} | ||
}); | ||
``` | ||
### Write all locales to a folder that is relative to `dist` | ||
@@ -106,5 +159,3 @@ | ||
[embadge]: http://embadge.io/ | ||
[ember-badge]: http://embadge.io/v1/badge.svg?start=1.0.0 | ||
[npm]: https://www.npmjs.org/package/ember-cli-moment-shim | ||
[npm-badge]: https://img.shields.io/npm/v/ember-cli-moment-shim.svg?style=flat-square |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
251719
293
159
2
+ Addedbroccoli-concat@^3.2.2
+ Addedamdefine@1.0.1(transitive)
+ Addedbroccoli-concat@3.7.5(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedelectron-to-chromium@1.4.773(transitive)
+ Addedfast-sourcemap-concat@1.4.0(transitive)
+ Addedfind-index@1.1.1(transitive)
+ Addedfs-extra@4.0.3(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedjsesc@0.3.0(transitive)
+ Addedlodash._reinterpolate@3.0.0(transitive)
+ Addedlodash.foreach@4.5.0(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedlodash.omit@4.5.0(transitive)
+ Addedlodash.template@4.5.0(transitive)
+ Addedlodash.templatesettings@4.2.0(transitive)
+ Addedlodash.uniq@4.5.0(transitive)
+ Addedmemory-streams@0.1.3(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedsource-map@0.1.430.4.4(transitive)
+ Addedsource-map-url@0.3.0(transitive)
+ Addedsourcemap-validator@1.1.1(transitive)
+ Addedstring_decoder@0.10.31(transitive)
- Removedexists-sync@^0.0.4
- Removedelectron-to-chromium@1.4.774(transitive)
- Removedexists-sync@0.0.4(transitive)