Skip to content

Commit

Permalink
feat: timezone support (#48)
Browse files Browse the repository at this point in the history
* feat: timezone support

* docs: formatting
  • Loading branch information
ricardogobbosouza committed Jan 3, 2020
1 parent cdc0abb commit 978ee47
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,39 @@ export default {

**Note:** Don't forget to install each plugin.

### Timezone

You can enable [moment-timezone](https://momentjs.com/timezone/) via the `timezone` option.

```js
export default {
buildModules: [
'@nuxtjs/moment'
],
moment: {
timezone: true
}
}
```

You can filter time zone data and thus produce significant savings in file size.
See all options in [moment-timezone-data-webpack-plugin](https://github.com/gilmoreorless/moment-timezone-data-webpack-plugin).

```js
export default {
buildModules: [
'@nuxtjs/moment'
],
moment: {
timezone: {
matchZones: /Europe\/(Belfast|London|Paris|Athens)/,
startYear: 2000,
endYear: 2030
}
}
}
```

### Disable plugin

This module also registers a plugin to include all needed locales as well as injecting moment as `$moment` to Vue context. You can disable this behaviour using `plugin: false`.
Expand Down
12 changes: 11 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const defaults = {
locales: [],
defaultLocale: false,
plugin: true,
plugins: []
plugins: [],
timezone: false
}

function momentModule (moduleOptions) {
Expand All @@ -18,11 +19,20 @@ function momentModule (moduleOptions) {
...moduleOptions
}

if (options.timezone && !options.plugins.includes('moment-timezone')) {
options.plugins.push('moment-timezone')
}

this.extendBuild((config) => {
const MomentLocalesPlugin = require('moment-locales-webpack-plugin')
config.plugins.push(new MomentLocalesPlugin({
localesToKeep: options.locales
}))

if (Object.keys(options.timezone).length) {
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin')
config.plugins.push(new MomentTimezoneDataPlugin(options.timezone))
}
})

if (!options.plugin) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
},
"dependencies": {
"moment": "^2.24.0",
"moment-locales-webpack-plugin": "^1.1.2"
"moment-locales-webpack-plugin": "^1.1.2",
"moment-timezone": "^0.5.27",
"moment-timezone-data-webpack-plugin": "^1.1.0"
},
"devDependencies": {
"@commitlint/cli": "latest",
Expand Down
14 changes: 14 additions & 0 deletions test/fixture/timezone/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
rootDir: __dirname,
render: {
resourceHints: false
},
buildModules: [
{ handler: require('../../../') }
],
moment: {
timezone: {
matchZones: 'America/Los_Angeles'
}
}
}
5 changes: 5 additions & 0 deletions test/fixture/timezone/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
{{ $moment('2014-06-01T12:00:00Z').tz('America/Los_Angeles').format('ha z') }}
</div>
</template>
18 changes: 18 additions & 0 deletions test/timezone.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils')

describe('timezone', () => {
let nuxt

beforeAll(async () => {
({ nuxt } = (await setup(loadConfig(__dirname, 'timezone'))))
}, 60000)

afterAll(async () => {
await nuxt.close()
})

test('render', async () => {
const html = await get('/')
expect(html).toContain('5am PDT')
})
})
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6668,7 +6668,22 @@ moment-strftime@latest:
dependencies:
moment "^2.11.2"

moment@^2.11.2, moment@^2.24.0:
moment-timezone-data-webpack-plugin@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/moment-timezone-data-webpack-plugin/-/moment-timezone-data-webpack-plugin-1.1.0.tgz#6e77b0353894054c5982fc3bc65e689105629fdd"
integrity sha512-szKf9rbRoY9u3WNcwK6D0tdCREk/OZkcF1k163Xc5m7GcqBh28LgNVWisb4sje6/qdG3WUkFD5hJ7/lmKOkBAA==
dependencies:
find-cache-dir "^3.0.0"
make-dir "^3.0.0"

moment-timezone@^0.5.27:
version "0.5.27"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.27.tgz#73adec8139b6fe30452e78f210f27b1f346b8877"
integrity sha512-EIKQs7h5sAsjhPCqN6ggx6cEbs94GK050254TIJySD1bzoM5JTYDwAU1IoVOeTOL6Gm27kYJ51/uuvq1kIlrbw==
dependencies:
moment ">= 2.9.0"

"moment@>= 2.9.0", moment@^2.11.2, moment@^2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
Expand Down

0 comments on commit 978ee47

Please sign in to comment.